gpt4 book ai didi

python - 如何在matplotlib中设置条形图的大小

转载 作者:行者123 更新时间:2023-12-01 07:34:19 25 4
gpt4 key购买 nike

我已经成功地用 matplotlib 绘制了簇状条形图,但我面临的问题是,有些数字与其他数字相比太小,并且当用 matplotlib 绘制时,它们看起来不存在。是否可以更改 y 轴,或者是否有任何方法可以使较小的条形图看起来更明显?

这是我的可视化代码。

# Setting the positions and width for the bars
pos = list(range(len(df3['Air'])))
width = 0.25

# Plotting the bars
fig, ax = plt.subplots(figsize=(10,5))

# Create a bar with pre_score data,
# in position pos,
plt.bar(pos,
#using df['pre_score'] data,
df3['Air'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#EE3224',
# with label the first value in first_name
label=df3['Region'][0])

# Create a bar with mid_score data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
#using df['mid_score'] data,
df3['Land'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#F78F1E',
# with label the second value in first_name
label=df3['Region'][1])

# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*2 for p in pos],
#using df['post_score'] data,
df3['Sea'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#FFC222',
# with label the third value in first_name
label=df3['Region'][2])

# Set the y axis label
ax.set_ylabel('Arrival count')

# Set the chart's title
ax.set_title('Total arrival count by mode of transport by region in 2014')

# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])

# Set the labels for the x ticks
ax.set_xticklabels(df3['Region'])

# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(df3['Air'] + df3['Land'] + df3['Sea'])] )

# Adding the legend and showing the plot
plt.legend(['Air', 'Land', 'Sea'], loc='upper left')
plt.grid()
plt.show()

这是我的图表。正如您所观察到的,大多数图表都看不到。

This is the graph that I have. As you can observe, most graphs cannot be seen.

最佳答案

尝试使用 log scale :

plt.yscale('log')

plt.bar([p + width*2 for p in pos],
....)

关于python - 如何在matplotlib中设置条形图的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57067832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com