plotly

Plotly.express.line_polar

Polar charts are types of plots that visualize data along a radial and angular axis. Using plotly express, you can represent polar data as lines using the line_polar() function.

Function Syntax and Parameter

The function syntax and parameter list are as shown:

lotly.express.line_polar(data_frame=None, r=None, theta=None, color=None, line_dash=None, hover_name=None, hover_data=None, custom_data=None, line_group=None, text=None, symbol=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, color_discrete_sequence=None, color_discrete_map=None, line_dash_sequence=None, line_dash_map=None, symbol_sequence=None, symbol_map=None, markers=False, direction='clockwise', start_angle=90, line_close=False, line_shape=None, render_mode='auto', range_r=None, range_theta=None, log_r=False, title=None, template=None, width=None, height=None)

 
The following are some of the important function parameters you will need to know:

    1. data_frame – specifies the data_frame containing the column names used in the plot.
    2. r – sets the values used to position the marks along the radial axis in the polar coordinate.
    3. theta – sets the values used to position the marks along the angular axis on the polar coordinate.
    4. color – specifies the values used to assign a unique color to the marks.
    5. line_dash – specifies the values used to assign dash patterns to the lines.
    6. start_angle – sets the start angle for the angular axis.
    7. line_shape – specifies the line shape either linear or spline.
    8. title – sets the title for the figure.
    9. width/height – defines the width and height of the figure in pixels.

Example

The example below illustrates how to create a line polar plot.

 import plotly.express as px
df = px.data.wind()
fig = px.line_polar(df, r='frequency', theta='direction')
fig.show()

 
The code above should return a figure as shown:


You can also specify the color of the lines by setting the color parameter to a column within the DataFrame.

import plotly.express as px
df = px.data.wind()
fig = px.line_polar(df, r='frequency', theta='direction', color='direction')
fig.show()

 
The code above should assign a unique color to the lines based on the wind direction. The resulting figure is shown below:


To change the background and color sequence of the plot, you can use the color_discrete_sequence parameter as shown in the code below:

import plotly.express as px
df = px.data.wind()
fig = px.line_polar(df, r='frequency', theta='direction', color='direction', color_discrete_sequence=px.colors.sequential.Blackbody_r)
fig.show()

 
The resulting figure:


Set a dark background using the template parameter:

import plotly.express as px
df = px.data.wind()
fig = px.line_polar(df, r='frequency', theta='direction', color='direction', color_discrete_sequence=px.colors.sequential.Pinkyl_r, template='plotly_dark')
fig.show()

 
Output:


To set the title for your Polar plot:

fig = px.line_polar(df, r='frequency', theta='direction', color='direction', color_discrete_sequence=px.colors.sequential.Pinkyl_r, title='Wind Polar Plot')
fig.show()

 
Output:


That’s it for this tutorial.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list