Цветные водопады и формат pandas ilo c - PullRequest
1 голос
/ 22 апреля 2020

Я создал водопад, используя Plotly, создав 2 кадра данных из Excel. Я могу изменить формат чисел на 3 десятичных знака для кадров данных, но не тогда, когда я использую ilo c. Есть ли способ сделать это?

Кроме того, я не могу найти ответ о том, можно ли изменить:

  • отдельный цвет полос
  • фон цвет
  • масштаб оси 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()

Большое спасибо

Ответы [ 2 ]

0 голосов
/ 04 мая 2020
import plotly.graph_objects as go

fig = go.Figure(go.Waterfall(
    x = [["2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018"],
       ["initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total"]],
    measure = ["absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total"],
    y = [10, 20, 30, -10, None, 10, 20, -40, None], base = 300,
    decreasing = {"marker":{"color":"Maroon", "line":{"color":"red", "width":2}}},
    increasing = {"marker":{"color":"Teal"}},
    totals = {"marker":{"color":"deep sky blue", "line":{"color":'blue', "width":3}}}
))

fig.update_layout(title = "Profit and loss statement", waterfallgap = 0.3)

fig.show()
0 голосов
/ 24 апреля 2020

Я думаю, что выяснил большинство вещей, которые вы искали. Я прокомментировал ту часть кода, которую вы просили, и немного отформатировал ваш код.

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

Я думаю, что выбор цвета, который я сделал, ужасен но тогда вы можете работать над этим.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...