在平时的科研中,Python实现可视化的三个步骤:

#导入包import numpy as npimport pandas as pdimport matplotlib.pyplot as plt#创建画布fig = plt.figure()<Figure size 432x288 with 0 Axes>#创建subplot,221表示这是2行2列表格中的第1个图像。ax1 = fig.add_subplot(221)#但现在更习惯使用以下方法创建画布和图像,2,2表示这是一个2*2的画布,可以放置4个图像fig , axes = plt.subplots(2,2,sharex=True,sharey=True)#plt.subplot的sharex和sharey参数可以指定所有的subplot使用相同的x,y轴刻度。
subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=None,hspace=None)
plt.plot(np.random.randn(30),color='g',linestyle='--',marker='o')[<matplotlib.lines.Line2D at 0x8c919b0>]
plt.plot(np.random.randn(30),color='g',linestyle='--',marker='o')plt.xlim() #不带参数调用,显示当前参数;#可将xlim替换为另外两个方法试试(-1.4500000000000002, 30.45)
plt.plot(np.random.randn(30),color='g',linestyle='--',marker='o')plt.xlim([0,15]) #横轴刻度变成0-15(0, 15)
fig = plt.figure();ax = fig.add_subplot(1,1,1)ax.plot(np.random.randn(1000).cumsum())ticks = ax.set_xticks([0,250,500,750,1000]) #设置刻度值labels = ax.set_xticklabels(['one','two','three','four','five']) #设置刻度标签ax.set_title('My first Plot') #设置标题ax.set_xlabel('Stage') #设置轴标签Text(0.5,0,'Stage')
fig = plt.figure(figsize=(12,5));ax = fig.add_subplot(111)ax.plot(np.random.randn(1000).cumsum(),'k',label='one') #传入label参数,定义label名称ax.plot(np.random.randn(1000).cumsum(),'k--',label='two')ax.plot(np.random.randn(1000).cumsum(),'k.',label='three')#图形创建完后,只需要调用legend参数将label调出来即可。ax.legend(loc='best') #要求不是很严格的话,建议使用loc=‘best’参数来让它自己选择最佳位置<matplotlib.legend.Legend at 0xa8f5a20>
plt.plot(np.random.randn(1000).cumsum())plt.text(600,10,'test ',family='monospace',fontsize=10)#中文注释在默认环境下并不能正常显示,需要修改配置文件,使其支持中文字体。具体步骤请自行搜索。plt.savefig('./plot.jpg') #保存图像为plot名称的jpg格式图像<Figure size 432x288 with 0 Axes>3、Pandas中的绘图函数
import matplotlib.pyplot as plts = pd.Series(np.random.randn(10).cumsum(),index=np.arange(0,100,10))s.plot() #Series对象的索引index会传给matplotlib用作绘制x轴。<matplotlib.axes._subplots.AxesSubplot at 0xf553128>
df = pd.DataFrame(np.random.randn(10,4).cumsum(0),columns=['A','B','C','D'])df.plot() #plot会自动为不同变量改变颜色,并添加图例<matplotlib.axes._subplots.AxesSubplot at 0xf4f9eb8>
fig,axes = plt.subplots(2,1)data = pd.Series(np.random.rand(10),index=list('abcdefghij'))data.plot(kind='bar',ax=axes[0],rot=0,alpha=0.3)data.plot(kind='barh',ax=axes[1],grid=True)<matplotlib.axes._subplots.AxesSubplot at 0xfe39898>
本文来源:https://blog.csdn.net/m0_72557783/article/details/125623698。仅用于传递和分享更多信息,并不代表本平台赞同其观点和对其真实性负责,版权归原作者所有,如有侵权请联系我们删除。
如需加入SCI、CSCD论文、课题等科研数据统计分析交流群,请加小编微信:tj211005,小编拉您进群。
本公众提供各种科研服务