使用Pandas DataFrame创建Matplotlib柱状图。

huangapple go评论92阅读模式
英文:

using pandas dataframe to create matplotlib bar chart

问题

这是我的pandasDF数据框的一个示例。这是一个电影数据集。计数表示每部电影标题的表示次数。

| 发布年份 | 计数 |
| 1989 | 1 |
| 1990 | 1 |
| 1991 | 1 |
| 1992 | 1 |
| 1993 | 1 |

之前我的数据框是一个Spark数据框,但我将其转换为一个pandas数据框。

  1. pandasDF = movies_DF.toPandas()
  2. pandasDF.head()

这是我现在拥有的绘图。

使用Pandas DataFrame创建Matplotlib柱状图。

我试图实现一个看起来像这样的绘图。

使用Pandas DataFrame创建Matplotlib柱状图。

这是我的代码:

  1. x = list(pandasDF[pandasDF['yearOfRelease'] >= '1990']['yearOfRelease'])
  2. import matplotlib.pyplot as plt
  3. plt.figure()
  4. pd.value_counts(x).plot.bar(title="每年发布的电影数量")
  5. plt.xlabel("年份")
  6. plt.ylabel("电影数量")
  7. plt.show()
英文:

This is an example of how my pandasDF dataframe look like. This is a movie dataset. The count represent how each movie title represent 1.

  1. | yearOfRelease |count|
  2. | 1989 | 1 |
  3. | 1990 | 1 |
  4. | 1991 | 1 |
  5. | 1992 | 1 |
  6. | 1993 | 1 |

Previously my dataframe is a spark dataframe but i convert it to a pandas dataframe

  1. pandasDF = movies_DF.toPandas()
  2. pandasDF.head()

This is the plot i have right now.

使用Pandas DataFrame创建Matplotlib柱状图。

I am trying to achieve a plot that look like this.

使用Pandas DataFrame创建Matplotlib柱状图。

This is my code:

  1. x = list(pandasDF[pandasDF['yearOfRelease'] >= '1990']['yearOfRelease'])
  2. import matplotlib.pyplot as plt
  3. plt.figure()
  4. pd.value_counts(x).plot.bar(title="number of movies by year of release ")
  5. plt.xlabel("yearOfRelease")
  6. plt.ylabel("numOfMovies")
  7. plt.show()

答案1

得分: 2

以下是您提供的代码部分的翻译:

  1. import matplotlib.pyplot as plt
  2. # 选择年份大于等于1990的数据并将'yearOfRelease'列转换为整数类型
  3. sr = pandasDF.loc[pandasDF['yearOfRelease'] >= '1990', 'yearOfRelease'].astype(int)
  4. # 绘制柱状图,显示每年上映电影的数量
  5. ax = (sr.value_counts().sort_index()
  6. .plot.bar(title='电影年度发行数量',
  7. xlabel='年份', ylabel='电影数量',
  8. rot=-90))
  9. # 设置x轴的主要刻度为每2年一次
  10. ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
  11. # 设置y轴的主要刻度为每500个电影一次
  12. ax.yaxis.set_major_locator(ticker.MultipleLocator(500))
  13. # 调整布局并显示图形
  14. plt.tight_layout()
  15. plt.show()

希望这有帮助。如果您需要进一步的翻译或解释,请告诉我。

英文:

You have to sort your values by index (yearOfRelease):

  1. import matplotlib.pyplot as plt
  2. sr = pandasDF.loc[pandasDF['yearOfRelease'] >= '1990', 'yearOfRelease'].astype(int)
  3. ax = (sr.value_counts().sort_index()
  4. .plot.bar(title='number of movies by year of release',
  5. xlabel='yearOfRelease', ylabel='numOfMovies',
  6. rot=-90))
  7. ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
  8. ax.yaxis.set_major_locator(ticker.MultipleLocator(500))
  9. plt.tight_layout()
  10. plt.show()

Output:

使用Pandas DataFrame创建Matplotlib柱状图。

huangapple
  • 本文由 发表于 2023年2月10日 03:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定