首页 > 编程知识 正文

pythonmatplotlib怎么设置坐标,matplotlib指南

时间:2023-05-05 16:11:16 阅读:228533 作者:3801

不显示右侧和顶端的坐标轴是通过spines实现的,具体操作如下:

# 隐藏右侧和顶端的坐标轴ax.spines['right'].set_visible(False)ax.spines['top'].set_visible(False)

可以参照matplotlib官方文档之Ticks and spines下边的例子进行操作,其中Spines这个例子最基础,可以从这个例子看起。
为了方便阅读,把Spines这个例子搬运到这里:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 100)y = 2 * np.sin(x)# Constrained layout makes sure the labels don't overlap the axes.fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, constrained_layout=True)ax0.plot(x, y)ax0.set_title('normal spines')ax1.plot(x, y)ax1.set_title('bottom-left spines')# Hide the right and top spinesax1.spines['right'].set_visible(False)ax1.spines['top'].set_visible(False)# Only show ticks on the left and bottom spinesax1.yaxis.set_ticks_position('left')ax1.xaxis.set_ticks_position('bottom')ax2.plot(x, y)# Only draw spine between the y-ticksax2.spines['left'].set_bounds(-1, 1)# Hide the right and top spinesax2.spines['right'].set_visible(False)ax2.spines['top'].set_visible(False)# Only show ticks on the left and bottom spinesax2.yaxis.set_ticks_position('left')ax2.xaxis.set_ticks_position('bottom')plt.show()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。