
上QQ阅读APP看书,第一时间看更新
How to do it...
The following code block displays six plots with the same data, but with different options for spines:
- Set up the data for the plot:
theta = np.linspace(0, 2*np.pi, 128)
y = np.sin(theta)
- Define the figure with its size:
fig = plt.figure(figsize=(8,6))
- Define the first axes and plot the sin wave with default spines:
ax1 = fig.add_subplot(2, 3, 1)
ax1.plot(theta, np.sin(theta), 'b-*')
ax1.set_title('default spines')
- Define the function to plot a graph:
def plot_graph(axs, title, lposition, bposition):
ax = fig.add_subplot(axs)
ax.plot(theta, y, 'b-*')
ax.set_title(title)
ax.spines['left'].set_position(lposition)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position(bposition)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
- Plot five graphs with different position of spines:
plot_graph(232, 'centered spines', 'center', 'center')
plot_graph(233, 'zeroed spines', 'zero', 'zero')
plot_graph(234, 'spines at axes [0.25, 0.75]', ('axes', 0.25),
('axes', 0.75))
plot_graph(235, 'spines at data [1.0, -1.0]', ('data', 1.0),
('data', -1.0))
plot_graph(236, 'adjusted spines', ('outward', 10), ('outward', 10))
- Adjust space in between the plots and display the figure on the screen:
plt.tight_layout()
plt.show()