Bar chart in matplotlib shows less bars when having big range on x coordinates

后端 未结 1 909
时光取名叫无心
时光取名叫无心 2020-12-07 04:10

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 secon

相关标签:
1条回答
  • 2020-12-07 04:44

    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)
    
    0 讨论(0)
提交回复
热议问题