Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create minor ticks for polar plot matplotlib

I am interested in the following two things for the polar plot plotted by matplotlib shown below

  1. How do I create minor ticks for a polar plot on the r axis?
  2. How do I move the r labels further away from the r ticks, as seen in the graph, some of the r ticks are in contact with the axis.

enter image description here

like image 859
Tian Avatar asked Jun 20 '17 15:06

Tian


People also ask

How do I make a minor tick in Matplotlib?

Minor tick labels can be turned on by setting the minor formatter. MultipleLocator places ticks on multiples of some base. StrMethodFormatter uses a format string (e.g., '{x:d}' or '{x:1.2f}' or '{x:1.1f} cm' ) to format the tick labels (the variable in the format string must be 'x' ).

How do I reduce the number of ticks in Matplotlib?

Method 3: Using xlim() Still, if we want only the first three values of the x-axis to visualize, you can use this xlim() and ylim() function to set a limit for the x-axis and y-axis of the plot and then use the locate param to reduce the number of ticks.

How do I make ticks longer in Matplotlib?

To make longer subplot tick marks in matplotlib, we can use tick_params() method for minor and major ticks length and width.

How do I change the spacing between ticks in Matplotlib?

MatPlotLib with Python Create a figure and add a set of subplots. To set the ticks on a fixed position, create two lists with some values. Use set_yticks and set_xticks methods to set the ticks on the axes. To display the figure, use show() method.


1 Answers

The polar plot does not have minor or major ticks. So I think you need to create the minor ticks manually by plotting small line segments.

For example:

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rmax(2)
ax.margins(y=0)
ax.set_rticks([0.5, 1, 1.5, 2])  # less radial ticks
ax.set_rlabel_position(120)  # get radial labels away from plotted line

ax.grid(True)

tick = [ax.get_rmax(),ax.get_rmax()*0.97]
for t  in np.deg2rad(np.arange(0,360,5)):
    ax.plot([t,t], tick, lw=0.72, color="k")

ax.set_title("A line plot on a polar axis", va='bottom')
plt.show()

enter image description here

like image 128
ImportanceOfBeingErnest Avatar answered Oct 17 '22 00:10

ImportanceOfBeingErnest