在matplotlib中的轴空间上分布图像 [英] Spread image over axis space in matplotlib

查看:31
本文介绍了在matplotlib中的轴空间上分布图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 matplotlib 创建数据图,并想在该图的顶部添加图像/草图.但是,使用下面的代码,图像宽度被限制为下面图形的x大小,或者换句话说:尽管我在图像上没有y轴,但是我浪费了那里的空间./p>

如何使图像区域覆盖整个宽度?

到目前为止我的代码:

fig, ((ax,ax0)) = plt.subplots(nrows=2, ncols=1, sharex=False, sharey=False,figsize=[5,4],gridspec_kw={"height_ratios":[1,4],'hspace':0.05})ax0.set_xlabel('X 轴标签')ax0.set_ylabel(r'Y 轴标签')ax.set_xticks([])ax.set_yticks([])ax.text(.5,.5,"IMAGE",ha='center',va='center')plt.tight_layout()

解决方案

您可以自己定义gridspec,并让顶部子图跨越两行.

 将matplotlib.pyplot导入为plt从matplotlib.gridspec导入GridSpecfig = plt.figure(figsize=[5,4])gs = GridSpec(2,2,height_ratios = [1,4],width_ratios = [0,1])ax = fig.add_subplot(gs[0,:])ax0 = fig.add_subplot(gs [1,1])ax0.set_xlabel('X 轴标签')ax0.set_ylabel(r'Y轴标签')ax.set_xticks([])ax.set_yticks([])ax.text(.5,.5,"IMAGE",ha='center',va='center')fig.tight_layout(h_pad=0.2)plt.show()

I am using matplotlib to create a data graph and want to add an image/sketch on top of that plot. However, with the code below, the image width is limited to the x-size of the graph below, or with other words: Although I don't have an y-axis on the image, I'm wasting the space there.

How can I make the image area cover the full width?

My Code until now:

fig, ((ax,ax0)) = plt.subplots(nrows=2, ncols=1, sharex=False, sharey=False, 
                    figsize=[5,4],gridspec_kw={"height_ratios":[1,4],'hspace':0.05})

ax0.set_xlabel('X AXIS LABEL')
ax0.set_ylabel(r'Y AXIS LABEL')

ax.set_xticks([])
ax.set_yticks([])
ax.text(.5,.5,"IMAGE",ha='center',va='center')

plt.tight_layout()

解决方案

You can define the gridspec yourself and let the top subplot span two rows.

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure(figsize=[5,4])
gs = GridSpec(2,2, height_ratios=[1,4], width_ratios=[0,1])

ax = fig.add_subplot(gs[0,:])
ax0 = fig.add_subplot(gs[1,1])

ax0.set_xlabel('X AXIS LABEL')
ax0.set_ylabel(r'Y AXIS LABEL')

ax.set_xticks([])
ax.set_yticks([])
ax.text(.5,.5,"IMAGE",ha='center',va='center')

fig.tight_layout(h_pad=0.2)

plt.show()

这篇关于在matplotlib中的轴空间上分布图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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