Use matplotlib to display 20 double y-axis line charts on the same canvas

Click on " Python Crawler and Data Mining " above to pay attention

Reply to " Books " to get a total of 10 e-books on Python from entry to advanced

now

Day

chicken

Soup

She said that she was originally a girl from the capital, and her family lived under Xiamaling.

Hello everyone, I am Pippi.

I. Introduction

A few days ago, I asked a Pythonvisualization question in the Python silver group [巭嫑勥烎], and I will share it with you here.

ade3fe25f0d41b814d02601927a77726.png

His code is shown below:

acd3730f0e67abb532e12be0caa6a0e6.png

Using supblot partition mapping can indeed be easily solved.

2. Implementation process

Here [E] gives an idea, taking 4 pictures as an example.

567d39408da42da783bc1d933b49fd10.png
# -*- coding: UTF-8 -*-
# 开发时间:2023/3/12 14:57
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# names = ['01.12', '01.13', '01.14', '01.15', '01.16']
# x = range(len(names))
Data = pd.read_excel(r"H:\小论文\小论文\白城空气质量气象数据\空气质量数据\白城2015.10.26.xlsx")
X1 = Data.Date
Y1 = Data.AQI
Y2 = Data["PM2.5"]
Y3 = Data.PM10

# 创建两幅子图
f, ax = plt.subplots(4,1,figsize=(15, 10))
plt.subplots_adjust(wspace =0, hspace =0)

# 选择第一幅图
ax1 = plt.subplot(411)
figure_1 = plt.subplot(411)
# 设置y轴的范围
figure_1.set_ylim(bottom=0,top=520)
plt.plot(X1, Y1, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="yellow", linestyle='-', label='AQI',markersize = 5)
figure_1.legend( ncol=1, frameon=False)
plt.ylabel("AQI")  # Y轴标签
# figure_1.axvline(Data.Date[47], color='red',linestyle = "-.")
# figure_1.axvline(Data.Date[58], color='red',linestyle = "-.")
plt.title("2015.10.26",loc = "center")

ax3 = figure_1.twinx()
ax3.plot(X1, Y2, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="lime", linestyle='-', label='PM$_2$$_.$$_5$',markersize = 5)
ax3.plot(X1, Y3, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="red", linestyle='-', label='PM$_1$$_0$',markersize = 5)
ax3.tick_params(axis='y', colors='black')
ax3.spines["right"].set_color("black")  # 修改右边颜色
ax3.legend(bbox_to_anchor=(0.92, 1),frameon=False,ncol=3,)  # 显示图例
plt.ylabel("PM$_2$$_.$$_5$/PM$_1$$_0$(${ug/m^3}$)")  # Y轴标签
# ax3.set_ylim(0,100)
ax3.set_ylim(bottom=0,top=800)
# plt.title("污染前",loc = "left")
# plt.title("污染前",x = 0.18,y = 1.05)
# plt.title("污染时",x = 0.5,y = 1.05)
# plt.title("污染后",loc = "right")
t1 = "污染前"
# plt.text(Data.Date[52], 500, t1, ha='left', rotation=15, wrap=True)

Y4 = Data.SO2
Y5= Data.CO
ax2 = plt.subplot(412)
figure_2 = ax[1]
# figure_2.axvline(Data.Date[47], color='red',linestyle = "-.")
# figure_2.axvline(Data.Date[58], color='red',linestyle = "-.")

figure_2.plot(X1, Y4, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="aqua", linestyle='-', label='SO$_2$',markersize = 5)
plt.ylabel("SO$_2$(${ug/m^3}$)")  # Y轴标签
figure_2.legend(frameon=False)  # 显示图例
plt.xticks([]) # 不显示x 轴刻度
plt.rcParams['font.sans-serif']=['Microsoft YaHei']
# plt.title("污染后",x = 0.75,y = 2.05)
figure_2.set_ylim(bottom=0,top=70)



ax3 = figure_2.twinx()
ax3.plot(X1, Y5, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="blue", linestyle='-', label='CO',markersize = 5)
ax3.tick_params(axis='y', colors='black')
ax3.spines["right"].set_color("black")  # 修改右边颜色
ax3.legend(bbox_to_anchor=(0.92, 0.99),frameon=False)  # 显示图例
plt.ylabel("CO(${ug/m^3}$)")  # Y轴标签
ax3.set_ylim(0,2.8)
# plt.title("污染时",x = 0.445,y = 2.05)
Y6 = Data.NO2
Y7 = Data.O3
# Data = pd.read_excel(r"G:\长春\长春20220201气象数据.xlsx")
Data = pd.read_excel(r"H:\小论文\小论文\白城空气质量气象数据\白城气象数据\白城2015.10.24-10.28.xlsx")

# X2 = Data.date

# 选择第一幅图
ax3 = plt.subplot(413)
figure_3 = plt.subplot(413)
# 设置y轴的范围
figure_3.set_ylim(bottom=0,top=88)
# figure_3.axvline(Data.date[47], color='red',linestyle = "-.")
# figure_3.axvline(Data.date[58], color='red',linestyle = "-.")
plt.plot(X1, Y6, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="yellow", linestyle='-', label='NO$_2$',markersize = 5)
# figure_1.legend( ncol=1, frameon=False)
ax3.legend( bbox_to_anchor=(0.92, 1), frameon=False)
plt.ylabel("NO$_2$(${ug/m^3}$)")  # Y轴标签
plt.xticks()


ax3 = figure_3.twinx()
ax3.plot(X1, Y7, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="lime", linestyle='-', label='O$_3$',markersize = 5)
ax3.tick_params(axis='y', colors='black')
ax3.spines["right"].set_color("black")  # 修改右边颜色
ax3.legend(bbox_to_anchor=(1, 1),frameon=False)  # 显示图例
plt.ylabel("O$_3$(${ug/m^3}$)")  # Y轴标签
# ax3.set_ylim(0,100)
ax3.set_ylim(bottom=0,top=119)


X3 = Data.date
Y8 = Data.温度
Y9= Data.相对湿度百分比
ax4 = plt.subplot(414)
figure_4 = ax[3]
# figure_4.axvline(Data.date[47], color='red',linestyle = "-.")
# figure_4.axvline(Data.date[58], color='red',linestyle = "-.")

figure_4.plot(X3, Y8, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="blue", linestyle='-', label='温度',markersize = 5)
plt.ylabel("温度(℃)")  # Y轴标签
# plt.xlabel("X轴坐标")  # X轴标签
ax4.legend(bbox_to_anchor=(1, 1),frameon=False)  # 显示图例
plt.rcParams['font.sans-serif']=['Microsoft YaHei']
figure_4.set_ylim(bottom=-8,top=14)
# plt.xticks(np.arange(1,120,24),
#            rotation=45,
#            position=(0,0), #调整年份的位置,让远离了x轴
#            fontsize=9)


ax4 = figure_4.twinx()
ax4.plot(X1, Y9, color='black', marker='o',markeredgecolor="black",linewidth = 0.5,markerfacecolor="red", linestyle='-', label='湿度',markersize = 5)
ax4.tick_params(axis='y', colors='black')
ax4.spines["right"].set_color("black")  # 修改右边颜色
ax4.legend(bbox_to_anchor=(0.92, 1),frameon=False)  # 显示图例
plt.ylabel("湿度(%)")  # Y轴标签
ax4.set_ylim(0,99)


plt.show()
# plt.savefig(r'H:\小论文\小论文\折线图\白城20151026.png',format='png',dpi=500)

Solved the problem of fans smoothly. When drawing the graph, some mistakes were very small, which was really annoying. The last time I encountered a pit when drawing a graph, the lines on the displayed graph were going to the sky. I checked for almost an hour before realizing that the data was not converted into a value.

3. Summary

Hello everyone, I am Pippi. This article mainly takes stock of a Pythonvisualization problem. Aiming at this problem, the article gives specific analysis and code implementation to help fans solve the problem smoothly.

Finally, I would like to thank the fans [巭婑勥烎] for asking questions, thank [E], [Teacher Yuliang], and [kim] for their ideas and code analysis, and thank [Mo Angry] and others for participating in the learning and exchange.

[Supplementary questions] Warm reminder, when you ask questions in the group. You can pay attention to the following points: if it involves large file data, you can desensitize the data, send some demo data (meaning small files), and then paste some code (the kind that can be copied), and remember to send the screenshot of the error report (complete cut ). If there are not many codes, just send the code text directly. If the code exceeds 50 lines, just send a .py file.

52104b26f34900cd61f5cfb66b1cd125.png

If you have any problems during the learning process, please feel free to contact me to solve them (my WeChat: pdcfighting1). At the request of fans, I have created some high-quality Python paid learning exchange groups and paid order receiving groups. Welcome everyone to join me Python learning exchange group and order receiving group!

84766c652b0674d6e38d1bfecf886d0a.png

Friends, hurry up and practice it! If you encounter any problems during the learning process, please add me as a friend, and I will pull you into the Python learning exchange group to discuss learning together.

6dfb986508db55777567b6c3e5a43978.jpeg

------------------- End -------------------

Recommendations for past wonderful articles:

37e0b99f8753929c9a312f613dbf3b95.png

Welcome everyone to like, leave a message, forward, repost, thank you for your company and support

If you want to join the Python learning group, please reply in the background [ join the group ]

Thousands of rivers and thousands of mountains are always in love, can you click [ Looking ]

/Today's Message Topic/

Just say a few words~~

Guess you like

Origin blog.csdn.net/pdcfighting/article/details/131714282