一,创建方式
import matplotlib.pypolt as plt
#方法一:
##创建画板
fig = plt.figure()
##创建画布
ax = fig.add_subplot(2,2,2)
#ax = fig.add_subplot(222)
#方式二
fig,axs = plt.subplots(nrows=2,ncols=3)
##获取画布
a = ax[1][2]
#方式三
fig,axes = plt.subplots(nrows=2,ncols=2)
ax0,ax1,ax2,ax3 = axes.flatten()
二,常用的创建图表
类别 | 方法 | 属性 |
---|---|---|
散点图 | scatter | color:颜色,marker:点的类型 |
条形图 | bar(水平),barh(垂直) | |
直方图 | hist | density控制Y轴是概率还是数量 |
饼图 | pie | autopct=%1.1f%%表示格式化百分比精确输出,explode,突出某些块,不同的值突出的效果不一样。pctdistance=1.12百分比距离圆心的距离,默认是0.6. |
箱型图 | boxplot | verft:控制方向 |
三,常用属性
1.ax.legend()
用来显示数据说明,可以控制数据的大小,legend({size:7})
Location String | Location Code |
---|---|
‘best’ | 0 |
‘upper right’ | 1 |
‘upper left’ | 2 |
‘lower left’ | 3 |
‘lower right’ | 4 |
‘right’ | 5 |
‘center left’ | 6 |
‘center right’ | 7 |
‘lower center’ | 8 |
‘upper center’ | 9 |
2.fig.subplots_adjust()设置布局
fig.subplots_adjust(wspace=0.5, hspace=0.3,
left=0.125, right=0.9,
top=0.9, bottom=0.1)
#fig.tight_layout() #自动调整布局,使标题之间不重叠
3.边框
%matplotlib inline
import matplotlib.pyplot as plt
fig,axs = plt.subplots()
axs.plot([-2,2,3,4],[-10,20,25,5])
#去除变界
axs.spines['top'].set_visible(False)
#axs.spines['left'].set_visible(False)
axs.spines['right'].set_visible(False)
#axs.spines['bottom'].set_visible(False)
#指定坐标轴
axs.yaxis.set_ticks_position('left')
axs.xaxis.set_ticks_position('bottom')
#移动坐标轴
axs.spines['bottom'].set_position(('data', 0))
axs.spines['left'].set_position(('data', 0))
plt.show()