How to create small ticks for a matplotlib polar plot

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

  • How do I create minor ticks for a polar chart on the r axis?
  • How to move the r marks away from the r ticks, as seen in the chart, some of the r ticks are in contact with the axis.

enter image description here

+3


source to share


2 answers


The polar chart has no major or minor ticks. So I think you need to create small ticks manually by plotting small 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

+1


source


For your first question, you can either increase the number of ticks that you don't think you need if you want to use small ticks, or you can manually generate ticks yourself. To do this, you will need to use your own polar axis plot objects to plot these ticks, i.e.

ax.plot([theta_start, theta_end], [radius_start, radius_end], kwargs**)

      

You need to figure out the interval in which you want these ticks and then mark them manually with a function like the one below.

def minor_tick_gen(polar_axes, tick_depth, tick_degree_interval, **kwargs):
    for theta in np.deg2rad(range(0, 360, tick_degree_interval)):
        polar_axes.plot([theta, theta], [polar_axes.get_rmax(), polar_axes.get_rmax()-tick_depth], **kwargs)

      

which you can call like this:



minor_tick_gen(ax, 0.25, 20, color = "black")

      

It's hard to find, but the polar axis is not normal axes, but instances of the Polar Axis class. In the documentation you can use set_ylim(min, max)

which will allow you to move the line labels, however this will change the scale of the entire graph. Going beyond the graph will require the developer's knowledge of the structure, because matplotlib does not provide this function for you. For example, using set_rgrids(...)

, even with a component position will not affect the relative positioning of the labels.

Combining these things, you can use the following code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import math


def minor_tick_gen(polar_axes, tick_depth, tick_degree_interval, **kwargs):
    for theta in np.deg2rad(range(0, 360, tick_degree_interval)):
        polar_axes.plot([theta, theta], [polar_axes.get_rmax(), polar_axes.get_rmax()-tick_depth], **kwargs)

def radian_function(x, y =None):
    rad_x = x/math.pi
    return "{}π".format(str(rad_x if rad_x % 1 else int(rad_x)))

ax = plt.subplot(111, projection='polar')
ax.set_rmax(2)
ax.set_rticks([3*math.pi, 6*math.pi, 9*math.pi, 12*math.pi])
ax.set_rlabel_position(112.5)
# go slightly beyond max value for ticks to solve second problem
ax.set_ylim(0, 13*math.pi)
ax.grid(True)
# generate ticks for first problem
minor_tick_gen(ax, math.pi, 20, color = "black", lw = 0.5)
ax.set_title("Polar axis label minor tick example", va='bottom')
ax.yaxis.set_major_formatter(ticker.FuncFormatter(radian_function))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(radian_function))
plt.show()

      

to get the next image enter image description here

0


source







All Articles