admin管理员组文章数量:1279017
My goal is to plot two sets of data against two different y axes, at the left and right spines, with two different colors. In addition, I want the following elements colored with the same two colors on each side:
- left and right vertical spine
- ticks (major and minor)
- tick labels
I was unable to achieve this after searching for a while.
It is easy to plot the data points correctly, either using the secondary-y
option in pandas.plot
, or by defining a secondary y axis with Matplotlib's twinx
.
Let me give a complete code example.
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
df = pd.DataFrame(data={'Series A':[1,1.25,1.5,1.75,2],'Series B':[11,10.75,10.5,10.25,10]})
fig1,ax = plt.subplots()
# Plot Series A on ax
df['Series A'].plot(ax=ax,style='o-',lw=0.5,legend=False,color='b',ylim=(0,3));
ax.set_xlabel('x')
ax.set_ylabel('Y values')
# Generate secondary axis
ax2 = ax.twinx()
# Plot Series B on ax2
df['Series B'].plot(ax=ax2,style='^-',legend=False,lw=0.5,color='g',ylim=(9,12));
plt.show();
At this point, my plot has the two sets of data plotted correctly. Now on to colorizing. I add the following two lines:
ax.tick_params(axis='y', colors='b',which='both')
ax2.tick_params(axis='y', colors='g',which='both')
And this gets almost what I want: ticks and tick labels are colorized. But the spines aren't.
I tried ax.spines['left'].set_color('orange')
but that does not work. Why?
So I have two questions:
- what is the accessor for the secondary axis parameters (for example, if I want to change ticks, etc.)?
- how can I get the spines colorized?
My goal is to plot two sets of data against two different y axes, at the left and right spines, with two different colors. In addition, I want the following elements colored with the same two colors on each side:
- left and right vertical spine
- ticks (major and minor)
- tick labels
I was unable to achieve this after searching for a while.
It is easy to plot the data points correctly, either using the secondary-y
option in pandas.plot
, or by defining a secondary y axis with Matplotlib's twinx
.
Let me give a complete code example.
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
df = pd.DataFrame(data={'Series A':[1,1.25,1.5,1.75,2],'Series B':[11,10.75,10.5,10.25,10]})
fig1,ax = plt.subplots()
# Plot Series A on ax
df['Series A'].plot(ax=ax,style='o-',lw=0.5,legend=False,color='b',ylim=(0,3));
ax.set_xlabel('x')
ax.set_ylabel('Y values')
# Generate secondary axis
ax2 = ax.twinx()
# Plot Series B on ax2
df['Series B'].plot(ax=ax2,style='^-',legend=False,lw=0.5,color='g',ylim=(9,12));
plt.show();
At this point, my plot has the two sets of data plotted correctly. Now on to colorizing. I add the following two lines:
ax.tick_params(axis='y', colors='b',which='both')
ax2.tick_params(axis='y', colors='g',which='both')
And this gets almost what I want: ticks and tick labels are colorized. But the spines aren't.
I tried ax.spines['left'].set_color('orange')
but that does not work. Why?
So I have two questions:
- what is the accessor for the secondary axis parameters (for example, if I want to change ticks, etc.)?
- how can I get the spines colorized?
1 Answer
Reset to default 2As for spines - I think ax
is behind ax2
and it is invisible and you have to use ax2
(at least this works for me)
ax2.spines['left'].set_color('blue')
ax2.spines['right'].set_color('green')
本文标签: pythonHow to access secondary axis properties in MatplotlibStack Overflow
版权声明:本文标题:python - How to access secondary axis properties in Matplotlib? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741296504a2370849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
does not work
? Do you get error message? We don't have example data to run code and see it - so you have describe all details. Or better add some example data in code so we could simply copy and run it. – furas Commented Feb 24 at 3:10df
has been added. Does not work means the spines remain black. – germ Commented Feb 24 at 4:17