Let’s discuss.
Function Syntax and Parameters
Although the function provides a large parameter list, for simplicity, we will outline the most useful parameters you will use.
Hence, we can reduce the function syntax as shown:
template=None, width=None, height=N0ne)
The parameters above are explained as follows:
- data_frame – specifies the data frame containing the column names used in the plot.
- r – specifies the values used to position the marks along the radial axis of the polar coordinate.
- theta – similar to r, but the values are used to place the marks along the angular axis of the polar coordinate.
- color – sets the values used to assign a unique color to the marks of the plot.
- title – sets the title for the figure.
- template – defines the template used for the plot.
- width/height – sets the width and height of the figure in pixels.
Example
The example code below shows how to create a bar polar plot.
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction')
fig.show()
The code above should create a bar polar plot from the provided wind data, as shown in the resulting figure:
We can also set the color for each bar based on the wind direction, as shown:
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction', color='direction')
fig.show()
Output:
We can also specify another column within the data frame as:
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction', color='frequency')
fig.show()
The output figure:
To set a dark background for the plot, you can use the template parameter as shown:
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction', color='frequency', template='plotly_dark')
fig.show()
This should return a figure as shown:
To change the bar plot colors, use the color_discrete_sequence parameter:
color_discrete_sequence=px.colors.sequential.Plasma_r)
Check the Plotly colors documentation to learn more.
Closing
That’s it for this one. In this short article, we discussed how you can create polar bar plots using Plotly express.