Plotly bar chart - change color based on positive/negative value - python(Plotly条形图-根据正/负值更改颜色-python)
                            本文介绍了Plotly条形图-根据正/负值更改颜色-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我有以下代码绘制条形图(1 系列),但如果净"值为正值,我需要将条形着色为蓝色,如果为负值,则将条形着色为红色:
将 pandas 导入为 pd导入 plotly.graph_objectsdf = pd.DataFrame({'网':[15,20,-10,-15],'日期':['07/14/2020','07/15/2020','07/16/2020','07/17/2020']})df['Date'] = pd.to_datetime(df['Date'])fig = go.Figure(data=[go.Bar(name='Net', x=df['Date'], y=df['Net'])])fig.update_layout(barmode='stack')图.show() 解决方案 
你可以查看文档
I have the following code which plots a bar chart (1 series), but I need the bars to be coloured blue if the 'Net' value is positive, and red if its negative:
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({
     'Net':[15,20,-10,-15], 
     'Date':['07/14/2020','07/15/2020','07/16/2020','07/17/2020']
})
df['Date'] = pd.to_datetime(df['Date'])
fig = go.Figure(data=[go.Bar(name='Net', x=df['Date'], y=df['Net'])])
fig.update_layout(barmode='stack')
fig.show()
 解决方案 
You can check documentation here. Full code as following
import pandas as pd
import plotly.graph_objects as go
import numpy as np
# Data
df = pd.DataFrame({
     'Net':[15,20,-10,-15], 
     'Date':['07/14/2020','07/15/2020','07/16/2020','07/17/2020']
})
df['Date'] = pd.to_datetime(df['Date'])
## here I'm adding a column with colors
df["Color"] = np.where(df["Net"]<0, 'red', 'green')
# Plot
fig = go.Figure()
fig.add_trace(
    go.Bar(name='Net',
           x=df['Date'],
           y=df['Net'],
           marker_color=df['Color']))
fig.update_layout(barmode='stack')
fig.show()
这篇关于Plotly条形图-根据正/负值更改颜色-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:Plotly条形图-根据正/负值更改颜色-python
 
				
         
 
            
        基础教程推荐
             猜你喜欢
        
	     - 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				