Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy Matplotlib集合Y标签内联_Python_Numpy_Matplotlib_Plot - Fatal编程技术网

Python Numpy Matplotlib集合Y标签内联

Python Numpy Matplotlib集合Y标签内联,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,我想知道如何在下面的图中设置Y标签,所有的Y标签都相互关联。由于Y值的空间不相等,因此当前它们未对齐 可以使用AXESSubplot上的set_ylabel方法偏移x和y标签。 也可以设置y记号标记标签的格式 使用matplotlib文本对象时,如果要在子图形中循环,可以使用参数transform=ax.transAxes来使用图形坐标而不是轴坐标 但我认为你想要的只是抵消它们。当文档稀疏时,使用dir戳穿名称空间 fig, ax = plt.subplots(1, 1) dirax包括ya

我想知道如何在下面的图中设置Y标签,所有的Y标签都相互关联。由于Y值的空间不相等,因此当前它们未对齐


可以使用AXESSubplot上的set_ylabel方法偏移x和y标签。

也可以设置y记号标记标签的格式

使用matplotlib文本对象时,如果要在子图形中循环,可以使用参数transform=ax.transAxes来使用图形坐标而不是轴坐标

但我认为你想要的只是抵消它们。当文档稀疏时,使用dir戳穿名称空间

fig, ax = plt.subplots(1, 1)
dirax包括yaxis

dirax.yaxis包括get_标签

dirax.yaxis.get_标签包括set_x

helpax.yaxis.get_label.set_x表示:


在这种情况下,根本不使用ylabel可能更容易。相反,请使用“注释”将文本放置在距离轴左侧的恒定偏移处

作为您问题的一个例子:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=4, sharex=True)
yranges = [(-1000, 100), (-0.001, 0.002), (0, 5), (0, 20)]
labels = ['$P_{EUC}[mm]$', '$P_z[mm]$', '$P_Y[mm]$', '$P_X[mm]$']

for ax, yrange, label in zip(axes, yranges, labels):
    ax.set(ylim=yrange, ylabel=label)

plt.show()
要解决这个问题,最容易使用annotate。诀窍是将文本放置在轴坐标中y=0.5处,然后在x方向上从图形的左边缘开始放置5个点。语法有点冗长,但相对容易阅读。键位于控制xy和xytext解释方式的xycoords和textcoords kwargs中:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=4, sharex=True)
yranges = [(-1000, 100), (-0.001, 0.002), (0, 5), (0, 20)]
labels = ['$P_{EUC}[mm]$', '$P_z[mm]$', '$P_Y[mm]$', '$P_X[mm]$']

for ax, yrange, label in zip(axes, yranges, labels):
    ax.set(ylim=yrange)
    ax.annotate(label, xy=(0, 0.5), xytext=(5, 0), rotation=90,
                xycoords=('figure fraction', 'axes fraction'),
                textcoords='offset points', va='center', ha='left')

plt.show()

使用ax.yaxis.set_label_coords可以更优雅地执行此操作,使用x坐标的常量值,如图所示。例如:

import matplotlib.pyplot as plt

labels = ['$P_{EUC}[mm]$', '$P_z[mm]$', '$P_Y[mm]$', '$P_X[mm]$']

fig,axs=plt.subplots(4,1,sharex=True)

axs[0].set_ylim(-1000,0)
axs[1].set_ylim(-0.0010,0.0020)
axs[2].set_ylim(0,5)
axs[3].set_ylim(0,20)

[axs[i].set_ylabel(labels[i]) for i in range(4)]

labelx = -0.1 # Change this to suit your needs
[axs[i].yaxis.set_label_coords(labelx,0.5) for i in range(4)]

fig.savefig('labelx.png')

但是,没有自动的方法来偏移x和y标签,使它们的间距相等。我不想对每个正在修补的绘图都这样做,ax.yaxis.get\u label.set\u x似乎不起作用。get_标签可能会从图例而不是轴标签返回文本。我有一些代码,其中我成功地使用ax.yaxis.label.set_family'Georgia'设置了y轴标签的样式,但是ax.yaxis.label.set_x0也无法满足您的要求:-奇怪的是,我可以使用ax.yaxis.get_label.set_verticalalignment'bottom'|'top来获得y轴标签的平移。位置似乎在字符的框架中;“向上”指向字母的顶部,即左侧。但我还没有实现进一步向左的偏移。你可以在不需要注释的情况下处理这些标签@好极了!我不知道那种方法!
import matplotlib.pyplot as plt

labels = ['$P_{EUC}[mm]$', '$P_z[mm]$', '$P_Y[mm]$', '$P_X[mm]$']

fig,axs=plt.subplots(4,1,sharex=True)

axs[0].set_ylim(-1000,0)
axs[1].set_ylim(-0.0010,0.0020)
axs[2].set_ylim(0,5)
axs[3].set_ylim(0,20)

[axs[i].set_ylabel(labels[i]) for i in range(4)]

labelx = -0.1 # Change this to suit your needs
[axs[i].yaxis.set_label_coords(labelx,0.5) for i in range(4)]

fig.savefig('labelx.png')