0

I need to plot a line plot with multiple lines.

Due to a large number of lines, the legend gets so large that it hides some of the lines, is there a way to automatically set the y ticks so that there will be enough room in the plot for the legend to fit properly?

Example plot:

Example plot

Thank you.

Hadarsi320
  • 343
  • 1
  • 5
  • 10
  • 2
    I would just plot the legend outside the plot area, something like `ax.legend(loc=[1,0.5])` – Quang Hoang Oct 20 '20 at 18:15
  • you could also add another, smaller, axes object using gridspec to give you more space to add the legend – Andrew Oct 20 '20 at 18:19
  • You could also try `ax.set_ylim(0, 4.5)` – Andrew Oct 20 '20 at 18:22
  • Just place the legend outside. See [How to put the legend out of the plot](https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot/43439132#43439132). – JohanC Oct 20 '20 at 18:49

1 Answers1

1

You can adjust the xlim and the dict prop, for example

x = np.linspace(1,10,10)
y = x + np.random.rand(10,10)
labels = list('abcdefghij')

fig,ax = plt.subplots(figsize=(12,6))
ax.plot(x,y,'-o')

ax.set_xlim(1,11)
ax.legend(labels,loc='upper right', prop={'size': 10})

plot

Tuning ax.set_xlim to leave enough space for the legend, and the size of the legend is controlled by prop={'size':10}

meTchaikovsky
  • 7,478
  • 2
  • 15
  • 34