python数据分析十九:matplotlib的常规用法、参数设置

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
#布局figure 数据subplot
#快捷方式
fig,axes=plt.subplots(2,2,sharex=True,sharey=True)#创建了4个figure对象,x,y轴的值保持一致
for i in range(2):
    for j in range(2):
        axes[i,j].hist(np.random.randn(500),bins=50,color='r',alpha=1)#axes[i,j] 遍历 是设定图片的位置

plt.subplots_adjust(wspace=0,hspace=0)#左右边界,上下边界
fig.show()
print(axes)
# [[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000B064978>
#   <matplotlib.axes._subplots.AxesSubplot object at 0x000000000D0D1CF8>]
#  [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000D0FDEF0>
#   <matplotlib.axes._subplots.AxesSubplot object at 0x000000000D12C320>]]


'''
颜色标记和线性
'''

plt.plot(np.random.randn(30).cumsum(),'ko--')


#设定数据
data=np.random.randn(30).cumsum()

#两种不同的图形
plt.plot(data,'k--',label='Default')

plt.plot(data,'k-',drawstyle='steps-post',label='steps-post')

#右上角显示图例
plt.legend(loc='best')
plt.show()


 
 
'''
设定标题,轴标签,刻度,刻度标签
'''
#获取figure对象
fig=plt.figure()

#布局
ax=fig.add_subplot(1,1,1)
ax.plot(np.random.randn(1000).cumsum(),'k-',label='one')

ax.plot(np.random.randn(1000).cumsum(),'k--',label='two')

ax.plot(np.random.randn(1000).cumsum(),'k.',label='three')
#设置x的刻度
ax.set_xticks([0,250,500,750,1000])

#设置x刻度的另一种方式
ax.set_xticklabels(['one','two','three','froe','five'],rotation=30,fontsize='small')

#设置标题
ax.set_title('HHB')

ax.legend(loc='best')
#设置x轴的名称
ax.set_xlabel('x_Name')
fig.show()


 
 



猜你喜欢

转载自blog.csdn.net/qq_38788128/article/details/80803349
今日推荐