Plotly Scatterpolar图表:如何自定义Theta轴的范围?
Problem Description
I'm using Plotly's scatterpolar chart to visualize multiple datasets. Here's a snippet of my code:
Creating the scatterpolar trace:
go.Scatterpolar( r=[dataset_dataframe['word_count'].median(), dataset_dataframe['char_count'].median(), dataset_dataframe['capitals'].median(), dataset_dataframe['num_exclamation_marks'].median(), dataset_dataframe['num_punctuation'].median()], name=dataset_name, theta=['No. of Words', 'No. of Characters', 'No. of Capitals', 'No. of Exclamation Marks', 'No. of Punctuations'], fill='toself', line=dict(color='brown'), subplot=subplot_name )
Layout configuration:
fig.update_layout( polar=dict( radialaxis=dict( visible=True, ) ), title='Dataset Statistics' )
This setup lets me compare multiple datasets, but I noticed the radial axis (r-axis) range is automatically calculated based on the maximum r-value of each scatterpolar trace. This makes it hard to compare datasets side by side. How can I set a custom fixed range (like 1 to 100) for the radial axis?
Solution
Hey, this is a quick fix! You just need to add the range parameter to the radialaxis configuration in your layout.
Basic Single Polar Plot
Modify your layout code to explicitly define the radial axis range:
fig.update_layout( polar=dict( radialaxis=dict( visible=True, range=[1, 100] # Your custom min and max values here ) ), title='Dataset Statistics' )
For Subplots
If you're using multiple polar subplots (like polar1, polar2, etc.), you'll need to set the range for each subplot's radial axis individually to keep them consistent:
fig.update_layout( polar1=dict( radialaxis=dict( visible=True, range=[1, 100] ) ), polar2=dict( radialaxis=dict( visible=True, range=[1, 100] ) ), title='Dataset Statistics' )
This will lock all radial axes to your specified 1-100 range, making it much easier to compare metrics across different datasets at a glance.
内容的提问来源于stack exchange,提问作者Aventinus

