我可以单独做其中任何一个,但不能一起做.我认为问题可以归结为:在Matplotlib图形中偏移脊柱后,如何在坐标系中找到脊柱边界,该坐标系可用于在脊柱末端绘制箭头?

code result

这些箭头(显然)没有与脊椎对齐.

箭头的代码是从这里开始的https://matplotlib.org/stable/gallery/spines/centered_spines_with_arrows.html

我的简化代码是:

import matplotlib.pyplot as plt
import numpy as np

spine_offset = 5

fig, ax = plt.subplots()
a = np.linspace(0, 6, 100)
ax.plot(a, np.sin(a) + 1)

ax.spines.top.set_visible(False)
ax.spines.right.set_visible(False)
ax.spines.left.set_position(('outward', spine_offset))
ax.spines.bottom.set_position(('outward', spine_offset))

ax.plot(1, 0, ">k", transform=ax.transAxes, clip_on=False)
ax.plot(0, 1, "^k", transform=ax.transAxes, clip_on=False)

推荐答案

set_position(('outward', spine_offset))中,偏移量以磅为单位测量(1/72英寸,类似于字体的测量方式,例如12磅字体).

using a distance in "axes coordinates"

除了'outward',你也可以使用'axes'个坐标:0在底部(左),1在顶部(右).由于通常绘图不是正方形,为了获得视觉上的相等距离,您可以将x偏移转换为y偏移,同时考虑边界框的纵横比.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

x_offset = 0.02
y_offset = x_offset / ax.get_window_extent().height * ax.get_window_extent().width

a = np.linspace(0, 2 * np.pi, 1000)
ax.plot(np.sin(5 * a), np.sin(6 * a))
ax.set_facecolor('lightsalmon')  # set a color to visualize the offset

ax.spines.top.set_visible(False)
ax.spines.right.set_visible(False)
ax.spines.left.set_position(('axes', -x_offset))
ax.spines.bottom.set_position(('axes', -y_offset))
ax.plot(1, -y_offset, ">k", transform=ax.transAxes, clip_on=False)
ax.plot(-x_offset, 1, "^k", transform=ax.transAxes, clip_on=False)

plt.show()

matplotlib: setting offset for spine arrows

Converting "points" to "axes coordinates"

或者,您可以将点转换为像素,然后从像素转换回轴坐标.一英寸有72个点,这个数字的dpi告诉我们一英寸有多少像素,

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

spine_offset_points = 5
spine_offset_pixels = fig.dpi * spine_offset_points / 72.0
x_offset, y_offset = ax.transAxes.inverted().transform((spine_offset_pixels, spine_offset_pixels)) \
                     - ax.transAxes.inverted().transform((0, 0))

a = np.linspace(0, 2 * np.pi, 1000)
ax.plot(np.sin(3 * a), np.sin(8 * a))
ax.set_facecolor('violet')

ax.spines.top.set_visible(False)
ax.spines.right.set_visible(False)
ax.spines.left.set_position(('outward', spine_offset_points))
ax.spines.bottom.set_position(('outward', spine_offset_points))
ax.plot(1, -y_offset, ">k", transform=ax.transAxes, clip_on=False)
ax.plot(-x_offset, 1, "^k", transform=ax.transAxes, clip_on=False)

plt.show()

converting points to axes coordinates

Python相关问答推荐

Pandas 填充条件是另一列

TARete错误:类型对象任务没有属性模型'

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

多处理队列在与Forking http.server一起使用时随机跳过项目

Django RawSQL注释字段

使用Python和文件进行模糊输出

dask无groupby(ddf. agg([min,max])?''''

python—telegraph—bot send_voice发送空文件

合并与拼接并举

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

BeautifulSoup:超过24个字符(从a到z)的迭代失败:降低了首次深入了解数据集的复杂性:

使用类型提示进行类型转换

删除特定列后的所有列

我可以不带视频系统的pygame,只用于游戏手柄输入吗?''

Pandas:将值从一列移动到适当的列

将数字数组添加到Pandas DataFrame的单元格依赖于初始化

关于数字S种子序列内部工作原理的困惑

在不降低分辨率的情况下绘制一组数据点的最外轮廓