matplotlib:轴边框和刻度线/标签位置 [英] matplotlib: axes border and tick mark/label locations

查看:305
本文介绍了matplotlib:轴边框和刻度线/标签位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现的最终结果是在我的情节周围有一个更厚"的黑色边界,沿着 xmin、xmax、ymin 和ymax.我尝试了几种不同的方法(例如只是在绘图上画一个矩形,见下文),但由于一些原因我未能达到预期的结果.

The end result I'm attempting to achieve is to have a "thicker" black boarder around my plot, along xmin, xmax, ymin, & ymax. I've tried a couple of different things (such as just drawing a rectangle on the plot, see below), but I have not been able to achieve the desired results for a few reasons.

因为我不能只使用刺(我将其中的2根始终设置为0),所以我需要添加其他线或矩形以创建所需的边框.

Because I cannot just use the spines (I've set 2 of them to always be at 0), I need to add some other line or rectangle to create the desired border.

默认情况下,第一个和最后一个刻度标签悬在轴上.我通过更改水平或垂直对齐方式克服了"这一问题,但是他们仍然可以使用更多的填充.我知道这是可能的,但需要转换并且有点笨重.

By default the first and last tick labels overhang the axes. I "overcame" this by changing the horizontal or vertical alignment, but they could still use some more padding. I know this is possible, but requires a transform and is a bit clunky.

现在,我想删除两个轴上的第一个和最后一个刻度.这是因为给定绘制矩形的方式,该矩形始终位于绘图区域内,但是第一个和最后一个刻度线始终位于其外部,而不管矩形的厚度如何.使矩形更粗只会使其与第一个和最后一个刻度标签重叠更多,实际刻度标记保留在矩形之外.

Now I'd like to remove the first and last tick marks on both axis. This is because given the way the rectangle is drawn it is always inside the plot area, but the first and last tick mark are always outside it, regardless of how thick the rectangle is. Making the rectangle thicker only causes it to overlap the first and last tick label more, which the actual tick mark remains outside the rectangle.

关于如何在始终保持轴为 0, 0 的同时实现这种边界的任何其他建议将受到欢迎.这是总体预期的结果.

Any other suggestions on how to achieve this kind of border while always maintaining an axis at 0, 0 would be welcomed. That is the overall desired result.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.patches import Rectangle

X = np.random.randint(low=-9, high=9, size=10)
Y = np.random.randint(low=-9, high=9, size=10)

fig, ax = plt.subplots()
ax.axis([-10, 10, -10, 10])
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.setp(ax.xaxis.get_majorticklabels()[0], ha='left')
plt.setp(ax.xaxis.get_majorticklabels()[-1], ha='right')
plt.setp(ax.yaxis.get_majorticklabels()[0], va='bottom')
plt.setp(ax.yaxis.get_majorticklabels()[-1], va='top')
patPlotBorder = ax.add_artist(Rectangle((-10, -10), 20, 20, fill=False, color='k', linewidth=2))
ax.grid(True)
fig.set_tight_layout(True)
ax.scatter(X, Y, c="b", marker="o", s=40)
plt.show()

推荐答案

无需改动你的大部分代码,你可以将 clip_on 设置为 False,这样完整的显示矩形.

Without changing much of your code, you can set the clip_on to False, such that the complete rectangle is shown.

border = Rectangle((-10, -10), 20, 20, fill=False, color='k', linewidth=3, clip_on=False)
ax.add_artist(border)

由于网格线显示在坐标区内容上方,因此矩形边框内有一些灰线.

Since the gridlines are shown above the axes content, you have some grey line within the rectangle border.

或者,您可以使用两个轴.一种包含所有内容和修改后的脊椎位置等,另一种是您只需将脊椎加粗并删除所有其余部分.

Alternatively, you can use two axes. One with all the content and the modified spine positions etc., and one where you just make the spines bold and remove all the rest.

import numpy as np
import matplotlib.pyplot as plt

X = np.random.randint(low=-9, high=9, size=10)
Y = np.random.randint(low=-9, high=9, size=10)

fig, ax = plt.subplots()
ax2 = fig.add_subplot(111)
ax2.patch.set_visible(False)
ax2.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)
for _, sp in ax2.spines.items():
    sp.set_linewidth(3)

ax.axis([-10, 10, -10, 10])
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.setp(ax.xaxis.get_majorticklabels()[0], ha='left')
plt.setp(ax.xaxis.get_majorticklabels()[-1], ha='right')
plt.setp(ax.yaxis.get_majorticklabels()[0], va='bottom')
plt.setp(ax.yaxis.get_majorticklabels()[-1], va='top')

ax.grid(True)
fig.set_tight_layout(True)
ax.scatter(X, Y, c="b", marker="o", s=40)
plt.show()

这篇关于matplotlib:轴边框和刻度线/标签位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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