首页 > 解决方案 > 如何在 jupyter notebook 上使用 python 中的 pyplot 包绘制直方图

问题描述

我正在尝试在 jupyter Notebook 上绘制直方图,其中数据是从数据框中提取的,以可视化所用数据集中每条记录的正面和负面情绪。

问题是,一旦我尝试绘制结果,就会显示以下错误并显示一个空的可视化。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-8903e5cb1735> in <module>
      5 plt.figure(figsize=(12,6))
      6 plt.xlim(0,45)
----> 7 plt.xlabel("word count")
      8 plt.ylabel("frequency")
      9 g = plt.hist([x,y],color=["r","b"],alpha=0.5,label=["positive","negative"])

TypeError: 'str' object is not callable

在此处输入图像描述

代码

import matplotlib.pyplot as plt
#plot word count distribution for both positive and negative sentiment
x= df_tweet_preds["word count"][df_tweet_preds.predictions ==1]
y= df_tweet_preds["word count"][df_tweet_preds.predictions ==0]
plt.figure(figsize=(12,6))
plt.xlim(0,45)
plt.xlabel("word count")
plt.ylabel("frequency")
g = plt.hist([x,y],color=["r","b"],alpha=0.5,label=["positive","negative"])
plt.legend(loc="upper right")

标签: pythonmatplotlibjupyter-notebooksentiment-analysis

解决方案


推荐阅读