第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Matplotlib 僅繪制堆積條形圖上每 2 個(gè)條中的 1 個(gè)

Matplotlib 僅繪制堆積條形圖上每 2 個(gè)條中的 1 個(gè)

一只斗牛犬 2023-05-23 14:51:50
我正在嘗試?yán)L制下面的堆積條形圖,來自loglList. 或用于確定顏色,整數(shù)是條形代表的百分比'up'。'down'數(shù)組可以改變大小,所以這就是我有for循環(huán)的原因。但是,當(dāng)我運(yùn)行代碼時(shí),只繪制了 2 個(gè)柱中的 1 個(gè),其余是它們之間的空格。盡管聲明如此,它們的顏色也是錯(cuò)誤的if。我想知道是否有人知道為什么會(huì)發(fā)生這種情況?謝謝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貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

您的代碼似乎有幾個(gè)拼寫錯(cuò)誤,因此很難猜測其真實(shí)意圖。這是一個(gè)最小的重寫:


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()

http://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)



查看完整回答
反對(duì) 回復(fù) 2023-05-23
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

幾個(gè)錯(cuò)誤。嘗試:


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()


查看完整回答
反對(duì) 回復(fù) 2023-05-23
  • 2 回答
  • 0 關(guān)注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)