Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is a gist of the csv file I am working with:

CODE     AGEGROUP      SEX     CITY      HEALTHSTATUS 
----     ---------     ---     ----      ------------
E101      25 to 29      M      Denver    Recovered
E102      25 to 29      F      Chicago   Recovered
E105      45 to 49      M      Denver    Mild

I wish to present this using a bar chart but I am quite confused as to how to code subplots. In this code I can go as far as showing how many males and females are affected, but I can't present the health status and age group:

import matplotlib.pyplot as plt

plt.style.use("bmh")
x = df2["SEX"]
y = df2["SEX"].value_counts().plot(kind="bar")

plt.xlabel("Sex", fontsize=12)
plt.ylabel("Number of People", fontsize=12)
plt.title("Number of people affected based on sex", fontsize=12)
plt.show()

How can I show the other two (health status and age group)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
588 views
Welcome To Ask or Share your Answers For Others

1 Answer

One way of doing this is to create a separate subplot for each condition :

import pandas as df
import matplotlib.pyplot as plt

df = pd.DataFrame({'CODE':["E101", "E102", "E105"],
                   'AGEGROUP':["25 to 29", "25 to 29", "45 to 49"],
                   'SEX':["M", "F", "M"],
                   'CITY':["Denver", "Chicago", "Denver"],
                   'HEALTHSTATUS':["Recovered", "Recovered", "Mild"],
                   })

fs = 6
plt.style.use("bmh")
fig = plt.figure()
ax0 = plt.subplot(3, 1, 1)
df["SEX"].value_counts().plot(kind="bar", ax=ax0)
ax0.set_xlabel("Sex", fontsize=fs)
ax0.set_ylabel("Number of People", fontsize=fs)
ax0.tick_params(axis='both', labelsize=fs)
ax1 = plt.subplot(3, 1, 2)
df["AGEGROUP"].value_counts().plot(kind="bar", ax=ax1)
ax1.set_xlabel("AGEGROUP", fontsize=fs)
ax1.set_ylabel("Number of People", fontsize=fs)
ax1.tick_params(axis='both', labelsize=fs)
ax1.tick_params(axis='x', labelrotation=45)
ax2 = plt.subplot(3, 1, 3)
df["HEALTHSTATUS"].value_counts().plot(kind="bar", ax=ax2)
ax2.set_xlabel("HEALTHSTATUS", fontsize=fs)
ax2.set_ylabel("Number of People", fontsize=fs)
ax2.tick_params(axis='both', labelsize=fs)
ax2.tick_params(axis='x', labelrotation=45)

ax0.set_title("Number of people affected based on condition", fontsize=fs)
plt.tight_layout()
plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...