为了账号安全,请及时绑定邮箱和手机立即绑定

Matplotlib 仅绘制堆积条形图上每 2 个条中的 1 个

Matplotlib 仅绘制堆积条形图上每 2 个条中的 1 个

一只斗牛犬 2023-05-23 14:51:50
我正在尝试绘制下面的堆积条形图,来自loglList. 或用于确定颜色,整数是条形代表的百分比'up'。'down'数组可以改变大小,所以这就是我有for循环的原因。但是,当我运行代码时,只绘制了 2 个柱中的 1 个,其余是它们之间的空格。尽管声明如此,它们的颜色也是错误的if。我想知道是否有人知道为什么会发生这种情况?谢谢import numpy as npimport matplotlib.pyplot as pltloglist = [['up', 0.8519966683633334], ['down', 0.09023182638471149], ['up', 4.120008329091666], ['down', 0.09023182638471149], ['up', 0.3441534403775855], ['down', 0.089075008097728], ['up', 0.3447318495210772], ['down', 1.650779695525427], ['up', 11.537527185229743], ['down', 22.519781592707417], ['up', 14.05245014113183], ['down', 4.601823145620286], ['up', 3.8163435287585026], ['down', 2.34313544028504], ['up', 7.137568830688075], ['down', 0.7415205219564112], ['up', 3.7324742029522002], ['down', 21.91534403775855]]width = 0.3barDict = []for index, plotLog in enumerate(logList):    if(plotLog[0] == "up"):        colour = "g"    elif(plotLog[0] == "down"):        colour = "r"    bottom = 0    for i in range(index):        bottom = bottom + logList[i][1]    barDict[index] = plt.bar(1, plotLog[1], width, bottom=bottom, color=colour)plt.yticks(np.arange(0, 101, 10))plt.show()
查看完整描述

2 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

您的代码似乎有几个拼写错误,因此很难猜测其真实意图。这是一个最小的重写:


import numpy as np

import matplotlib.pyplot as plt


logList = [['up', 0.8519966683633334], ['down', 0.09023182638471149], ['up', 4.120008329091666], ['down', 0.09023182638471149], ['up', 0.3441534403775855], ['down', 0.089075008097728], ['up', 0.3447318495210772], ['down', 1.650779695525427], ['up', 11.537527185229743], ['down', 22.519781592707417], ['up', 14.05245014113183], ['down', 4.601823145620286], ['up', 3.8163435287585026], ['down', 2.34313544028504], ['up', 7.137568830688075], ['down', 0.7415205219564112], ['up', 3.7324742029522002], ['down', 21.91534403775855]]


width = 0.3

bottom = 0

for index, plotLog in enumerate(logList):

    if (plotLog[0] == "up"):

        colour = "g"

    elif (plotLog[0] == "down"):

        colour = "r"

    plt.bar(1, plotLog[1], width, bottom=bottom, color=colour)

    bottom += plotLog[1]

plt.yticks(np.arange(0, 101, 10))

plt.xticks([])

plt.show()

//img1.sycdn.imooc.com//646c62a400015b7203300225.jpg

或者,您可以使用以下方法绘制相同的图:


width = 0.3

vals = np.array([plotLog[1] for plotLog in logList])

colours = ['g' if plotLog[0] == "up" else 'r' for plotLog in logList]

bars = plt.bar(np.ones_like(vals), vals, bottom=np.append(0, np.cumsum(vals[:-1])), color=colours, width=width)



查看完整回答
反对 回复 2023-05-23
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

几个错误。尝试:


import numpy as np

import matplotlib.pyplot as plt


logList = [['up', 0.8519966683633334], ['down', 0.09023182638471149], ['up', 4.120008329091666], ['down', 0.09023182638471149], ['up', 0.3441534403775855], ['down', 0.089075008097728], ['up', 0.3447318495210772], ['down', 1.650779695525427], ['up', 11.537527185229743], ['down', 22.519781592707417], ['up', 14.05245014113183], ['down', 4.601823145620286], ['up', 3.8163435287585026], ['down', 2.34313544028504], ['up', 7.137568830688075], ['down', 0.7415205219564112], ['up', 3.7324742029522002], ['down', 21.91534403775855]]


width = 0.3

barDict = []


for index, plotLog in enumerate(logList):

    if(plotLog[0] == "up"):

        colour = "g"

    elif(plotLog[0] == "down"):

        colour = "r"


    bottom = 0


    for i in range(index):

        bottom = bottom + logList[i][1]


    print(plotLog, plotLog[0])

    barDict.append(plt.bar(1, plotLog[1], width, bottom=bottom, color=colour))


plt.yticks(np.arange(0, 101, 10))

plt.show()


查看完整回答
反对 回复 2023-05-23
  • 2 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信