[英]How to make quadruple bar graph from this pandas dataframe?
I am a new coder, and for my class we have an assignment where we are supposed to be making an API call to an outside dataset and then plotting something interesting about the data.我是一名新的编码员,对于我的班级,我们有一项作业,我们应该对外部数据集进行 API 调用,然后绘制有关数据的一些有趣内容。 I made my API call to a NYC tree census data set.我对纽约市树木普查数据集进行了 API 调用。 In the data, it shows both tree species, and health status (Good, Fair, Poor, Dead).在数据中,它显示了树种和健康状况(好、一般、差、死)。 I want to make a stacked bar plot showing the percentage of health status for each tree.我想制作一个堆积条形图,显示每棵树的健康状况百分比。 For example, I want one bar for Maple trees, showing that 56% are good, 26% are fair, 13% are poor, and 5% are dead.例如,我想要一个枫树条,显示 56% 是好的,26% 是一般的,13% 是差的,5% 是死的。 I'm not really sure how to accomplish all of this.我不确定如何完成所有这些。 Here is a screenshot showing how my dataset looks.这是一个屏幕截图,显示了我的数据集的外观。 Thanks for any advice!感谢您的任何建议!
import kaggle.cli
import sys
import pandas as pd
from pathlib import Path
from zipfile import ZipFile
# search for data set
# sys.argv = [sys.argv[0]] + "datasets list -s \"2015-street-tree-census-tree-data.csv\"".split(" ")
# kaggle.cli.main()
# download data set
sys.argv = [sys.argv[0]] + "datasets download new-york-city/ny-2015-street-tree-census-tree-data".split(" ")
kaggle.cli.main()
zfile = ZipFile("ny-2015-street-tree-census-tree-data.zip")
zfile.infolist()
# use CSV
df = pd.read_csv(zfile.open(zfile.infolist()[0]))
plotly
使用plotly
准备数据和绘图import plotly.express as px
spc = 'spc_common'
# aggregate the data and shape it for plotting
dfa = (
df.groupby([spc, "health"])
.agg({"tree_id": "count"})
.groupby(level=spc)
.apply(lambda x: x / x.sum())
.unstack("health")
.droplevel(0, 1)
)
fig = px.bar(
dfa.reset_index(),
x=spc,
y=["Poor", "Fair", "Good"],
color_discrete_sequence=["red", "blue", "green"],
)
fig.update_layout(yaxis={"tickformat": "%"})
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(14, 3))
dfa.plot(kind="bar", stacked=True, ax=ax)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.