Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot legend into multiple column outside the plot canvas?

I have a data file which consists of 131 columns and 4 rows. I am plotting it into python as follows

df = pd.read_csv('data.csv')
df.plot(figsize = (15,10))

Once it is plotted, all 131 legends are coming together like a huge tower over the line plots.

Please see the image here, which I have got : Link to Image, I have clipped after v82 for better understanding

I have found some solutions on Stackoverflow (SO) to shift legend anywhere in the plot but I could not find any solution to break this legend tower into multiple small-small pieces and stack them one beside another.

Moreover, I want my plot something look like this

My desired plot :

enter image description here

Any help would be appreciable. Thank you.

like image 560
Samual Avatar asked Feb 17 '26 18:02

Samual


1 Answers

You can specify the position of the legend in relative coordinates using loc and use ncol parameter to split the single legend column into multiple columns. To do so, you need an axis handle returned by the df.plot

df = pd.read_csv('data.csv')
ax = df.plot(figsize = (10,7))
ax.legend(loc=(1.01, 0.01), ncol=4)
plt.tight_layout()

enter image description here

like image 130
Sheldore Avatar answered Feb 19 '26 08:02

Sheldore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!