首页 > 解决方案 > 就 ax.spines 而言,set_position('center') 和 set_position('zero') 有什么区别

问题描述

我正在尝试绘制这样的情节

我研究了来自matplotlib的示例。

我所知道的是

  1. 左脊柱可以在笛卡尔坐标平面中扮演 y 轴的角色。

  2. 底部脊柱可以在笛卡尔坐标平面中扮演 x 轴的角色。

  3. set_position()可以移动刺。

我的问题是将脊椎(例如,“左”)放在“中心”和“零”之间有什么区别。

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2 * np.sin(x)

ax = fig.add_subplot(2, 2, 1)
ax.set_title('centered spines')
ax.plot(x, y)
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

ax = fig.add_subplot(2, 2, 2)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.show()

标签: pythonmatplotlib

解决方案


推荐阅读