Search code examples
pythonmatplotlibgraphing

How to change axes labels to multiples of pi


I am displaying the distribution of data points which I have transformed to polar coordinates, and am displaying the distribution of points using a histogram. How do I change the x axes to be in multiples of pi?

import numpy as np
import matplotlib.pyplot as plt


Phi_coords = []

# Extracting phi values from polars
for i in range(len(test_polars)):
    Phi_coords.append(test_polars[i][1])

    
# matplotlib histogram
plt.hist(Phi_coords, color = 'blue', edgecolor = 'black', bins = 360)


# Add labels
plt.title('Distribution')
plt.xlabel('Phi Coordinate')
plt.ylabel('Number at Phi Coordinate')

current plot


Solution

  • Needed to add:

    pi = np.pi
    
    plt.xticks(np.arange(-pi, pi+pi/2, step=(pi/2)), ['-π','-π/2','0','π/2','π'])
    

    New plot