双胞胎杀死滴答作响标签颜色 [英] twinx kills tick label color
问题描述
我用两个y轴绘制一个双点图。第二个轴 ax2
由 twinx
创建。问题是,通过 yticks
的第二个y轴的着色不再工作了。相反,我必须 set_color
单独的标签。这里是相关代码:
I am plotting a double plot with two y-axes. The second axis ax2
is created by twinx
. The problem is that the coloring of the second y-axis via yticks
is not working anymore. Instead I have to set_color
the labels individually. Here is the relevant code:
fig = plt.figure()
fill_between(data[:,0], 0, (data[:,2]), color='yellow')
yticks(arange(0.2,1.2,0.2), ['.2', '.4', '.6', '.8', ' 1'], color='yellow')
ax2 = twinx()
ax2.plot(data[:,0], (data[:,1]), 'green')
yticks(arange(0.1,0.6,0.1), ['.1 ', '.2', '.3', '.4', '.5'], color='green')
# color='green' has no effect here ?!
# instead this is needed:
for t in ax2.yaxis.get_ticklabels(): t.set_color('green')
show()
导致:
此问题只有在我设置刻度字符串时才会发生。
This issue only occurs if I set the tick strings.
yticks(arange(0.1,0.6,0.1), ['.1 ', '.2', '.3', '.4', '.5'], color='green')
省略,像这里
yticks(arange(0.1,0.6,0.1), color='green')
并且着色工作正常。
这是一个错误到这个),一个功能(?!)或
我缺少这里的东西?我在ubuntu上使用python 2.6.5和matplotlib 0.99.1.1。
Is that a bug (could not find any reports to this), a feature (?!) or am I missing something here? I am using python 2.6.5 with matplotlib 0.99.1.1 on ubuntu.
推荐答案
它的价值,你的代码工作正常在我的系统,即使没有为
循环设置标签颜色。作为参考,这里有一个独立的例子,试图基本上完全遵循你发布的:
For whatever it's worth, you code works fine on my system even without the for
loop to set the label colors. Just as a reference, here's a stand-alone example trying to follow essentially exactly what you posted:
import matplotlib.pyplot as plt
import numpy as np
# Generate some data
num = 200
x = np.linspace(501, 1200, num)
yellow_data, green_data = np.random.random((2,num))
green_data -= np.linspace(0, 3, yellow_data.size)
# Plot the yellow data
plt.fill_between(x, yellow_data, 0, color='yellow')
plt.yticks([0.0, 0.5, 1.0], color='yellow')
# Plot the green data
ax2 = plt.twinx()
ax2.plot(x, green_data, 'g-')
plt.yticks([-4, -3, -2, -1, 0, 1], color='green')
plt.show()
我的猜测是你的问题主要来自于混合对不同对象的引用。我猜你的代码有点复杂,当你调用 plt.yticks
, ax2
是而不是当前轴。您可以在调用之前显式调用
,看看是否改变了事情。 sca(ax2)
(将当前轴设置为 ax2
), c $ c> yticks
My guess is that your problem is mostly coming from mixing up references to different objects. I'm guessing that your code is a bit more complex, and that when you call plt.yticks
, ax2
is not the current axis. You can test that idea by explicitly calling sca(ax2)
(set the current axis to ax2
) before calling yticks
and see if that changes things.
一般来说,最好坚持使用完全的matlab-ish状态机接口或OO接口,不要混用太多。 (个人而言,我更喜欢只是坚持使用OO接口。使用 pyplot
设置图形对象, show
Generally speaking, it's best to stick to either entirely the matlab-ish state machine interface or the OO interface, and don't mix them too much. (Personally, I prefer just sticking to the OO interface. Use pyplot
to set up figure objects and for show
, and use the axes methods otherwise. To each his own, though.)
无论如何,使用matplotlib> = 1.0, tick_params
函数使这一点更方便。 (我也使用 plt.subplots
这里,也只有在= 1.0)。
At any rate, with matplotlib >= 1.0, the tick_params
function makes this a bit more convenient. (I'm also using plt.subplots
here, which is only in >= 1.0, as well.)
import matplotlib.pyplot as plt
import numpy as np
# Generate some data
yellow_data, green_data = np.random.random((2,2000))
yellow_data += np.linspace(0, 3, yellow_data.size)
green_data -= np.linspace(0, 3, yellow_data.size)
# Plot the data
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(yellow_data, 'y-')
ax2.plot(green_data, 'g-')
# Change the axis colors...
ax1.tick_params(axis='y', labelcolor='yellow')
ax2.tick_params(axis='y', labelcolor='green')
plt.show()
>
较旧版本的matplotlib的等效代码看起来更像这样:
The equivalent code for older versions of matplotlib would look more like this:
import matplotlib.pyplot as plt
import numpy as np
# Generate some data
yellow_data, green_data = np.random.random((2,2000))
yellow_data += np.linspace(0, 3, yellow_data.size)
green_data -= np.linspace(0, 3, yellow_data.size)
# Plot the data
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax2 = ax1.twinx()
ax1.plot(yellow_data, 'y-')
ax2.plot(green_data, 'g-')
# Change the axis colors...
for ax, color in zip([ax1, ax2], ['yellow', 'green']):
for label in ax.yaxis.get_ticklabels():
label.set_color(color)
plt.show()
这篇关于双胞胎杀死滴答作响标签颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!