在matplotlib中添加y轴标签到次要y轴。

时间:2022-10-19 16:32:01

I can add a y label to the left y-axis using plt.ylabel, but how can I add it to the secondary y-axis?

我可以使用plt在左边的y轴上添加一个y标签。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('$')

3 个解决方案

#1


111  

The best way is to interact with the axes object directly

最好的方法是直接与坐标轴对象交互。

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中添加y轴标签到次要y轴。

#2


5  

I don't have access to Python right now, but off the top of my head:

我现在无法访问Python,但我的脑海中有:

fig = plt.figure()

axes1 = fig.add_subplot(111)
# set props for left y-axis here

axes2 = axes1.twinx()   # mirror them
axes2.set_ylabel(...)

#3


4  

There is a straightforward solution without messing with matplotlib: just pandas.

有一种直截了当的解决方案,而不影响matplotlib:只是熊猫。

Tweaking the original example:

调整原来的例子:

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')

Basically, when the secondary_y=True option is given (eventhough ax=ax is passed too) pandas.plot returns a different axes which we use to set the labels.

基本上,当给出第二个选项时(即使ax=ax =也通过了),熊猫。plot返回一个不同的轴,我们用来设置标签。

I know this was answered long ago, but I think this approach worths it.

我知道这是很久以前的回答,但我认为这种方法是值得的。

#1


111  

The best way is to interact with the axes object directly

最好的方法是直接与坐标轴对象交互。

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中添加y轴标签到次要y轴。

#2


5  

I don't have access to Python right now, but off the top of my head:

我现在无法访问Python,但我的脑海中有:

fig = plt.figure()

axes1 = fig.add_subplot(111)
# set props for left y-axis here

axes2 = axes1.twinx()   # mirror them
axes2.set_ylabel(...)

#3


4  

There is a straightforward solution without messing with matplotlib: just pandas.

有一种直截了当的解决方案,而不影响matplotlib:只是熊猫。

Tweaking the original example:

调整原来的例子:

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')

Basically, when the secondary_y=True option is given (eventhough ax=ax is passed too) pandas.plot returns a different axes which we use to set the labels.

基本上,当给出第二个选项时(即使ax=ax =也通过了),熊猫。plot返回一个不同的轴,我们用来设置标签。

I know this was answered long ago, but I think this approach worths it.

我知道这是很久以前的回答,但我认为这种方法是值得的。