1

I need to create a plot using pyplot where I can color markers based on the first index level in the DataFrame (df). Example: All values under: A -- Red, B -- Green, C -- Blue I'm plotting a1, a2, a3, a4, a5 against cat2 values. Every value should be on the same plot.

I can do it using subplots by using cross section:

f, a = plt.subplots(3,1)
df.xs('A').plot(ax=a[0])
df.xs('B').plot(ax=a[1])
df.xs('C').plot(ax=a[2])

Subplot lets me color different xs, but I need a way to do it on the same plot. Link is an image of my dataframe in csv

dkisiale
  • 35
  • 4

1 Answers1

0

I guess something like this would work.

f, a = plt.subplots(1, 1)
# Only create one subplot as all the data is meant to go on one plot only
df.xs('A').plot(ax=a, marker='D', markersize=6, markerfacecolor='red',
                markeredgecolor='red')
df.xs('B').plot(ax=a, marker='D', markersize=6, markerfacecolor='green',
                markeredgecolor='green')
df.xs('C').plot(ax=a, marker='D', markersize=6, markerfacecolor='blue',
                markeredgecolor='blue')
Patol75
  • 4,342
  • 1
  • 17
  • 28
  • Thanks, I was able top get them on the same plot.... But this method plots the data points on top of one another. Is there anyway I can plot it out sequentially on the same plot, preferably without breaks? Such as: A data (red)--> B data (green) ---> C data (blue) – dkisiale May 06 '19 at 16:37
  • It is pretty hard to understand what you want without a visual. – Patol75 May 07 '19 at 00:16
  • How would I shift df.xs('B').plot datapoints to the left on x-axis by 3. I can use the same method to do section C. This will achieve what I need. – dkisiale May 07 '19 at 16:19
  • You need to provide the keywords x and y to the plot function. Have a look at this answer for example (https://stackoverflow.com/a/43063196/10640534). – Patol75 May 08 '19 at 00:38