1

我正在尝试可视化 Worldometer Covid19 数据,但是当我这样做时seaborn lmplot,我收到一条错误消息,内容为:

IndexError: Invalid index to scalar variable.

这些图也显示错误,但我更喜欢在没有错误消息的情况下显示它们。

这是代码片段( lmplot 在最后):

# Read Dataset
df = pd.read_csv('../input/corona-virus-report/worldometer_data.csv')

# Fill missing values with zeros
df= df.fillna(0)
# Deaths per 1M population for each continent 
plt.figure(figsize=(10,6))
sns.barplot(x=df['Continent'],y=df['Deaths/1M pop'])
# Distribution of Deaths per 1M population
plt.figure(figsize=(10,6))
sns.kdeplot(data=df['Deaths/1M pop'],shade=True)
# Distribution of Cases per 1M population
plt.figure(figsize=(10,6))
sns.kdeplot(data=df['Tot Cases/1M pop'],shade=True)
# Joint Distribution of Deaths and Cases per 1 M Population.
plt.figure(figsize=(10,6))
sns.jointplot(x=df['Tot Cases/1M pop'], y=df['Deaths/1M pop'], kind="kde")

# We slice the dataset into a dataframe of the columns we need to plot
selected_df2 = df.loc[:,['Tot Cases/1M pop','Tests/1M pop','Continent']]

# first plot is to show the relation between tests and cases per 1M poplulation.
plt.figure(figsize=(10,6))
sns.regplot(x=selected_df2['Tests/1M pop'],y=selected_df2['Tot Cases/1M pop'])



# Second plot is uses the Continent as hue to detect if there is a genetic effect.
plt.figure(figsize=(10,6))
sns.lmplot(x="Tests/1M pop",y="Tot Cases/1M pop", hue="Continent", data=df)

这是错误:

<!-- language: none -->

IndexError                                Traceback (most recent call last)
<ipython-input-2-9794e0b2f139> in <module>
     29 # use the Continent as hue to detect if there is a genetic effect.
     30 plt.figure(figsize=(10,6))
---> 31 sns.lmplot(x="Tests/1M pop",y="Tot Cases/1M pop", hue="Continent", data=df)

/opt/conda/lib/python3.7/site-packages/seaborn/regression.py in lmplot(x, y, data, hue, col, row, palette, col_wrap, height, aspect, markers, sharex, sharey, hue_order, col_order, row_order, legend, legend_out, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, seed, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, x_jitter, y_jitter, scatter_kws, line_kws, size)
    615         scatter_kws=scatter_kws, line_kws=line_kws,
    616         )
--> 617     facets.map_dataframe(regplot, x, y, **regplot_kws)
    618 
    619     # Add a legend

/opt/conda/lib/python3.7/site-packages/seaborn/axisgrid.py in map_dataframe(self, func, *args, **kwargs)
    831 
    832             # Draw the plot
--> 833             self._facet_plot(func, ax, args, kwargs)
    834 
    835         # Finalize the annotations and layout

/opt/conda/lib/python3.7/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
    849 
    850         # Draw the plot
--> 851         func(*plot_args, **plot_kwargs)
    852 
    853         # Sort out the supporting information

/opt/conda/lib/python3.7/site-packages/seaborn/regression.py in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, seed, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)
    808                                  order, logistic, lowess, robust, logx,
    809                                  x_partial, y_partial, truncate, dropna,
--> 810                                  x_jitter, y_jitter, color, label)
    811 
    812     if ax is None:

/opt/conda/lib/python3.7/site-packages/seaborn/regression.py in __init__(self, x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, seed, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, color, label)
    112         # Drop null observations
    113         if dropna:
--> 114             self.dropna("x", "y", "units", "x_partial", "y_partial")
    115 
    116         # Regress nuisance variables out of the data

/opt/conda/lib/python3.7/site-packages/seaborn/regression.py in dropna(self, *vars)
     64             val = getattr(self, var)
     65             if val is not None:
---> 66                 setattr(self, var, val[not_na])
     67 
     68     def plot(self, ax):

IndexError: invalid index to scalar variable.

请帮我解决这个错误...谢谢:)

4

0 回答 0