I have multiple dataframes which I have read from an excel sheet as follows -
A = pd.read_excel("sample.xlsx", usecols="A:B", sheet_name="A")
B = pd.read_excel("sample.xlsx", usecols="A:B", sheet_name="B")
...
...
Each dataframe has their own unique x, and y values and I am able to make a combined plot using the following code -
ax.plot(A.iloc[:, 0], A.iloc[:, 1], color=colors(0), label='A')
ax.plot(B.iloc[:, 0], B.iloc[:, 1], color=colors(1), label='B')
...
...
I now want to retain this original plot in an initial figure, but also additionally make multiple plots using the same data such that I am able to group some of these plots into a group and assign a common label for them. A very rudimentary and brutal code I can think of for it is as follows -
ax.plot(A.iloc[:, 0], A.iloc[:, 1], color=colors(0), label='A, B')
ax.plot(B.iloc[:, 0], B.iloc[:, 1], color=colors(0), label='A, B')
...
...
However, this has a drawback. If I do this, I will have two legend entries with the labels [A, B]. In addition, I have close to 50 such data frames which I have extracted from multiple sources and it will be a cumbersome process to constantly change all colors and labels for these plots.
Is there a way I can group these dataframes as I wish and label them as a grouped entity? I am imagining something like the following -
ax.plot(A.iloc[:, 0], A.iloc[:, 1], color=colors(0), label='A')
ax.plot(B.iloc[:, 0], B.iloc[:, 1], color=colors(1), label='B')
...
...
set1 = [A, B, C]
set2 = [G, E, F]
ax.legend((set1), ('Legend for Set1'), color=colors(0))
ax.legend((set2), ('Legend for Set2'), color=colors(2))
These groupings will also change such as, in subsequent plots, I will want to group (A, C, E) together, for example. Is there an easy way to achieve this in Python and Matplotlib? I am new to this community, so please let me know if I should frame my questions differently next time.
Ps - I am also fine if I have to manually place my groups in individual labels like
ax.legend((A, B, C), ('Legend for Set1'), color=colors(0))
question from:
https://stackoverflow.com/questions/65864819/how-to-iteratively-plot-custom-groups-of-dataframes