I have a code that generates a simple donut chart (technically, a pie chart) using Plotly
.
import plotly.graph_objects as go
def main():
labels = ["Yale blue", "Lemon chiffon",
"Naples yellow", "Sandy brown", "Tomato"]
values = [14, 15, 15, 18, 19]
colors = ["#0D3B66", "#FAF0CA", "#F4D35E", "#EE964B", "#F95738"]
fig = go.Figure(data=[go.Pie(labels=labels,
values=values,
hole=.7,
direction="clockwise",
sort=False)])
fig.update_traces(marker=dict(colors=colors))
fig.show()
if __name__ == "__main__":
main()
For some reason, I want to add 12 circular labels to the plot: the first, located at 0:00, would say '1', the second, located at 1:00, would say '2' and so on. I would like to see something along those lines:
def add_circular_labels(fig: go.Figure,
labels: List[str]:
...
add_circular_labels(fig, ["1", "2", ...])
How could I achieve that?
An easy way would be to superimpose an empty polar plot on your pie chart. Then settings the ticks/grid as follows:
import plotly.graph_objects as go
labels = ["Yale blue", "Lemon chiffon",
"Naples yellow", "Sandy brown", "Tomato"]
values = [14, 15, 15, 18, 19]
colors = ["#0D3B66", "#FAF0CA", "#F4D35E", "#EE964B", "#F95738"]
fig = go.Figure(data=[go.Pie(labels=labels,
values=values,
hole=.7,
direction="clockwise",
sort=False)])
fig.update_traces(marker=dict(colors=colors))
def add_circular_labels(fig: go.Figure, labels: list):
fig.add_trace(go.Scatterpolar())
fig.update_layout(
polar = dict(
radialaxis_visible=False,
angularaxis = dict(
type="category",
categoryarray=labels,
direction="clockwise",
#rotation=90,
showticklabels=True,
ticks='', showgrid=False)
)
)
add_circular_labels(fig, list(range(1, 13)))
fig.show()
See the doc for details about layout.polar
.