Reputation: 980
could anyone guide me in how do I put a vertical axis beside a polar plot using matplotlib?
Quoting an example from http://www.originlab.com/doc/Origin-Help/Polar-Graph, of the desired outcome.
As shown in the diagram, on the left is the desired vertical bar in my polar plot which I would like to reproduce in matplotlib:
EDIT: This is an example of the code that I would want to add the vertical axis to.
import matplotlib.pyplot as plt
import numpy as np
def sin_func(array):
final = np.array([])
for value in array:
final = np.append(final, abs(np.sin(value)))
return final
x = np.arange(0, 4*np.pi, 0.1)
y = sin_func(x)
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
plt.plot(x, y)
# Changing axis to pi scale
ax.set_ylim([0, 1.2])
x_tick = np.arange(0, 2, 0.25)
x_label = [r"$" + format(r, '.2g') + r"\pi$" for r in x_tick]
ax.set_xticks(x_tick*np.pi)
ax.set_xticklabels(x_label, fontsize=10)
ax.set_rlabel_position(110)
plt.show()
Upvotes: 2
Views: 1172
Reputation: 36705
Add additional axis with add_axes method in position your want and then set tick positions and labels as you want:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator
def sin_func(array):
final = np.array([])
for value in array:
final = np.append(final, abs(np.sin(value)))
return final
x = np.arange(0, 4*np.pi, 0.1)
y = sin_func(x)
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
plt.plot(x, y)
# Changing axis to pi scale
ax.set_ylim([0, 1.2])
x_tick = np.arange(0, 2, 0.25)
x_label = [r"$" + format(r, '.2g') + r"\pi$" for r in x_tick]
ax.set_xticks(x_tick*np.pi)
ax.set_xticklabels(x_label, fontsize=10)
ax.set_rlabel_position(110)
# Add Cartesian axes
ax2 = fig.add_axes((.1,.1,.0,.8))
ax2.xaxis.set_visible(False) # hide x axis
ax2.set_yticks(np.linspace(0,1,7)) # set new tick positions
ax2.set_yticklabels(['60 %','40 %', '20 %', '0 %', '20 %', '40 %', '60 %'])
ax2.yaxis.set_minor_locator(AutoMinorLocator(2)) # set minor tick for every second tick
plt.show()
Upvotes: 3