0

I have multiple lines plotted on an xy scatter plot

There are more than the number of colours in my palette, which means the colours start cycling.

I have played with using other palettes but the visibility suffers.

An idea I would like to explore now is to add the legend labels at the point where each line intercepts the right-hand y-axis.

Something like this:

enter image description here

Is this possible to achieve with matplotlib and/or seaborn?

Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213

1 Answers1

1

Quick one, with use of the other answer to this question

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

names = ['foo', 'bar', 'foobar']
N_size = 100
fig, ax = plt.subplots(1, 1)
df = pd.DataFrame(map(lambda x: pd.Series(np.cumsum(np.random.rand(N_size) - 0.5), name=x), names)).T
df.plot(ax=ax)
ax2 = ax.twinx()
ax2.set_ylim(ax.get_ylim())
ax2.set_yticks([df[col].iloc[-1] for col in df.columns])
ax2.set_yticklabels(df.columns)
plt.show()

enter image description here

xletmjm
  • 257
  • 1
  • 3
  • Thanks for this. I'm not sure why you were downvoted, because it does exactly what I asked! Perhaps because you didn't include details on what, in particular, achieves the effect I'm looking for? I'm not sure, nut nonetheless, thank you! – Steve Lorimer Feb 20 '20 at 13:46