我一直在努力在DataFrame中生成名为“国家”和“公司”的2列的频率图,并将它们显示为2个子图。这就是我所拥有的。
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')
作为Subplot1。没有属性pd。 〜有人能对此有所启发吗?
预先感谢您的提示,
R。
答案 0 :(得分:1)
Pandas的绘图方法可以接收Matplotlib轴对象,并将生成的绘图定向到该子图中。
# 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 :(得分:1)
您不必分别创建Figure
和Axes
对象,并且您应该避免使用变量名中的初始大写字母来将它们与类区分开。
在这里,您可以使用plt.subplots
,它创建一个Figure
和许多Axes
并将它们绑定在一起。然后,您可以将Axes
对象传递给plot
的{{1}}方法:
pandas