我想用資料框制作散點圖:“df_death_mois1”。但它不起作用。錯誤訊息是:“x 和 y 的大小必須相同”。你能幫助我嗎 ?
import pandas as pd
import matplotlib.pyplot as plt
members = pd.read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/members.csv")
expeditions = pd.read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/expeditions.csv")
expeditions['highpoint_date'] = pd.to_datetime(expeditions['highpoint_date'])
lesmois = expeditions['highpoint_date'].dt.month
expeditions["mois"] = lesmois
expeditions
df_members_mois = pd.merge(members,expeditions[['expedition_id','mois']], on='expedition_id', how='inner')
df_death_mois = df_members_mois[df_members_mois["death_cause"]=="Avalanche"]
df_death_mois
df_death_mois1 = df_death_mois.groupby("mois")['death_cause'].count()
df_death_mois1 = df_death_mois1.to_frame()
df_death_mois1
plt.scatter(x="mois", y = "death_cause", data = df_death_mois1)
plt.title('scatterplot')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
uj5u.com熱心網友回復:
reset_index然后呼叫plot.scatter:
>>> df_death_mois1.reset_index().plot.scatter(x="mois", y="death_cause")

有了matplotlib.pyplot你可以使用:
>>> plt.scatter(x=df_death_mois1.index, y=df_death_mois1["death_cause"])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/376210.html
標籤:Python matplotlib 散点图
上一篇:型別錯誤:*不支持的運算元型別:Python中的“QuadMesh”和“float”
下一篇:跨列共享直方圖箱
