生成多个图
import numpy as np
import pandas as pda
import matplotlib.pyplot as plt
import seaborn as sns
from pandas import Series
from numpy.random import randn
plt.rcParams['font.sans-serif']=['simHei']
fig,axes=plt.subplots(2,2,sharex=True,sharey=True) #生成4个subplot sharex,sharey共用x和y轴
for i in range(2):
for j in range(2):
axes[i,j].hist(randn(500),bins=10,color='k',alpha=1) #bins表示分组数,alpha透明度
plt.subplots_adjust(wspace=0.1,hspace=0.1) #间距

设置X轴Y轴的刻度,标签
fig=plt.figure(figsize=(10,6),frameon=True,facecolor="#ECECEC") #定义画布
ax=fig.add_subplot(1,1,1) #划分
ax.plot(randn(1000).cumsum(),'g--') #随机漫步图,'g--'=> color='green',linestyle="--"
ax.set_xticks([0,250,500,750,1000]) #设置刻度
ax.set_xticklabels(['one','two','three','four','five'],rotation=30,fontsize='large')
ax.set_xlabel("x轴")
ax.set_ylabel("y轴")
fig=plt.figure(figsize=(10,6),frameon=True,facecolor="#ECECEC")
plt.plot(randn(1000).cumsum())
plt.xlabel("注意")
plt.xticks(rotation=45)

将多组数据合并到同一张图中,并设置图例
fig=plt.figure(figsize=(10,6),frameon=True,facecolor="#ECECEC")
ax=fig.add_subplot(1,1,1)
ax.plot(randn(100).cumsum(),'r',label='one')
ax.plot(randn(100).cumsum(),'k--',label='two')
ax.plot(randn(100).cumsum(),'g.',label='three')
ax.legend(loc="best") #选择一个不碍事的地方待着
幸甚至哉,歌以咏志
| ID | NAME |
| CarryJoe | 周楷钥 |
| ZKy | ZKy |
本文详细介绍了使用Python的Matplotlib库进行数据可视化的方法,包括创建多个子图、调整子图间距、设置轴标签和刻度,以及如何在一个图表中合并多组数据并设置图例。通过实例展示了如何生成直方图、随机漫步图等,是数据分析师和科研人员不可多得的绘图指南。
1241

被折叠的 条评论
为什么被折叠?



