0

I have a df. See below for the head:

Country         Date     suspected case    confirmed cases   suspected deaths   confirmed deaths   

0   Guinea   2014-08-29       25.0            141.0              482.0              648.0
1   Nigeria  2014-08-29       3.0             1.0                15.0               19.0
2   Liberia  2014-08-29       382.0           674.0              322.0              1378.0

By using df.groupby('Country') I want to plot the suspected case against the confirmed case using the Date column for the xaxis. Plotting these as (5, 2) subplots

What I've done so far hasn't quite got it yet:

fig, ax = plt.subplots(2, 5, sharey=True)
df.groupby('Country').plot(x='Date', y=['suspected cases', 'confirmed cases'], title=f'Suspected vs. Confirmed cases {country}')
plt.show()

What's happened so far is that there is an empty 5x2 subplot and below displays each graph individually. Why is this?

Also just a minor issue but would like some clarification. Within my .plot() function, why is the last grouped country only being shown in the title? For instance I have 10 countries grouped together for this plot but the title Suspected vs. Confirmed cases USA is showing fr each graph.

Looked at a few SO posts and combined a few answers to try to solve my problem but I seem to be going in circles.

2
  • So you have 10 countries and want to plot each country on a separate subplot? Commented Mar 23, 2020 at 14:14
  • @QuangHoang Yup! Commented Mar 23, 2020 at 14:23

1 Answer 1

1

You could do:

fig, ax = plt.subplots(2, 5, sharey=True)
for (c,d), a in zip(df.groupby('Country'), ax.ravel()):
    d.plot(x='Date', 
           y=['suspected cases', 'confirmed cases'], 
           title=f'Suspected vs. Confirmed cases {c}', 
           ax=a, subplots=False)

Output:

enter image description here

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.