绘制 Python 中的值组
Plotting over groups of values in Python
我有一个看起来像这样的数据框。
country age new_user
298408 UK 32 1
193010 US 37 0
164494 UK 17 0
28149 US 34 0
297080 China 29 1
我想在 Python 中的单个图表中为每个国家/地区的年龄组(20-30、30-40 等)绘制 new_users 的计数。
基本上,我需要为所有年龄组绘制 new_user(值 0),为所有国家/地区的所有年龄组绘制 new_user(值 1)。
我发现很难将年龄分为 20-30、30-40 等等。
有人可以帮我在 python 中使用 seaborn 或 ggplot 或 matplotlib 绘制这个吗? ggplot 是首选!
谢谢。
import seaborn as sns
from pandas import DataFrame
from matplotlib.pyplot import show, legend
d = {"country": ['UK','US','US','UK','PRC'],
"age": [32, 37, 17, 34, 29],
"new_user": [1, 0, 0, 0,1]}
df = DataFrame(d)
bins = range(0, 100, 10)
ax = sns.distplot(df.age[df.new_user==1],
color='red', kde=False, bins=bins, label='New')
sns.distplot(df.age[df.new_user==0],
ax=ax, # Overplots on first plot
color='blue', kde=False, bins=bins, label='Existing')
legend()
show()
我有一个看起来像这样的数据框。
country age new_user
298408 UK 32 1
193010 US 37 0
164494 UK 17 0
28149 US 34 0
297080 China 29 1
我想在 Python 中的单个图表中为每个国家/地区的年龄组(20-30、30-40 等)绘制 new_users 的计数。
基本上,我需要为所有年龄组绘制 new_user(值 0),为所有国家/地区的所有年龄组绘制 new_user(值 1)。
我发现很难将年龄分为 20-30、30-40 等等。 有人可以帮我在 python 中使用 seaborn 或 ggplot 或 matplotlib 绘制这个吗? ggplot 是首选!
谢谢。
import seaborn as sns
from pandas import DataFrame
from matplotlib.pyplot import show, legend
d = {"country": ['UK','US','US','UK','PRC'],
"age": [32, 37, 17, 34, 29],
"new_user": [1, 0, 0, 0,1]}
df = DataFrame(d)
bins = range(0, 100, 10)
ax = sns.distplot(df.age[df.new_user==1],
color='red', kde=False, bins=bins, label='New')
sns.distplot(df.age[df.new_user==0],
ax=ax, # Overplots on first plot
color='blue', kde=False, bins=bins, label='Existing')
legend()
show()