Osm*_*hop 100 python matplotlib
我可以使用左侧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('$')
Run Code Online (Sandbox Code Playgroud)
Pau*_*l H 190
最好的方法是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()
Run Code Online (Sandbox Code Playgroud)

Nim*_*rod 19
对于因为提到 pandas 而偶然发现这篇文章的每个人,您现在有一个非常优雅且直接的选项,可以直接访问pandas 中的secondary_y 轴ax.right_ax
因此,解释一下最初发布的示例,您可以这样写:
table = sql.read_frame(query,connection)
ax = table[[0, 1]].plot(ylim=(0,100), secondary_y=table[1])
ax.set_ylabel('$')
ax.right_ax.set_ylabel('Your second Y-Axis Label goes here!')
Run Code Online (Sandbox Code Playgroud)
kir*_*ril 13
有一个简单的解决方案,没有搞乱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')
Run Code Online (Sandbox Code Playgroud)
基本上,当secondary_y=True给出选项时(尽管ax=ax也传递了),pandas.plot返回一个我们用来设置标签的不同轴.
我知道很久以前就已经回答了这个问题,但我认为这种做法值得.
Hun*_*phu 13
具有很少位置的简单示例:
plot(y1)
plt.gca().twinx().plot(y2, color = 'r') # default color is same as first ax
Run Code Online (Sandbox Code Playgroud)
解释:
ax = plt.gca() # Get current axis
ax2 = ax.twinx() # make twin axis based on x
ax2.plot(...) # ...
Run Code Online (Sandbox Code Playgroud)
我现在无法访问Python,但是我不知道如何:
fig = plt.figure()
axes1 = fig.add_subplot(111)
# set props for left y-axis here
axes2 = axes1.twinx() # mirror them
axes2.set_ylabel(...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152895 次 |
| 最近记录: |