Home > Enterprise >  Scatterplot error : "x and y must be the same size" but they have the same size
Scatterplot error : "x and y must be the same size" but they have the same size

Time:12-08

I would like to make a scatterplot with the dataframe :"df_death_mois1". But it doesn't work. The error message is : "x and y must be the same size". Can you help me ?

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()

CodePudding user response:

reset_index and then call plot.scatter:

>>> df_death_mois1.reset_index().plot.scatter(x="mois", y="death_cause")

enter image description here

With matplotlib.pyplot you can use:

>>> plt.scatter(x=df_death_mois1.index, y=df_death_mois1["death_cause"])
  • Related