pythonplotlabel_python-matplotlib⼦图的通⽤xlabely。。。python - matplotlib⼦图的通⽤xlabel / ylabel
我有以下情节:
fig,ax = plt.subplots(5,2,sharex=True,sharey=True,figsize=fig_size)
现在我想给这个图绘制常见的x轴标签和y轴标签。 使⽤" common",我的意思是在整个⼦图⽹格下⾯应该有⼀个⼤的x轴标签,在右边有⼀个⼤的y轴标签。 我在plt.subplots的⽂档中不到任何相关内容,我的⾕歌搜索建议我需要制作⼀个⼤的
plt.subplot(111) - 但是如何使⽤plt.subplots将我的5 * 2⼦图表放⼊其中?
jolindbe asked 2019-08-08T13:12:21Z
7个解决⽅案
152 votes
这看起来像你真正想要的。 它对您的具体案例采⽤与此答案相同的⽅法:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=(6, 6))
<(0.5, 0.04, 'common X', ha='center')
<(0.04, 0.5, 'common Y', va='center', rotation='vertical')
divenex answered 2019-08-08T13:12:37Z
31 votes
如果没有sharex=True, sharey=True,您将得到:
有了它你应该更好:
fig, axes2d = plt.subplots(nrows=3, ncols=3,
sharex=True, sharey=True,
figsize=(6,6))
for i, row in enumerate(axes2d):
for j, cell in enumerate(row):
cell.imshow(np.random.rand(32,32))
plt.tight_layout()
但是,如果要添加其他标签,则只应将它们添加到边缘图中:
fig, axes2d = plt.subplots(nrows=3, ncols=3,
sharex=True, sharey=True,
figsize=(6,6))
for i, row in enumerate(axes2d):
for j, cell in enumerate(row):
cell.imshow(np.random.rand(32,32))
if i == len(axes2d) - 1:
cell.set_xlabel("noise column: {0:d}".format(j + 1))
if j == 0:
cell.set_ylabel("noise row: {0:d}".format(i + 1))
plt.tight_layout()
为每个绘图添加标签会破坏它(可能有⼀种⽅法可以⾃动检测重复的标签,但我不知道⼀个)。
Piotr Migdal answered 2019-08-08T13:13:24Z
14 votes
由于命令:
fig,ax = plt.subplots(5,2,sharex=True,sharey=True,figsize=fig_size)
你使⽤过返回⼀个由图形和轴实例列表组成的元组,它已经⾜够做(⽐如我已经改变了plt.show()到i):fig,axes = plt.subplots(5,2,sharex=True,sharey=True,figsize=fig_size)
for ax in axes:
ax.set_xlabel('Common x-label')
ax.set_ylabel('Common y-label')
如果您碰巧想要更改特定⼦图上的某些细节,可以通过plt.show()访问它,其中i遍历您的⼦图。
包含⼀个可能也⾮常有帮助
fig.tight_layout()
在⽂件的末尾,在plt.show()之前,为了避免重叠标签。
Marius answered 2019-08-08T13:14:20Z
13 votes
由于我认为它相关且⾜够优雅(不需要指定坐标来放置⽂本),我复制(略微改编)对另⼀个相关问题的答案。import matplotlib.pyplot as plt
fig, axes = plt.subplots(5, 2, sharex=True, sharey=True, figsize=(6,15))
# add a big axis, hide frame
fig.add_subplot(111, frameon=False)
# hide tick and tick label of the big axis
plt.tick_params(labelcolor='none', top=False, bottom=False, left=False, right=False)
plt.xlabel("common X")
plt.ylabel("common Y")
这导致以下结果(使⽤matplotlib 2.2.0版):
bli answered 2019-08-08T13:14:56Z
2 votes
我在绘制图形⽹格时遇到了类似的问题。 图表由两部分组成(顶部和底部)。 y标签应该以两个部分为中⼼。
我不想使⽤依赖于知道外图中位置的解决⽅案(如()),因此纵了set_ylabel()函数的y位置。 它通常是0.5,它被添加到的图的中间。 由于我的代码中的部分(hspace)之间的填充为零,我可以计算
相对于上部的两个部分的中间部分。
import matplotlib.pyplot as plt
idspec as gridspec
# Create outer and inner grid
outerGrid = gridspec.GridSpec(2, 3, width_ratios=[1,1,1], height_ratios=[1,1])
somePlot = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=outerGrid[3], height_ratios=[1,3], hspace = 0)
# Add two partial plots
partA = plt.subplot(somePlot[0])
partB = plt.subplot(somePlot[1])
# No x-ticks for the upper plot
plt._xticklabels(), visible=False)
# The center is (height(top)-height(bottom))/(2*height(top))
# Simplified to 0.5 - height(bottom)/(2*height(top))
mid = _height_ratios()[1]/(2.*_height_ratios()[0])
# Place the y-label
partA.set_ylabel('shared label', y = mid)
plt.show()
图⽚
缺点:
绘图的⽔平距离基于顶部,底部刻度可能延伸到标签中。
该公式不考虑部件之间的空间。matplotlib中subplot
当顶部的⾼度为0时引发异常。
可能存在⼀种通⽤解决⽅案,其将数字之间的填充考虑在内。
CPe answered 2019-08-08T13:16:11Z
2 votes
我发现了⼀种更强⼤的⽅法:
如果您知道进⼊y初始化的set_position和matplotlib kwargs,或者您知道轴的边缘位置在set_position坐标中,您还可以在Figure坐标中指定ylabel位置,并使⽤某些奇特的变换&#34; 魔法。 例如:
ansforms as mtransforms
bottom, top = .1, .9
f, a = plt.subplots(nrows=2, ncols=1, bottom=bottom, top=top)
avepos = (bottom+top)/2
a[0].yaxis.label.set_transform(mtransforms.blended_transform_factory(
mtransforms.IdentityTransform(), f.transFigure # specify x, y transform
)) # changed from default blend (IdentityTransform(), a[0].transAxes)
a[0].yaxis.label.set_position((0, avepos))
a[0].set_ylabel('Hello, world!')
......你应该看到标签仍然适当地左右调整以防⽌与标签重叠,就像正常⼀样 - 但现在它将调整为始终正好在所需的⼦图之间。
此外,如果你甚⾄不使⽤set_position,那么默认情况下ylabel将显⽰在该数字的⼀半。 我猜测这是因为当最终绘制标签时,matplotlib使⽤0.5作为y坐标,⽽不检查基础坐标变换是否已更改。
Luke Davis answered 2019-08-08T13:17:01Z
1 votes
如果通过为左下⾓的⼦图制作隐形标签为常⽤标签保留空间,效果会更好。 从rcParams传⼊fontsize也很好。 这样,常⽤标签将根据您的rc设置更改⼤⼩,并且还将调整轴以为公共标签留出空间。
fig_size = [8, 6]
fig, ax = plt.subplots(5, 2, sharex=True, sharey=True, figsize=fig_size)
# Reserve space for axis labels
ax[-1, 0].set_xlabel('.', color=(0, 0, 0, 0))
ax[-1, 0].set_ylabel('.', color=(0, 0, 0, 0))
# Make common axis labels
<(0.5, 0.04, 'common X', va='center', ha='center', fontsize=rcParams['axes.labelsize'])
<(0.04, 0.5, 'common Y', va='center', ha='center', rotation='vertical', fontsize=rcParams['axes.labelsize'])
EL_DON answered 2019-08-08T13:17:29Z

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。