0

The original dataframe,df I had was:

 date        name   count
0  2017-08-07  ABC      12
1  2017-08-08  ABC      5
2  2017-08-08  TTT      6
3  2017-08-09  TAC      5
4  2017-08-09  ABC      10

Then the following code was used to transform to the new dataframe,df2 below:

df = pd.DataFrame({"date":["2017-08-07","2017-08-08","2017-08-08","2017-08-09","2017-08-09"],"name":["ABC","ABC","TTT","TAC","ABC"], "count":           ["12","5","6","5","10"]})
df = df.pivot(index='date', columns='name', values='count').reset_index().fillna(0)

Now dataframe,df2 is transformed to:

   date        ABC     TTT    TAC 
0  2017-08-07  12       0      0
1  2017-08-08  5        6      0
2  2017-08-09  10       0      5

Now I tried to plot the dataframe,df2 in order to show each day on the x -axis vs the values in the column names/day on y-axis:( ABC,TTT,TAC ) but I keep getting two straight lines.

Here is the code:

fig=pyplot.figure() 
ax=fig.add_subplot(1,1,1) 
ax.set_title('Plot') 
ax.plot(df2)
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
Bode
  • 527
  • 2
  • 9
  • 19
  • What do you want it to look like? – cs95 Nov 06 '17 at 13:41
  • Thanks! Three different lines showing the of values the columns per day i.e for 2017-08-07 , a line for ABC which shows 12, and also for TTT and TAC representing 0 – Bode Nov 06 '17 at 13:49
  • Let me know if my answer isn't what you're looking for. – cs95 Nov 06 '17 at 13:49
  • Thanks.It is what I am looking for. How do export it for presentation purpose? – Bode Nov 06 '17 at 14:01
  • See here for details: https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib If the answer helped, you could accept it. Thanks. – cs95 Nov 06 '17 at 14:03

1 Answers1

4

Set date as the index and call df.plot:

df

         date  ABC  TTT  TAC
0  2017-08-07   12    0    0
1  2017-08-08    5    6    0
2  2017-08-09   10    0    5

df.set_index('date').plot(subplots=True)
plt.show()

enter image description here


Or, in a single graph:

df.set_index('date').plot()
plt.show()

enter image description here

cs95
  • 379,657
  • 97
  • 704
  • 746