ฉันสามารถเพิ่มป้ายกำกับ ay ให้กับแกน y ด้านซ้ายโดยใช้plt.ylabel
แต่จะเพิ่มลงในแกน y รองได้อย่างไร
table = sql.read_frame(query,connection)
table[0].plot(color=colors[0],ylim=(0,100))
table[1].plot(secondary_y=True,color=colors[1])
plt.ylabel('$')
ฉันสามารถเพิ่มป้ายกำกับ ay ให้กับแกน y ด้านซ้ายโดยใช้plt.ylabel
แต่จะเพิ่มลงในแกน y รองได้อย่างไร
table = sql.read_frame(query,connection)
table[0].plot(color=colors[0],ylim=(0,100))
table[1].plot(secondary_y=True,color=colors[1])
plt.ylabel('$')
คำตอบ:
วิธีที่ดีที่สุดคือโต้ตอบกับaxes
วัตถุโดยตรง
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 *y1
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')
ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.set_ylabel('Y2 data', color='b')
plt.show()
มีวิธีแก้ปัญหาที่ตรงไปตรงมาโดยไม่ต้องยุ่งกับ matplotlib: แค่แพนด้า
ปรับแต่งตัวอย่างเดิม:
table = sql.read_frame(query,connection)
ax = table[0].plot(color=colors[0],ylim=(0,100))
ax2 = table[1].plot(secondary_y=True,color=colors[1], ax=ax)
ax.set_ylabel('Left axes label')
ax2.set_ylabel('Right axes label')
โดยทั่วไปเมื่อมีการกำหนดsecondary_y=True
ตัวเลือก (แม้ว่าจะax=ax
ถูกส่งผ่านไปด้วย) pandas.plot
จะส่งกลับแกนอื่นที่เราใช้ในการตั้งค่าป้ายกำกับ
ฉันรู้ว่าคำตอบนี้นานมาแล้ว แต่ฉันคิดว่าแนวทางนี้คุ้มค่า
ฉันไม่สามารถเข้าถึง Python ได้ในตอนนี้ แต่จากด้านบนของหัว:
fig = plt.figure()
axes1 = fig.add_subplot(111)
# set props for left y-axis here
axes2 = axes1.twinx() # mirror them
axes2.set_ylabel(...)