目录
- 条形图
bar
- 直方图
hist
- 散点图
scatter
- 堆叠图
stackplot
- 饼图
pie
条形图
'''
**kwargs参数有很多,请参照官方文档
'''
bar(x, height, *, align='center', **kwargs)
bar(x, height, width, *, align='center', **kwargs)
bar(x, height, width, bottom, *, align='center', **kwargs)
效果图:
代码实现:
import matplotlib.pyplot as plt
from matplotlib import font_manager
plt.bar([1, 3, 5, 7, 9], [5, 2, 7, 8, 2], label="一班")
plt.bar([2, 4, 6, 8, 10], [8, 6, 2, 5, 6], label="二班", color='g')
#字体文件
zhfont = font_manager.FontProperties(fname='../config/simhei.ttf')
#标签要添加prop属性
plt.legend(prop=zhfont)
#坐标系要添加fontproperties属性
plt.xlabel('横坐标 ', fontproperties=zhfont)
plt.ylabel('纵坐标', fontproperties=zhfont)
plt.title('标题', fontproperties=zhfont)
plt.show()
资源:字体文件资源
直方图
#调用的方法
matplotlib.pyplot.hist(
x,
bins=None, #区间范围,如bins=[1, 2, 3, 4],则第一个区间为[1,2),第二个区间为[2,3)...依次类推
range=None,
density=None,
weights=None,
cumulative=False,
bottom=None, #改变纵坐标基数,原纵坐标数值全部加上该基数
histtype='bar', #{'bar','barstacked','step','stepfilled'},可选
align='mid',
orientation='vertical', #方向 {'horizontal','vertical'}
rwidth=None, #矩形的宽度占区间的百分比,最大为1
log=False, #如果为True,则直方图轴将被设置为对数刻度
color=None, #直方图的颜色
label=None, #直方图代表的名称
stacked=False, #堆叠
normed=None, #如果normed=True,则纵坐标数值会变,全部的矩形面积之和为1;
hold=None,
data=None,
**kwargs
)
效果图:
代码实现:
population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,110,120,121,122,130,111,115,112,80,75,65,54,44,43,42,48]
bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]
plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)
plt.xlabel('The X_Axes desc')
plt.ylabel('The Y_Axes desc')
plt.title('Chart title')
plt.legend()
plt.show()
散点图
matplotlib.pyplot.scatter(
x,
y,
s=None,
c=None,
marker=None,
cmap=None,
norm=None,
vmin=None,
vmax=None,
alpha=None,
linewidths=None,
verts=None,
edgecolors=None,
hold=None,
data=None,
**kwargs
)
效果图:
代码实现:
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8]
y = [5,2,4,2,1,4,5,2]
plt.scatter(x,y, label='Label1', color='k', s=25, marker="o")
plt.xlabel('The X_Axes desc')
plt.ylabel('The Y_Axes desc')
plt.title('Chart title')
plt.legend()
plt.show()
堆叠图
stackplot(x, y) # where y is MxN(2d array of dimension MxN)
stackplot(x, y1, y2, y3, y4) # where y1, y2, y3, y4, are all 1xNm
stackplot(x, y1, y2, y3, y4, labels=[], colors=[])
效果图:
代码实现:
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]
plt.stackplot(days, sleeping, eating, working, playing, colors=['m','c','r','k'], labels=['label1', 'label2', 'label3', 'label4'])
plt.xlabel('The X_Axes desc')
plt.ylabel('The Y_Axes desc')
plt.title('Chart title')
plt.legend()
plt.show()
饼图
matplotlib.pyplot.pie(
x,
explode=None, #突出的部分
labels=None, #含义
colors=None, #颜色
autopct=None,
pctdistance=0.6,
shadow=False, #阴影
labeldistance=1.1,
startangle=None, #开始的角度
radius=None, #饼图半径
counterclock=True,
wedgeprops=None,
textprops=None,
center=(0, 0),
frame=False,
hold=None,
data=None
)
效果图:
代码实现:
import matplotlib.pyplot as plt
slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
cols = ['c','m','r','b']
plt.pie(slices,
labels=activities,
colors=cols,
startangle=90,
shadow= True,
explode=(0,0.1,0,0),
autopct='%1.1f%%')
plt.title('Chart title')
plt.show()