【问题标题】:Drawing plot and displaying head at the same time - PyCharm同时绘制绘图和显示头部 - PyCharm
【发布时间】:2018-10-23 06:08:06
【问题描述】:

在下面的代码中,只绘制了plot,但没有打印head,为什么?

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("file.csv")
df.set_index("id", inplace=True)
plt.plot(df)
plt.show() # this draws plof of entire df form csv
print(df.head(10)) # this does not print the first 10 rows of the dataframe

【问题讨论】:

  • 不知道懂不懂,需要print(df. head())吗?

标签: python pandas matplotlib pycharm


【解决方案1】:

如果只想先绘制5 行,则需要重新分配:

df = df.head()
plt.plot(df)

或者:

plt.plot(df.head())

熊猫DataFrame.plot:

df.head().plot()

但如果想查看前 5 行,请使用:

print(df. head())

如果想要将图表与图表一起使用this solution,只需少量更改:

clust_data = df.head().values

【讨论】:

  • @jazreal 我已经编辑了这个问题。所以我想打印整个 df 的图,另外打印 10 行的 head。现在它只绘制情节。
  • PS 当我删除 plt 行 (plt.plot(df) plt.show() ) head 将按预期打印
  • 好的,当我切换它时,print(df.head(10)) 被打印并绘制了情节
  • @mCs - 我认为另一个解决方案正是我们所需要的。
  • 在 Jupyter 中确定 print(df.head(10)) 然后绘图工作正常,但在 PyCharm 中却不是
【解决方案2】:

plt.show() 阻塞脚本的环境中(由于它启动了一个事件循环),在将其作为脚本运行或从控制台运行时会出现这种情况,您需要

  • 先关图
  • 打印之前显示该图的值,

    plt.plot(df)
    print(df.head(10))
    plt.show()
    

【讨论】:

  • 问题在于 PyCharm plt.plot(df) print(df.head(10)) plt.show() 仍然只绘制情节
  • 您使用 PyCharm 的事实很重要,您错过了在问题中说明这一点。我认为 PyCharm 有很多问题,这就是我根本不使用它的原因。但当然可能有解决方案,我不知道。
  • 谢谢,我已将此信息附加到该主题。你能推荐我另一个可靠和有效的工具吗?可能是您正在使用的那个?
  • 我正在使用 Spyder。我不能推荐任何东西,适合自己的东西不适合其他人。
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 2014-06-30
  • 1970-01-01
  • 2015-05-16
  • 2017-02-27
  • 1970-01-01
相关资源
最近更新 更多