szczor1
szczor1

Reputation: 191

How to make a line plot from dataframe?

I have a dataframe as below and want to make line plots, where x axis will be time and y axis will be values, so there should be 5 lines (one for each country). Also I want to make 5 different plots, first should just include year 1960 and 5 dots (one for each country), next plot should include years 1960 and 1961 and line connecting values for each country, etc. Last plot should include all years.

Country Name    1960        1961        1962        1963        1964
Pakistan        44.98869    46.065231   47.198878   48.387301   49.627623
Indonesia       87.751068   90.098394   92.518377   95.015297   97.596733
United States   180.671     183.691     186.538     189.242     191.889
India           450.547679  459.642165  469.07719   478.825608  488.848135
China           667.07      660.33      665.77      682.335     698.355

This command makes y what I want, but x axis are indexes, so I thought I might transpose the matrix, but it does not help. Also I would like to know how to make plots as I described above.

ax=newdf.plot( y=names, kind='line')

Upvotes: 1

Views: 341

Answers (1)

kederrac
kederrac

Reputation: 17322

you could use a pivot table:

years = ['1960', '1961','1962', '1963', '1964']
df.pivot_table(values=years, columns='Country Name').plot()

output:

enter image description here

You can manipulate the list years to plot only 2/3/4 years, you just have to let in the list the years that you want to plot

Upvotes: 1

Related Questions