瀑布的颜色和大熊猫的iloc格式

问题描述 投票:0回答:1

我使用Plotly创建了一个瀑布,从excel中创建了两个数据框。我可以将数据框中的数字格式改为3位小数,但当我使用iloc时就不行了。有什么方法可以做到这一点吗?

此外,我找不到答案,是否可以改变。

  • 条形图的个别颜色
  • 背景色
  • y轴刻度和格式

我在下面添加了一个例子。

import plotly.express as px
import plotly.graph_objects as go

start = 0.143
ap = 0.02
impairments = 0.03
PVA = -0.02
other =0.04
rwa = 0.02
end = 0.146

fig = go.Figure(go.Waterfall(
name = "legend", orientation = "v",
measure = ["relative", "relative", "relative", "relative", "relative","relative","total"],
x = ["Start", "AP", "Impairments", "PVA", "Other", "RWA", "Final point"],
textposition = "outside",
text = [start*100, ap*100, impairments*100,PVA*100, other*100, rwa*100, end*100],
y = [start, ap, impairments, PVA, other, rwa, end], 
connector = {"line":{"color":"rgb(63, 63, 63)"}}, )) 
fig.update_layout( 
title = "CET1 movements",
showlegend = False ) 
fig.show()

谢谢你

python pandas plotly
1个回答
0
投票

我想我知道了你要找的大部分东西。我评论了你要求的那部分代码,并格式化了一点你的代码。

import plotly.graph_objects as go
import pandas as pd
import numpy as np

x = ["Start", "AP", "Impairments", "PVA", "Other", "RWA", "Final point"]
y = [0.143, 0.02, 0.03,-0.02, 0.04, 0.02, 0.146]

df = pd.DataFrame({"x":x,
                   "y":y})

# here you better work with string
df["text"] = (df["y"]*100).apply(lambda x: '{:,.3f}'.format(x))

# to lazy to write all possible cases
df["measure"] = np.where(df["x"]=="Final point", "total", "relative")
# as alternative
# df["measure"] = ["relative"]*(len(df)-1) + ["total"]

fig = go.Figure()
fig.add_trace(
    go.Waterfall(x=df["x"],
                 y=df["y"],
                 text=df["text"],
                 measure=df["measure"],
                 textposition = "outside",
                 orientation = "v",
                 connector = {"line":{"color":"rgb(63, 63, 63)"}},
                 # set colors for different types of bars
                 # see https://plotly.com/python/waterfall-charts/#setting-marker-size-and-color
                 decreasing = {"marker":{"color":"Maroon",
                                         "line":{"color":"red", 
                                                 "width":2}}},
                 increasing = {"marker":{"color":"Teal"}},
                 totals = {"marker":{"color":"orange", 
                                     "line":{"color":'blue', 
                                             "width":3}}}


                ))

# with this you can change the yaxes range
fig.update_yaxes(range=[0, 0.3])
fig.update_layout(title="CET1 movements",
                  title_x=0.5,
                  # background color
                  plot_bgcolor="lightgrey")
fig.show()

enter image description here

我觉得我做的颜色选择是可怕的,但随后你可以在它的工作。

© www.soinside.com 2019 - 2024. All rights reserved.