Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plot a Pandas Pivoted table using python

Iam trying to produce a line plot for the following table such that:

  1. X- axis is the dates[shown in the columns]
  2. Y-axis is the number value for each Region/Date
  3. Chart legend would be the Region[index]. [ARABIAN GULF/BALTIC SEA …]

Hence, there would be total of 3 line plots, one for each Region, where x-axis is the dates.

Here is the table:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Here is the Code:

x={'REGION': {0: 'ANDAMAN SEA', 1: 'ARABIAN GULF', 2: 'BALTIC SEA'}, '2022-08-29': {0: 13, 1: 28, 2: 121}, '2022-09-05': {0: 13, 1: 24, 2: 120}, '2022-09-12': {0: 12, 1: 26, 2: 114}, '2022-09-19': {0: 18, 1: 55, 2: 105}, '2022-09-26': {0: 20, 1: 36, 2: 113}, '2022-10-03': {0: 19, 1: 25, 2: 116}, '2022-10-10': {0: 19, 1: 70, 2: 114}, '2022-10-17': {0: 23, 1: 95, 2: 113}}

df=pd.DataFrame(x)

df.plot()

plt.show()

Using the above code im getting the following INCORRECT plot:

enter image description here

>Solution :

I think you want:

df.set_index('REGION').T.plot()

Output:

enter image description here

Intermediate:

df.set_index('REGION').T

REGION      ANDAMAN SEA  ARABIAN GULF  BALTIC SEA
2022-08-29           13            28         121
2022-09-05           13            24         120
2022-09-12           12            26         114
2022-09-19           18            55         105
2022-09-26           20            36         113
2022-10-03           19            25         116
2022-10-10           19            70         114
2022-10-17           23            95         113
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading