My Plotly Note

ifeelfree
1 min readFeb 25, 2021

--

Part 1: Multiple Plots

Using make_subplots takes the following steps:

(1) generate a figure via make_subplots function

(2) add trace via add_trace function

(3) update_layout is used to reset the image setting

Notes on make_subplots

  • specs is an important parameter in make_subplots , and it will determine the type of plots we want to have, for example, if we want to have pie plot, then we should write specs=[[{'type':'pie'}]]
  • subplot_titles is used to denote the title of each subplots
  • we can also disable shared xaxes title function by calling shared_xaxes=False ; then use fig.update_xaxesto set individual xaxes titles.

Part 2: Basic Plot Element

Part 2.1: Pie

go.Pie

  • pull is used to emphasize some parts of the pie plot see this example
  • how to make sure that several pies in different HTMLs share the same color scheme
color_dict = {'A': px.colors.qualitative.G10[1], 
'B': px.colors.qualitative.G10[5],
'C': px.colors.qualitative.G10[2],
'D': px.colors.qualitative.G10[0],
'E': px.colors.qualitative.G10[7]}

color_list = np.array([''] * len(tmp_dict['type']), dtype = object)
for index, type in enumerate(tmp_dict['type']):
color_list[index] = color_dict[type]
fig.add_trace(go.Pie(labels=tmp_dict['AB'],
values=tmp_dict['CD'],
pull=[DetectionEnum.indicator(ele) for ele in tmp_dict['type']],
marker={'colors':color_list}
)

see color sequence in plotly express to check the default color maps provided by plotly

Part 3: Plotly Express

Part 3.1 Change the label

Axis titles (and legend titles) can also be overridden using the labels argument of Plotly Express functions:

import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="sex",
labels=dict(total_bill="Total Bill ($)", tip="Tip ($)", sex="Payer Gender")
)
fig.show()

see https://plotly.com/python/axes/

Reference

--

--

No responses yet