Pyplot - bar chart of positive and negative values

后端 未结 1 1036
说谎
说谎 2021-02-07 17:52

I want the negative bars to be facing downwards, and the positive upwards, with the x-axis(0-line) passing right between them. I tried this

chart = fig.bar(x, ne         


        
相关标签:
1条回答
  • 2021-02-07 18:35

    You don't need to add a twin axis, you can plot both bar charts on the same axis like this:

    x = range(7)
    negative_data = [-1,-4,-3,-2,-6,-2,-8]
    positive_data = [4,2,3,1,4,6,7,]
    
    fig = plt.figure()
    ax = plt.subplot(111)
    ax.bar(x, negative_data, width=1, color='r')
    ax.bar(x, positive_data, width=1, color='b')
    

    bar chart with negative an positive y values

    Bars with a width of one will fill the axes and will scale with the figure as it is resized.

    0 讨论(0)
提交回复
热议问题