搜索
您的当前位置:首页正文

Matplotlib之坐标轴设置

来源:哗拓教育

通常软件绘图,包括 matlab、python 的 matplotlib,默认都是将坐标轴置于画布(figure)的最下侧(x 轴),最左侧(y 轴),也即将坐标原点置于左下角。

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(1, 1, 1)
ax.set_title('centered spines')
ax.plot(x, y)

# 左右下上4个轴

# 设置轴的位置
ax.spines['left'].set_position('center')
# 设置轴的颜色
ax.spines['right'].set_color('none')
# 设置轴的位置
ax.spines['bottom'].set_position('center')
# 设置轴的颜色
ax.spines['top'].set_color('none')

plt.show()

效果:


Top