我一直在努力在我的DataFrame中生成名为"Country“和"Company”的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') as Subplot1。没有pd属性。~有没有人能对此有所了解?
先感谢你的提示,
R.
发布于 2019-05-14 10:01:27
您不必单独创建Figure和Axes对象,而且您可能应该避免变量名中的首字母大写,以便将它们与类区分开来。
在这里,您可以使用plt.subplots,它创建一个Figure和许多Axes,并将它们绑定在一起。然后,您可以只将Axes对象传递给pandas的plot方法
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)发布于 2019-05-14 10:00:52
Pandas的plot方法可以接受Matplotlib axes对象,并将结果图定向到该子图中。
# 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])https://stackoverflow.com/questions/56121883
复制相似问题