1

I have a dataframe like this

timestamp|type|value_1|value_2

t1|A|v1|v2

t2|B|v3|v4

t3|C|v5|v6

t4|A|v7|v8

I would like to plot a graph with 6 lines each type and value for example

type A - value_1
type A - value_2

type B - value_1
type B - value_2

type C - value_1
type C - value_2

thanks,

it is like doing this

A = df[df["type"] == A]

A.plot(x="time", y=["value_1", "value_2"])

do this for three types and combine those 6 lines on the same graph

cynric4sure
  • 189
  • 1
  • 12

2 Answers2

2

I think you can reshape DataFrame to columns and then plot:

df['g'] = df.groupby('type').cumcount()
df = df.set_index(['timestamp','g', 'type']).unstack().reset_index(level=1, drop=True)
df.columns = df.columns.map('_'.join)

df.plot()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

As far as the plotting goes I recommend you check out: MatPlotLib: Multiple datasets on the same scatter plot and Multiple data set plotting with matplotlib.pyplot.plot_date , as well as this tutorial.

For the selection of data to plot I recommend the section "selection by label" in the pandas docs. I suppose you could store the values from your corresponding columns / rows in some temporary variables x1 - xn and y1 - yn and then just plot all the pairs, which could look something like:

xs = sheet.loc[<appropriate labels>]
ys = sheet.loc[<appropriate labels>]
for i in range(len(xs)):
   plt.plot(xs[i],ys[i],<further arguments>)
plt.show()

In your case, just accessing the 'values' label might not be sufficient, as only every n'th element of that column seams to belong to any given type. inthis question you can see how you can get a new list with only the appropriate values inside. Basically something like:

allXs = sheet.loc['v1']
xsTypeA = allXs[1::4]
...

hope that helps.

Community
  • 1
  • 1
Chris
  • 710
  • 7
  • 15