Python lee datos de Excel y dibuja múltiples imágenes del eje Y

 Hay muchas formas de agregar el eje y en matplotlib. Este artículo usa ax.twinx() para agregar múltiples ejes y. Es un comando compuesto de suma del eje y de uso común y no limita el número de superposiciones.


explicación de set_zorder()

ax1.set_zorder (valor) se utiliza para controlar el orden de dibujo. Cuanto mayor sea el valor, más arriba se dibujará la capa.


  • Primero crea una nueva coordenada.
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(4,2),dpi=500)
ax1=fig.add_axes([0,0,0.7,1])
Nuevas coordenadas
Nuevas coordenadas

  • 1. Agregue múltiples ejes y

  • import matplotlib.pyplot as plt
    fig=plt.figure(figsize=(4,2),dpi=500)
    ax1=fig.add_axes([0,0,0.7,1])
    # 增加第2个y轴
    ax2=ax1.twinx()
    # 增加第3、4个y轴,并调整第3、4y轴向右移动40、80
    ax3=ax1.twinx()
    ax4=ax1.twinx()
    ax3.spines['right'].set_position(('outward',25))
    ax4.spines['right'].set_position(('outward',50))

código completo 

# -*- coding: utf-8 -*-
"""
Created on Wed May 10 17:23:08 2023

@author: ypzhao
"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.spines as sp
import matplotlib.path as mpath
import pandas as pd

excel = pd.read_excel("C:/Users/ypzhao/Desktop/train/电机数据/电机参数.xlsx")
data = pd.DataFrame(excel)

t = data['时间']
T1 = data['转矩1']
S1 = data['转速1']
P1 = data['功率1']

x=np.arange(0,12,1)
degree=np.array([-2.3,5,17.1,28,15,2,8,5,10.18,23,25,4])
# T2 = data['转矩2']
# S2 = data['转速2']
# P2 = data['功率2']

plt.rcParams['font.sans-serif']=['Times New Roman']
plt.rcParams['axes.unicode_minus']=False
fig=plt.figure(figsize=(14,6),dpi=300)
font = {'family': 'Times New Roman','size': 24}

ax1=fig.add_axes([0,0,0.6,1])
# 增加第2个y轴
ax2=ax1.twinx()
# 增加第3、4个y轴,并调整第3、4y轴向右移动40、80
ax3=ax1.twinx()
ax4=ax1.twinx()

ax3.spines['right'].set_position(('outward',80))
ax4.spines['right'].set_position(('outward',160))

ax1.set_xlabel('Time/s',font,color='red')
ax1.set_ylabel('Temperature(℃)',font,color='tab:red')
ax2.set_ylabel('Torque/N·m',font,color='tab:green')
ax3.set_ylabel('r/min',font,color='tab:blue')
ax4.set_ylabel('Power/kW',font,color='tab:orange')

# 设置左边轴的颜色为红色
ax1.spines['left'].set_color('tab:red')
# zorder用来控制绘图顺序,值越大,绘制的时间越靠后
ax1.set_zorder(5)

ax1.patch.set_visible(False)
ax1.tick_params(axis='y',labelcolor='tab:red',color='tab:red',labelsize=8)

ax1.spines['right'].set_color('tab:green')

# 设置循环调整坐标轴颜色
for ax,c in zip([ax1,ax2,ax3,ax4],['red','tab:green','tab:blue','tab:orange']):
    ax.tick_params(labelcolor=c,color=c,labelsize=22)
    ax.spines['right'].set_color(c)
    
# 调整绘图格式紧凑
fig.tight_layout() 
#设置x,y轴及增加的y轴的坐标范围 
ax1.set_xlim(-20,2030)
ax1.set_ylim(-10,30) 
ax2.set_ylim(-230,800)
ax3.set_ylim(-10,3600)
ax4.set_ylim(-300,120)

ax1.plot(x,degree,c='tab:red',alpha=.9,linewidth=1)
ax2.plot(t,T1,c='tab:green',alpha=.9,linewidth=1.5)
ax3.plot(t,S1,color='tab:blue',alpha=.9,linewidth=1.5)
ax4.plot(t,P1,color='tab:orange',alpha=.9,linewidth=1.5)

# 保存完整的图片
plt.savefig('转矩转速功率温度图.jpg',dpi=1200, bbox_inches = 'tight') #保存为图片png格式
plt.show()

 resultado de la operación

 

Supongo que te gusta

Origin blog.csdn.net/m0_58857684/article/details/130607647
Recomendado
Clasificación