python中matplotlib条形图数值大的在最底层显示,matplotlib中的条形图在x坐标上具有较大范围时显示较少的条形图...

What am I doing wrong here with a bar chart in python and matplotlib? The first plot is good, the second has a much wider range on the x-axis up to 1500. Notice in the second plot that most of the bars on the low scale disappear, but also the bar at 1500 is not shown.

I tried setting the width=0.8 in the bar() method, but that does not help.

With a logarithmic x-axis (using plt.xscale('log') ) it seems to work fine

import matplotlib.pyplot as plt

import numpy as np

fig = plt.figure(figsize=(12, 6))

plt.bar(np.array([1,2,3,4,11,12]), np.array([10,2,1,1,3,10]))

plt.show()

#Then I close the figure and run this:

plt.bar(np.array([1,2,3,4,11,1500]), np.array([10,2,1,1,3,10]))

plt.show()

nQ5fh.png

FVbgv.png

解决方案

Bars are by default 1 data units wide. The axes is ~600 pixels wide, but has ~1500 units. The chances you will see any single bar is hence 600/1500*1 = 40%. In that sense it's bad luck that you don't see the bar at x=1500. The other bars are so close together that you don't see which one of them is actually shown.

Instead of relying on the chances of seeing a bar, you would rather make the bar wide enough such that it can be seen safely. E.g. using a width of 5

plt.bar(np.array([1,2,3,4,11,1500]), np.array([10,2,1,1,3,10]), width=5)

Alternatively you could also show a stem plot, i.e. a plot with lines instead of bars,

plt.stem(np.array([1,2,3,4,11,1500]), np.array([10,2,1,1,3,10]),markerfmt=" ", basefmt=" ")

plt.ylim(0,None)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值