简体   繁体   English

根据值更改条形图颜色

[英]Changing Bar chart color based on value

I have the following data: 8,518,000 27,525,000 20,532,000 12,897,000 (27,858,000) (5,266,000) (18,442,000) (78,850,000) (43,813,000) (26,920,000) (4,501,000) (19,619,000) (12,938,000) The first four years positive and the last 9 years is negative.我有以下数据:8,518,000 27,525,000 20,532,000 12,897,000(27,858,000)(27,858,000)(5,266,000)(18,442,000)(18,442,000)(78,850,000)(78,850,000)(43,813,000)(43,813,000)(26,920,000)(26,920,000)(4,501,000)(4,501,000)(4,501,000)(19,6119,000)(12,619,000)(12,938,000)(12,938)。

For my plotly barchart, I want the color to be black when positive and red when negative.对于我的 plotly 条形图,我希望颜色在正时为黑色,在负时为红色。 What is the easiest way to color this?最简单的着色方法是什么?

Right now I have everything set to red with marker_color='red' but marker_color[0:3]='black' causes an error.现在我用 marker_color='red' 将所有内容设置为红色,但 marker_color[0:3]='black' 会导致错误。

Your question is a bit unclear but if I understand you correctly, you can make a list of colors for each data point, then pass a dictionary of this colors list to the marker parameter.您的问题有点不清楚,但如果我理解正确,您可以为每个数据点列出 colors 列表,然后将此 colors 列表的字典传递给marker参数。

import plotly.graph_objects as go

y = [8510000, 27525000, 20532000, 12897000, 
27858000, 5266000, 18442000, 78850000, 43813000, 26920000, 4501000, 19619000, 12938000]
# colors list with first 4 set to 'black', last 9 set to 'red'
colors = ['black']*4 + ['red']*9

data = [dict(type='bar',
    y=y,
    marker=dict(color=colors)
)]

fig = go.Figure(data=data)
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM