【问题标题】:Scatterplot error : "x and y must be the same size" but they have the same size散点图错误:“x 和 y 的大小必须相同”但它们的大小相同
【发布时间】:2022-01-12 15:08:28
【问题描述】:

我想用数据框制作散点图:“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()

【问题讨论】:

  • 将此plt.scatter(x="mois", y = "death_cause", data = df_death_mois1) 更改为df_death_mois1.plot.scatter(x='mois', y='death_cause')
  • 不行
  • KeyError: 'mois'
  • 是索引,去掉x选项。
  • expeditions 中没有名为“mois”的列。你到底想合并什么?

标签: python matplotlib scatter-plot


【解决方案1】:

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"])

【讨论】:

  • 非常感谢
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 2016-09-17
  • 2019-08-20
  • 2020-02-23
  • 2017-05-30
  • 2021-10-20
  • 2014-08-25
  • 2022-01-17
相关资源
最近更新 更多