首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子图频率图

子图频率图
EN

Stack Overflow用户
提问于 2019-05-14 09:37:00
回答 2查看 882关注 0票数 1

我一直在努力在我的DataFrame中生成名为"Country“和"Company”的2列的频率图,并将它们显示为2个子图。这就是我得到的。

代码语言:javascript
复制
 Figure1 = plt.figure(1)
 Subplot1 = Figure1.add_subplot(2,1,1)

在这里,我将使用条形图pd.value_counts(DataFrame['Country']).plot('barh')来显示为第一个子图。问题是,我不能就这么走:Subplot1.pd.value_counts(DataFrame['Country']).plot('barh') as Subplot1。没有pd属性。~有没有人能对此有所了解?

先感谢你的提示,

R.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-14 10:01:27

您不必单独创建FigureAxes对象,而且您可能应该避免变量名中的首字母大写,以便将它们与类区分开来。

在这里,您可以使用plt.subplots,它创建一个Figure和许多Axes,并将它们绑定在一起。然后,您可以只将Axes对象传递给pandasplot方法

代码语言:javascript
复制
from matplotlib import pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))

pd.value_counts(df['Country']).plot('barh', ax=ax1)
pd.value_counts(df['Company']).plot('barh', ax=ax2)
票数 3
EN

Stack Overflow用户

发布于 2019-05-14 10:00:52

Pandas的plot方法可以接受Matplotlib axes对象,并将结果图定向到该子图中。

代码语言:javascript
复制
# If you want a two plots, one above the other.
nrows = 2
ncols = 1

# Here axes contains 2 objects representing the two subplots
fig, axes = plt.subplots(nrows, ncols, figsize=(8, 4))

# Below, "my_data_frame" is the name of your Pandas dataframe.
# Change it accordingly for the code to work.

# Plot first subplot
# This counts the number of times each country appears and plot
# that as a bar char in the first subplot represented by axes[0].
my_data_frame['Country'].value_counts().plot('barh', ax=axes[0])

# Plot second subplot
my_data_frame['Company'].value_counts().plot('barh', ax=axes[1])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56121883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档