1

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')
3
  • What is the exact problem? df.plot(y=df.columns, kind='line') should give you the plot you want if I understood your question correctly. Commented Mar 17, 2020 at 21:11
  • I tried it, it puts on x axis Country Names, not time. Commented Mar 17, 2020 at 21:27
  • What would you like on the x-axis? Please describe the resulting plot more precisely. Commented Mar 17, 2020 at 21:37

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.