Python数据可视化——循环子图,图例位置、大小和样式
部分数据展示
代码
import pandas as pd import matplotlib.pyplot as plt import matplotlib as mpl data = pd.read_excel("data.xlsx",'y3') df = pd.read_excel("data.xlsx",'y2') column = data.columns.tolist()[1:39] # 列表头 # matplotlib其实是不支持显示中文的 显示中文需要一行代码设置字体 mpl.rcParams['font.family'] = 'STKAITI' #'STKAITI'——字体 plt.rcParams['axes.unicode_minus'] = False # 解决坐标轴负数的负号显示问题 # 设置figure大小、像素 fig=plt.figure(figsize=(12,8), dpi=100) for i in range(38): plt.subplot(5, 8, i + 1) # 5行8列子图 plt.legend(frameon=False) plt.plot(data['txt'],data[column[i]],marker='*',) #数字化 plt.plot(df['txt'],df[column[i]],marker='^',) #传统 plt.xlabel(column[i], fontsize=15) plt.ylim(0,1.2) #y范围 plt.xlim(2011,2021)#x范围 fig.tight_layout(h_pad=1) #子图的内边距 plt.xticks(fontsize=12) #坐标轴字体大小 plt.yticks(fontsize=12) #坐标轴字体大小 fig.legend(["1","2"],bbox_to_anchor=(1,0.2),prop = {'size':15}) #图例位置,大小 plt.show()
bbox_to_anchor() (1,0)#右下角 (0,1)#左上角 (0.5,0.5)#中间 (0.4,0.4)#中间往左下一点 legend(ncol=1) #默认为1,并列展示;2为横向展示
本文作者:小平凡的记录
本文链接:https://www.cnblogs.com/Rshimmer/p/17364157.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步