English 中文(简体)
杀双胞胎
原标题:twinx kills tick label color

我正在策划两艘游艇。 第二个轴心是ax2。 问题是,通过<条码>标准对二轴的颜色不再奏效。 相反,我必须逐个贴上“条码>。 相关法典:

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

Resulting in:

“colored_etslabels”/

只有当我确定标准指示时,这个问题才会发生。

yticks(arange(0.1,0.6,0.1), [ .1  ,  .2 ,  .3 ,  .4 ,  .5 ], color= green )

Omit it, like here

yticks(arange(0.1,0.6,0.1), color= green )

彩色作品罚款。

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.

问题回答

不管其价值如何,你甚至在没有<<<><>>>>条码>的情况下,就在我的系统中打上了记号。 作为参考,这里有一个独立的例子,试图基本上遵循你所表述的内容:

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

“entergraph

我的猜测是,你的问题主要来自对不同物体的混淆。 我猜测,你的代码比较复杂,当你打电话<编码>plt.yrites时,ax2不是目前的轴心。 您可在打电话<条码>(<>yps>>之前,明确打上<>sca(ax2)(目前轴向ax2),并看到这一改动。

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

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

“entergraph

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




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签