如何在 Matplotlib 中的绘图内绘制轴线? [英] How can I draw axis lines inside a plot in Matplotlib?

查看:92
本文介绍了如何在 Matplotlib 中的绘图内绘制轴线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Matplotlib绘制数据时,默认情况下始终将轴绘制为框定绘图的框.假设我在轴范围内绘制数据 -2 <×<2-2 ,但是我想通过原点在此绘图区域内绘制轴线,最好沿着这些轴线而不是沿着外部框架带有刻度线和刻度线标签.

解决方案

When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2 and -2 < y < 2, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.

解决方案

This is well documented in the spines example (old link) / spine placement demo (new link).

You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')).

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(111)
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')

# remove the ticks from the top and right edges
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

这篇关于如何在 Matplotlib 中的绘图内绘制轴线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆