1

I have a dataset of corona virus which looks something like this

day month cases deaths country
3     5     10    1     USA
4     5     12    2     USA

it has multiple entries for each country for different days, I made it filter out so it only gets the data from 28th of May now I want to make a graph for every country with how many cases they have use pandas and matploitlib

However it's not working properly

when I use

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel("covid.xlsx")
print(df.columns.tolist())
df.loc[(df.day == 28) & (df.month==5), :].plot.line(x='countriesAndTerritories', y='deaths')
plt.show()

The code executes but only shows 5 countries and the countriesAndTerritories is on the x axis, when I switch around x='countriesAndTerritories', y='deaths' I get TypeError: no numeric data to plot

This is a reference of what I want: https://qap.ecdc.europa.eu/public/single/?appid=f818d019-18c5-41e0-99e6-bd2b7f6f17b5&obj=3d471628-9a6e-4938-95da-0cba933925ca&opt=nointeraction&select=clearall (europe's data visualization)

1
  • 2
    Do you want to reproduce the same plot as in the figure you have linked? Commented May 29, 2020 at 7:18

2 Answers 2

1

You can reproduce the plot with seaborn:

import seaborn as sns
plt.figure(figsize=(10,6))
sns.lineplot(data=df.iloc[0:600,:], x='dateRep', y='deaths', hue='countriesAndTerritories')

Output:

enter image description here

Here I selected a subset of the whole dataframe (since there are 200 countries it would be too messy to show the legend). If you want to select another range, change the 0 and 600 (df.iloc[0:600,:]). Or if you have a list of countries you want to plot you can do this:

countries_to_plot = ["Zimbabwe", "Italy","France","Netherlands"]
sns.lineplot(data=df[df.countriesAndTerritories.isin(countries_to_plot)], x='dateRep', y='deaths', hue='countriesAndTerritories')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much, just wondering, how could I change the subset of countries?
Thank you so much! This makes me excited for data visualization:)
0

I believe you would like to plot day vs deaths and the country as a legend. Is my understanding correct? If that is the case, you can do something like this:

plt.scatter(df['day'], df['deaths'],label= df['country'])

4 Comments

I want a graph with the y axis being the names of the countries (countriesAndTerritories) and the x axis being the deaths
The link you shared has a separate graph for each country. That is not X axis, it seems like a legend of the plot
Yes the lines are coloured for hte countries on the link but I don't know if this is do-able with pandas.

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.