plt.plot分类轴过度拥挤

时间:2020-05-05 11:39:09

标签: python matplotlib plot

我目前正在绘制一个数据框,其中包含119个观察结果,这些观察结果表示按国家(类别)分类的回收率(浮动)。我曾尝试绘制散点图和条形图,但是两次都遇到了绝对y轴的拥挤情况。

我认为可以增大轴或增加类别之间的间距,但没有找到任何有用的方法。您能否向我解释如何改进代码,以便可以观察y轴上的所有国家?

附带的是MWE(尽管最终很简单),plt.plot()输出和dataset.info()

MWE:

plt.plot(
    rr_data['r_rate'], 
    rr_data['country'], 
    '.', 
    color='red')
plt.title('Recycling rates by country')
plt.ylabel('Countrycode')
plt.xlabel('Waste recycled, %')

信息:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 119 entries, 0 to 120
Data columns (total 2 columns):
country    119 non-null object
r_rate     119 non-null float64
dtypes: float64(1), object(1)
memory usage: 2.8+ KB

图: rr_by_country

2 个答案:

答案 0 :(得分:1)

您可以使用figsize=(x,y)

来增加整个图形的大小

所以您可以简单地

plt.plot(rr_data['r_rate'], rr_data['country'], figsize(20,20),'.',color='red')
plt.title('Recycling rates by country')
plt.ylabel('Countrycode')
plt.xlabel('Waste recycled, %')

这是您要找的东西吗?您可以玩弄数字以所需的方式增加情节

答案 1 :(得分:1)

有几种方法可以定义图形的大小。例如:

fig, ax = plt.subplot(figsize=(10, 10))
ax.plot(rr_data['r_rate'], rr_data['country'], figsize(10,10),'.',color='red')

OR

plt.plot(rr_data['r_rate'], rr_data['country'], figsize(10,10),'.',color='red')

OR

fig, ax = plt.subplots()
ax.plot(...)

fig.set_size_inches(width,height)