Matplotlib – Radian Ticks


Matplotlib – Radian Ticks



”;


A radian is a unit of angular measure used to express angles in mathematics and physics. Ticks, in the field of data visualization, refer to the small lines or marks indicating the scale of the axes.

Radian Ticks in Matplotlib

In the context of Matplotlib, radian ticks typically denote the ticks or markers on an axis that represent values in radians. When dealing with circular or angular data, it is common to use radian ticks on the axis to indicate specific angles. Setting radian ticks involves placing tick marks at specific intervals corresponding to certain radian values on the axis.

Here is a reference image illustrating Radian ticks on a plot −


Radian_Ticks_Intro

In the image, you can observe that radian ticks represent the angles of the sine wave along the x-axis.

Radian Ticks for the x axis

Setting Radian Ticks for the x-axis involves using two key methods which are axes.set_xticks() and axes.set_xticklabels(). These methods are allowing you to specify the positions and labels of ticks on the x-axis, respectively.

In addition to these methods, the FormatStrFormatter and MultipleLocator classes from the matplotlib.ticker module can be used to enhance the customization of tick positions and labels.

Example 1

The following example demonstrates how to create a plot with custom radian ticks.


import matplotlib.pyplot as plt
import numpy as np

# Create plot
fig, ax = plt.subplots(figsize=(7, 4))

# Sample data
theta = np.linspace(0, 2 * np.pi, 100)
y = np.sin(theta)

# Plotting the data
plt.plot(theta, y)
plt.title(''Sine Wave'')
plt.xlabel(''Angle (radians)'')
plt.ylabel(''Y-axis'')

# Custom radian ticks and tick labels
custom_ticks = [0, np.pi/2, np.pi, (3*np.pi)/2, 2*np.pi]
custom_tick_labels = [''$0$'', ''$pi/2$'', ''$pi$'', ''$3pi/2$'', ''$2pi$'']

ax.set_xticks(custom_ticks)
ax.set_xticklabels(custom_tick_labels)

plt.grid(axis=''x'')

# Display the plot
plt.show()

Output

On executing the above code we will get the following output −


radian_ticks_ex1

Example 2

This example uses the FormatStrFormatter and MultipleLocator classes from the matplotlib.ticker module to control the format and positions of ticks.


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

# Create Plot
f,ax=plt.subplots(figsize=(7,4))

# Sample Data
x=np.linspace(0, 2 * np.pi, 100)
y=np.sin(x)

# Plot the sine wave
ax.plot(x/np.pi,y)

# Customizing X-axis Ticks
ax.xaxis.set_major_formatter(tck.FormatStrFormatter(''%g $pi$''))
ax.xaxis.set_major_locator(tck.MultipleLocator(base=1.0))

# Set the titles
plt.title(''Sine Wave'')
plt.xlabel(''Angle (radians)'')
plt.ylabel(''Y-axis'')

plt.grid()
plt.show()

Output

On executing the above code we will get the following output −


radian_ticks_ex2

Radian Ticks for the y axis

Similar to the x-axis, setting radian ticks for the y-axis can be done by using the ax.set_yticks() and ax.set_yticklabels() methods. These methods allow you to define the positions and labels of ticks on the y-axis. Additionally, the FormatStrFormatter and MultipleLocator classes from the matplotlib.ticker module can also used.

Example

This example demonstrates how to set customized radian ticks for the y-axis. using the FormatStrFormatter and MultipleLocator classes from the matplotlib.ticker module.


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

# Create Plot
f,ax=plt.subplots(figsize=(7,4))

# Sample Data
x=np.arange(-10.0,10.0,0.1)
y=np.arctan(x)

# Plot the data
ax.plot(x/np.pi,y)

# Customizing y-axis Ticks
ax.yaxis.set_major_formatter(tck.FormatStrFormatter(''%g $pi$''))
ax.yaxis.set_major_locator(tck.MultipleLocator(base=0.5))

plt.grid()
plt.show()

Output

On executing the above code we will get the following output −


radian_ticks_ex3

Custom Package for Radian Ticks

A custom package named “basic_units.py” can denote the tick marks using the radians. This package is not part of the standard or widely recognized packages and needs to be downloaded separately(available in the examples folder of Matplotlib).

Example

This example demonstrates how to create a plot with radians using the basic_units mockup example package.


import matplotlib.pyplot as plt
from basic_units import radians
import numpy as np

# Create Plot
f,ax=plt.subplots(figsize=(7,4))

x = np.arange(-10.0,10.0,0.1)
y = list(map(lambda y: y*radians,np.arctan(x)))
x = list(map(lambda x: x*radians,x))

ax.plot(x,y,''b.'')
plt.xlabel(''radians'')
plt.ylabel(''radians'')
plt.show()

Output

On executing the above code we will get the following output −


radian_ticks_ex4

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *