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?
Share Improve this question edited Feb 24 at 4:16 germ asked Feb 24 at 2:46 germgerm 1,6891 gold badge19 silver badges19 bronze badges 3
  • Could you define df in the script? – Subir Chowdhury Commented Feb 24 at 2:53
  • what means 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:10
  • Sorry, definition of df has been added. Does not work means the spines remain black. – germ Commented Feb 24 at 4:17
Add a comment  | 

1 Answer 1

Reset to default 2

As 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