본문 바로가기

Data Science/Visualization

[plotly] Layout components of plotly

1. Updating or Modfying Figures mad with Plotly Express

If none of built-in plotly arguments allow us to customize the figure the way we need to, we can use the update_* and add_* methods on the plotly.graph objects.Figure object returned by the PX function to make any further modifications to the figure.

 

2. Usecase of those methods

import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="day", y="total_bill", color="sex",
            title="Receipts by Payer Gender and Day of Week vs Target",
            width=600, height=400,
            labels={"sex": "Payer Gender",  "day": "Day of Week", "total_bill": "Receipts"},
            category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "sex": ["Male", "Female"]},
            color_discrete_map={"Male": "RebeccaPurple", "Female": "MediumPurple"},
            template="simple_white"
            )

fig.update_yaxes( # the y-axis is in dollars
    tickprefix="$", showgrid=True
)

fig.update_layout( # customize font and legend orientation & position
    font_family="Rockwell",
    legend=dict(
        title=None, orientation="h", y=1, yanchor="bottom", x=0.5, xanchor="center"
    )
)

fig.add_shape( # add a horizontal "target" line
    type="line", line_color="salmon", line_width=3, opacity=1, line_dash="dot",
    x0=0, x1=1, xref="paper", y0=950, y1=950, yref="y"
)

fig.add_annotation( # add a text callout with arrow
    text="below target!", x="Fri", y=400, arrowhead=1, showarrow=True
)

fig.show()

 

3. My frequently used format of layout and annotation

# layout 
fig.update_layout(
    {
        "title": {
            "text": "<b>Percentage or heart rate by Age decades and sex</b>",
            "x": 0.5,
            "y": 0.9,
            "font": {
                "size": 15
            }
        },
        "xaxis": {
            "title": "Age Decades",
            "showticklabels":True,
            "tickfont": {
                "size": 10                
            }
        },
        "yaxis": {
            "title": "Percentage of HeartDisease",
            "tickfont": {
                "size": 10                
            }
        },
        "template":'plotly_white'
    }
)

# annotation 
fig.add_annotation(
            x="2017-11-30",
            y=1153393,
            text="<b>Peaked Monthly Turnover</b>",
            showarrow=True,
            font=dict(
                size=10,
                color="#ffffff"
                ),
            align="center",
            arrowhead=2,
            arrowsize=1,
            arrowwidth=2,
            arrowcolor="#77CFD9",
            ax=20,
            ay=-30,
            bordercolor="#77CFD9",
            borderwidth=2,
            borderpad=4,
            bgcolor="#F25D50",
            opacity=0.9
)

 

 

Source from : https://plotly.com/python/styling-plotly-express/ 

'Data Science > Visualization' 카테고리의 다른 글

[plotly] Graph Objects in Python  (0) 2022.09.18
[plotly] Plotly Express in Python  (0) 2022.09.18
[plotly] iplot in Python  (0) 2022.09.18
[plotly] Getting Started with Plotly in Python  (0) 2022.09.18