Samual
Samual

Reputation: 552

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.

Upvotes: 3

Views: 5174

Answers (1)

Sheldore
Sheldore

Reputation: 39052

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

Upvotes: 7

Related Questions