【问题标题】:Pandas.plotting doesn't show graphPandas.plotting 不显示图表
【发布时间】:2018-05-13 14:13:05
【问题描述】:

我正在学习“python 机器学习简介”一书中的教程,并复制了以下代码:

#create dataframe from data in X_train
#label the colums using the strings in iris_dataset.features_names
iris_dataframe = pd.DataFrame(X_train, columns = iris_dataset.feature_names)
#create a scatter matrix from the dataframe, color by y_train
pd.plotting.scatter_matrix(iris_dataframe,c=y_train,
                           figsize=(15,15), marker='o', 
                           hist_kwds={'bins':20},s=60, 
                           alpha=.8,
                           cmap=mglearn.cm3)

它应该绘制一个图形,但它只打印那些线:

array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d073934a8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07352908>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07376e48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d0732ee48>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d072e3f28>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d072e3f60>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07308ac8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07211400>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d071ca470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07183470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d071be470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07165e80>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07127390>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d070e5390>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d0709d390>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07047da0>]], dtype=object)

谁能告诉我一个原因?

EDIT-SOLVED:我不知道为什么,但在重新运行单元格后,图表出现了..

【问题讨论】:

  • 也许查看 Pandas 文档以进行绘图,看看您是否遗漏了什么? pandas.pydata.org/pandas-docs/stable/visualization.html
  • 我认为文档中的所有这些示例都希望在 Jupyter 之类的东西中运行,它会自动显示图表。如果你在常规的 python 解释器中运行它,你需要自己调用matplotlib.pyplot.show
  • 有时在 Jupyter 中你必须打开内联绘图,%matplotlib inline
  • 如果您自己解决了问题,请发布您自己问题的答案,而不是编辑原始问题
  • 您可以抑制文本输出的行,它们是 scatter _matrix() 函数返回的值的 str() 或 repr() (我忘记了)版本。为此,只需将输出设置为一个变量:axes = pd.plotting.scatter_matrix(iris_dataframe,c=y_train, ...

标签: python pandas matplotlib machine-learning


【解决方案1】:

pandas 本身没有绘图库。大多数用户在绘图时通常使用具有不同命令集的 matplotlib 库。假设您安装了所有正确的库,包括 matplotlib,我会以以下方式结束您的代码:

import matplotlib.pyplot as plt
#assuming you got the correct conversion to a pandas dataframe  
pd.plotting.scatter_matrix(df,c=y_train,
                       figsize=(15,15), marker='o', 
                       hist_kwds={'bins':20},s=60, 
                       alpha=.8,
                       cmap=mglearn.cm3)


plt.show()

这里有一个link到matplotlib库,进入plt.show()还有保存图的方法,可以在文档here中找到

【讨论】:

    【解决方案2】:

    已解决! 我不知道为什么,但是在重新运行 jupyter notebook 的所有单元格后,图表显示正确。

    【讨论】:

    • 您必须确保在单元格中使用“import matplotlib.pyplot as plt”语句才能使其正常工作。如果您正在调用将绘制图形的函数,请确保使用函数定义中的语句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2018-02-09
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多