python数据处理
前言
pyplot 简介
matplotlib.pyplot是使 matplotlib 像 MATLAB 一样工作的函数集合。每个pyplot函数都会对图形进行一些更改:例如,创建图形、在图形中创建绘图区域、在绘图区域中绘制一些线条、用标签装饰绘图等。
在matplotlib.pyplot函数调用中保留各种状态,以便跟踪当前图形和绘图区域等内容,并且绘图函数指向当前轴(请注意此处和文档中的大多数地方的“轴”是指到图形的轴 部分, 而不是用于多个轴的严格数学术语)。
利用python,把数据以图表(柱状图)的形式直观的展示出来
一、柱状图
#导入matplotlib里面的pyplot包,简写成plt
import matplotlib.pyplot as plt
制作简单柱状图,使用plt.bar()方法。
import matplotlib.pyplot as plt
num_list = [9.7,14.2,5,8] #数据列表
ind = list(range(len(num_list))) #索引序列转换为列表
plt.bar(ind,num_list)#绘制柱状图,索引列表和数据列表一一对应
plt.show()#展示图表
接下来我们再绘制一个消费对比表。
#绘制一个两月的消费对比表
import matplotlib.pyplot as plt
jan = [345,234,500,450]
feb = [456,230,500,350]
ind = list(range(len(jan)))
#width=0.3柱状图每隔住的宽度
plt.bar(ind,jan,width=0.3)
for i in range(len(ind)):
ind[i] = ind[i] + 0.3
plt.bar(ind,feb,width=0.3)
plt.show()
二、饼状图
#饼状图
import matplotlib.pyplot as plt
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
#explode=explode让其中一块饼突出显示出来0.1代表的就是突出的距离
explode = (0, 0.1, 0, 0)
fig1, ax1 = plt.subplots()
#labels=labels给每一块饼加上标签
#autopct='%1.1f%%' 每个饼上显示百分比
#shadow=True给饼加上阴影
#startangle=90饼的角度是90,如果不写这个参数则是从0度开始
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal')
#给饼图加上图例
ax1.legend(title="slice",loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
#设置饼图标题
ax1.set_title("slice")
plt.show()
#饼状图
import matplotlib.pyplot as plt
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
fig1, ax1 = plt.subplots()
#startangle=90饼的角度是90,如果不写这个参数则是从0度开始
#wedgeprops=dict(edgecolor='k',width=0.8)设置饼的边界颜色,
#width设置饼的实际宽度,设置为0.8,表示饼的实际宽度比半径要小一些,饼的中间就会出现一个半径为0.2的洞
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True,wedgeprops=dict(edgecolor='k',width=0.8))
ax1.axis('equal')
plt.show()
#我们可以为autopct参数提供一个函数,它会通过显示绝对值来扩展自动百分比标记;我们根据相关数据和所有值的已知总和计算后者。
def func(pct, allvals):
absolute = int(round(pct/100.*np.sum(allvals)))
return "{:.1f}%\n({:d})".format(pct, absolute)
#lambda pct: func(pct, sizes)定义函数的一种方法
ax1.pie(sizes, explode=explode, labels=labels,autopct=lambda pct: func(pct, sizes),
shadow=True,wedgeprops=dict(edgecolor='k',width=0.8))
代码如下:
#饼状图
import matplotlib.pyplot as plt
import numpy as np
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
fig1, ax1 = plt.subplots()
#我们可以为autopct参数提供一个函数,它会通过显示绝对值来扩展自动百分比标记;我们根据相关数据和所有值的已知总和计算后者。
def func(pct, allvals):
absolute = int(round(pct/100.*np.sum(allvals)))
return "{:.1f}%\n({:d})".format(pct, absolute)
#lambda pct: func(pct, sizes)定义函数的一种方法
ax1.pie(sizes, explode=explode, labels=labels,autopct=lambda pct: func(pct, sizes),
shadow=True,wedgeprops=dict(edgecolor='k',width=0.8))
ax1.axis('equal')
plt.show()
最后,我们来看一下pie函数的返回值
#我们来看一下pie函数的返回值
wedges, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels,autopct=lambda pct: func(pct, sizes),
shadow=True,wedgeprops=dict(edgecolor='k',width=0.8))
print('Wedges = ',wedges)#对应三块饼
print('Texts = ',texts)#对应标签,文本对象
print('Autotexts = ',autotexts)#也是三个文本对象,分别对应三个百分比字符串
我们可以使用图例的bbox_to_anchor参数将图例定位在饼图之外。这里我们使用轴坐标和位置;即图例的左中心点将位于边界框的左中心点,从到in 轴坐标。(1, 0, 0.5, 1)“center left”(1, 0)(1.5, 1)
通过pie()函数的返回值,也可以修改对应的参数,以‘图例’为例。
import matplotlib.pyplot as plt
import numpy as np
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
fig1, ax1 = plt.subplots()
def func(pct, allvals):
absolute = int(round(pct/100.*np.sum(allvals)))
return "{:.1f}%\n({:d})".format(pct, absolute)
#我们来看一下pie函数的返回值
wedges, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels,autopct=lambda pct: func(pct, sizes),
shadow=True,wedgeprops=dict(edgecolor='k',width=0.8))
#print('Wedges = ',wedges)#对应三块饼
#print('Texts = ',texts)#对应标签,文本对象
#print('Autotexts = ',autotexts)#也是三个文本对象,分别对应三个百分比字符串
ax1.legend(wedges, labels,
title="slice",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
ax1.axis('equal')
plt.show()