更改 matplotlib 中轴线的长度 [英] Changing the length of axis lines in matplotlib

查看:33
本文介绍了更改 matplotlib 中轴线的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改matplotlib图的轴的显示长度.这是我当前的代码:

I am trying to change the displayed length of the axis of matplotlib plot. This is my current code:

import matplotlib.pyplot as plt
import numpy as np

linewidth = 2
outward = 10
ticklength = 4
tickwidth = 1

fig, ax = plt.subplots()

ax.plot(np.arange(100))

ax.tick_params(right="off",top="off",length = ticklength, width = tickwidth, direction = "out")
ax.spines["top"].set_visible(False), ax.spines["right"].set_visible(False)

for line in ["left","bottom"]:
    ax.spines[line].set_linewidth(linewidth)
    ax.spines[line].set_position(("outward",outward))

生成以下图:

我希望我的绘图看起来像下面的那样,其中轴线缩短了:

I would like my plot to look like the following with axis line shortened:

我无法在 ax [axis] .spines 方法中找到它.我也无法使用 ax.axhline 方法很好地绘制此图.

I wasn't able to find this in ax[axis].spines method. I also wasn't able to plot this nicely using ax.axhline method.

推荐答案

您可以将这些行添加到代码的末尾:

You could add these lines to the end of your code:

ax.spines['left'].set_bounds(20, 80)
ax.spines['bottom'].set_bounds(20, 80)

for i in [0, -1]:
    ax.get_yticklabels()[i].set_visible(False)
    ax.get_xticklabels()[i].set_visible(False)

for i in [0, -2]:
    ax.get_yticklines()[i].set_visible(False)
    ax.get_xticklines()[i].set_visible(False)

为了得到这个:

这篇关于更改 matplotlib 中轴线的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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