0

I have a data frame with multiple columns (194, 76) and I'm trying to plot multiple columns in a single plot by using Matplotlib.

To plot I use iloc command so I write,

plt.plot(finaldf.iloc[:, 0], finaldf.iloc[:, 51:76])

This does the job but when I try to insert the legends, which is basically the column header I always encounter problems.

For instance, if I use -

plt.plot(finaldf.iloc[:, 0], finaldf.iloc[:, 51:76], label=finaldf.columns.values) 

plt.legend()

This basically includes all the columns name while I want to select only certains columns.

So I tried,

plt.plot(finaldf.iloc[:, 0], finaldf.iloc[:, 51:76], label=finaldf.iloc[0, 51:76]) 

plt.legend()

But this gives me the column name as well as the first value.

Can someone tell me how this would be possible?

Thank you in advance for your help.

Rajam E
  • 7
  • 4
  • I tried that, but the problem is the same, the same label [2:7] appear for each color, instead of assigning for each curve. – Rajam E Feb 11 '21 at 11:13
  • `finaldf.plot(x=finaldf.columns.values[1], y=finaldf.columns.values[2:4])`? – Mr. T Feb 11 '21 at 11:33
  • yes, this works, thank you. Also could you also tell me if I can place the legend outside the plot area? – Rajam E Feb 11 '21 at 11:45
  • You are trying to mix pandas plotting functions with matplotlib. This rarely goes smoothly. Pandas provides convenience wrappers for matplotlib functions. If your desired output is covered by their functionality - excellent. If not, it is often easier to directly use matplotlib than retrospectively trying to adapt the pandas figure. Having said that, [this should work](https://stackoverflow.com/a/65206978/8881141). – Mr. T Feb 11 '21 at 12:01

0 Answers0