I have two dataframes (data_frame_EHGP
and data_frame_P
) and I am plotting particular columns from each dataframe, everything in the same plot.
Considering the way this script is written since I am plotting multiple columns, for each data frame column I am getting multiple legends that are named the same.
Hence, if I am plotting 8 columns [0:7] from data_frame_EHGP and 200 columns from data_frame_P, I am getting legend printed out 8 200 times.
However, I only need two legends, one for each dataframe ("ehgp" and "P"), for each dataframe, does anyone knows how to do this in python?
figure, ax1 = plt.subplots()
ax1.plot(data_frame_EHGP[data_frame_EHGP.columns[0:7]],linewidth=1.5,zorder=1, label = "Ehgp", color="blue")
ax1.plot(data_frame_P[data_frame_P.columns[0:199]],linewidth=1,zorder=1, label = "P", color="yellow")
Below is an example of the plot of what the current script does.
The image can be found on this link
Data_frame_EHGP looks like:
ehgp1 ehgp2 ehgp3 ehgp4 ehgp5 ehgp6 ehgp7 ehgp8 XXX
1 30.965454 31.433349 31.482033 31.689665 33.065981 31.987559 33.408349 32.527560 1
2 33.319992 28.847623 29.991308 28.278939 30.812096 32.167360 30.207887 29.110781 2
3 27.356627 28.016364 30.542680 29.993470 25.756890 27.512679 30.443207 32.031628 3
4 23.705713 25.333608 32.280713 31.634398 24.919924 29.810451 32.001503 26.082818 4
5 28.828271 30.459324 26.619850 22.957481 28.356956 34.134850 27.756692 26.343534 5
... ... ... ... ... ... ... ... ... ...
96 9.840532 9.367901 13.405007 9.212901 -8.923153 11.692112 10.067901 14.210006 96
97 12.915509 9.823141 11.148668 12.318141 -11.677649 10.089457 10.269456 11.762878 97
98 11.065410 14.490674 13.061462 17.482779 -12.301433 11.971989 14.136989 14.175410 98
99 13.826624 17.742150 9.436623 14.984782 -14.872851 12.721097 16.495571 12.690045 99
100 13.942704 15.877967 14.007177 16.033756 -14.451508 11.845861 14.470861 13.294019 100
100 rows × 9 columns
Data_frame_P looks like:
1 2 3 4 5 6 7 8 9 10 ... 192 193 194 195 196 197 198 199 200 XX
1 20.752032 17.214033 20.302032 17.978034 19.380034 18.332033 19.402032 16.400032 20.128034 23.466032 ... -16.551969 -19.117968 -19.605969 -22.445969 -20.027969 -17.903970 -17.321969 -19.077969 -20.423969 1
2 21.503306 17.041307 23.497308 21.215307 21.333306 17.163307 18.467308 18.175307 21.843307 24.061306 ... -15.122694 -23.030695 -19.664695 -22.506695 -23.582695 -18.934695 -15.450695 -19.122695 -20.678695 2
3 20.386994 17.658996 17.042996 21.590996 20.998994 14.058995 21.136996 15.312994 18.652994 22.478997 ... -14.725006 -23.575007 -19.483007 -27.095007 -22.103007 -15.343006 -17.277006 -22.949007 -22.683006 3
4 22.158765 20.308766 21.002767 23.750766 22.440766 18.982765 22.838764 18.824765 22.724765 22.424765 ... -13.441236 -20.723237 -19.165237 -23.101237 -25.441237 -16.459236 -14.917236 -19.773237 -24.341236 4
5 26.705375 18.711377 19.263375 22.919375 25.139375 18.381375 24.049377 17.477378 21.927377 23.665376 ... -14.344625 -21.994626 -14.216626 -23.820626 -25.922626 -16.102626 -15.236626 -21.174625 -21.314627 5
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
96 24.688743 14.496743 20.840744 20.830742 21.262743 22.444745 23.212745 20.756743 19.688743 23.150746 ... -22.777258 -24.005259 -23.477259 -21.527258 -22.073258 -25.427259 -20.115259 -18.627258 -17.843258 96
97 26.707827 21.461824 21.787825 23.241826 24.829827 20.645826 22.425824 22.789827 18.131826 19.291826 ... -22.628177 -24.978176 -19.260176 -22.386177 -23.166176 -20.462176 -20.732176 -18.514176 -16.210176 97
98 23.157565 18.359568 17.527569 23.669570 22.339569 20.177569 26.799568 20.569567 17.489569 22.203569 ... -23.004434 -24.984434 -20.528433 -23.332434 -24.332434 -21.524434 -23.466434 -18.642433 -14.750434 98
99 23.090204 19.956203 17.970203 20.172202 19.236204 17.598203 22.784205 18.728203 15.854203 20.784202 ... -24.207799 -24.209799 -21.853799 -19.377799 -23.559799 -19.995799 -23.305799 -16.113799 -13.083799 99
100 24.470229 20.590229 21.172231 20.026229 19.946229 18.876229 22.534232 24.462230 15.550228 20.956229 ... -19.001773 -24.141772 -21.431771 -17.037772 -25.311773 -13.935772 -23.385772 -20.547771 -18.421771 100
100 rows × 201 columns
CodePudding user response:
Try this:
figure, ax1 = plt.subplots()
l1 = ax1.plot(data_frame_EHGP.iloc[:, 0:7], linewidth=1.5, zorder=1, label="Ehgp", color="blue")
l2 = ax1.plot(data_frame_P.iloc[:, 0:199], linewidth=1, zorder=1, label="P", color="yellow")
ax1.legend(handles=[l1[0], l2[0]])
More context
You are plotting multiple lines and assigning single label / color to all of them in single ax1.plot()
statement. If you take a closer look ax1.plot()
statement returns list with all the line plots which you selected from given dataframe. It looks like this:
[<matplotlib.lines.Line2D at 0x136da4e50>,
<matplotlib.lines.Line2D at 0x136da4fa0>,
<matplotlib.lines.Line2D at 0x136daf100>,
<matplotlib.lines.Line2D at 0x136daf220>,
<matplotlib.lines.Line2D at 0x136daf340>,
<matplotlib.lines.Line2D at 0x136daf460>,
<matplotlib.lines.Line2D at 0x136daf580>]
If you want to have all these lines to have the same color, label and only one legend item, you can plot legend to only one of them (e.g. first) which is what I did in: ax1.legend(handles=[l1[0], l2[0]])