How to fix range of axis in plot without specifying a number

Viewed 41

I'm doing a polar plot with matplotlib and I have almost everything ready by now. The problem is that I have several datafiles and it would be convenient to get the axis limits automatically instead of me having to set it each time. So the code is like this

def rose_diagram(x, y, N):
# Compute pie slices
theta = 2 * np.pi * x / N
radii = y 
width = np.pi / 24     

ax = plt.subplot(projection='polar')
ax.bar(theta, radii, width=width, bottom=0.0, color=colors, alpha=0.9)

# Format x axis
ax.set_xticks((np.linspace(0,2*np.pi,24,endpoint=False)))
ax.set_xticklabels([xlabels])

# Format y axis
max_r = y.max() + ((y.max()-y.min())/10)
ax.set_yticks((0,max_r/2,max_r))
yticks = ax.yaxis.get_major_ticks()
yticks[0].set_visible(False)

# Add nighttime
night_theta = 55*np.pi/180
night_width = 145*np.pi/180
ax.bar(night_theta,max_r,night_width, bottom=0.0, color='gray', alpha=0.3)

plt.show()

The problem is that when I add the "nighttime the axis rescales and i get this graph polar plot

and what I want is that the nighttimebar goes until the end of the polar plot instead of it increasing the size of the axis.

1 Answers

If I understood the question correctly you can add ylim = ax.get_ylim() after you make your non-nighttime bars. This saves the default radial axis limits that result from just drawing the bars. Then at the end, add ax.set_ylim(ylim) to revert to those saved axis limits (that were there before you added nighttime).

Related