Python Numpy 2D plot set total number of y-tics with autoscaling -
i limit total number of tics on y-axis in 2d plot. in example image provided can see f_x
plot has 6 total tics (0,0.2,0.4,0.6,0.8,1) while f_y
plot has 8. there way still have axis scale automatically fix number of total tics plots greater 6 tics in example busy?
ax.locator_params(axis='y', nbins=num)
easiest way this.
nbins
set maximum number of bins (i.e. spaces between ticks) each axis. "even" numbers still chosen, value controls density of ticks on axis. default value new axes 9 (in other words, maximum of 10 ticks/ticklabels).
for example, let's set rather busy default:
import matplotlib.pyplot plt import numpy np np.random.seed(1977) # generate data different ranges x = np.linspace(0, 8.8, 1000) ydata = np.random.normal(0, 1, (4, x.size)).cumsum(axis=1) ydata *= np.array([1e-3, 1e3, 10, 1e-2])[:,none] fig, axes = plt.subplots(nrows=4) y, ax in zip(ydata, axes): ax.plot(x, y, color='salmon') plt.show()
oy!! not good! let's see if can better:
fig, axes = plt.subplots(nrows=4) y, ax in zip(ydata, axes): ax.plot(x, y, color='salmon') ax.locator_params(axis='y', nbins=5) plt.show()
getting there, still bit busy. reduce nbins
further, we'll wind no ticks. instead, 1 trick use "prune" first , last ticks axes. can controlled locator_params
:
fig, axes = plt.subplots(nrows=4) y, ax in zip(ydata, axes): ax.plot(x, y, color='salmon') ax.locator_params(axis='y', nbins=5, prune='both') plt.show()
this "pruning" more effective when combined shared x-axes, in case of type of plot you're making. main effect post turn off of x-ticklabels. however, link interactive zooming , panning of axes x-range shared:
fig, axes = plt.subplots(nrows=4, sharex=true) y, ax in zip(ydata, axes): ax.plot(x, y, color='salmon') ax.locator_params(axis='y', nbins=5, prune='both') plt.show()
now can move things bit closer together:
fig, axes = plt.subplots(nrows=4, sharex=true) y, ax in zip(ydata, axes): ax.plot(x, y, color='salmon') ax.locator_params(axis='y', nbins=5, prune='both') fig.subplots_adjust(hspace=0) plt.show()
finally, in particular case, might consider using ax.margins(...)
add padding in y-direction , force "tight" scaling of data range in x-direction.
fig, axes = plt.subplots(nrows=4, sharex=true) y, ax in zip(ydata, axes): ax.plot(x, y, color='salmon') ax.margins(x=0, y=0.05) ax.locator_params(axis='y', nbins=5, prune='both') fig.subplots_adjust(hspace=0) plt.show()
Comments
Post a Comment