0

I am creating a simple line plot in Python where I want to plot three different data points on the same graph. data1 will be represented by values on the left side of the y-axis, where data2 and data3 will be represented by the same scale of values on the right side of the y-axis. This is fairly simple to do below:

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plt(x,data1,'r')
ax2.plt(x,data2,'g')
ax2.plt(x,data3,'b')

plt.show()

Now if I want to set the labels on each side I can do:

ax1.set_ylabel('data1',color='r')
ax2.set_ylabel('data2\ndata3',color='g')

The problem with the above set of code is that the text of both 'data2' and 'data3' are colored green. How do I make it so the text of 'data2' is green and the text of 'data3' is blue?

WX_M
  • 458
  • 4
  • 20
  • 2
    The principle is explained in the [parasite demo](https://matplotlib.org/3.1.1/gallery/ticks_and_spines/multiple_yaxis_with_spines.html). – Mr. T Jan 08 '21 at 21:06
  • 1
    It seems it is quite fashionable to simply copy the matplotlib example into an answer. [Matplotlib: Colour multiple twinx() axes](https://stackoverflow.com/questions/20356982/matplotlib-colour-multiple-twinx-axes) – Mr. T Jan 08 '21 at 23:43

0 Answers0