在matplotlib中的轴空间上分布图像

时间:2018-10-29 13:52:35

标签: python matplotlib

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

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

我的代码至今:

abc.dat

What I have until now

2 个答案:

答案 0 :(得分:1)

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

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()

enter image description here

答案 1 :(得分:0)

imshow可以做到!

import pandas as pd
import matplotlib.pyplot as plt
import requests

r = requests.get('https://upload.wikimedia.org/wikipedia/commons/c/cc/Rodger_Young.jpg', stream=True)

with open("young.jpg", "wb") as file:
  file.write(r.raw.read())


image = plt.imread("young.jpg")
plt.imshow(image)

给予: Rodger Young

plt的所有熟悉的方法仍然应该可以使用。

r = requests.get('https://upload.wikimedia.org/wikipedia/commons/c/cc/Rodger_Young.jpg', stream=True)

with open("young.jpg", "wb") as file:
  file.write(r.raw.read())


image = plt.imread("young.jpg")

plt.suptitle("Rodger Young")
plt.axis("off")

plt.imshow(image)

给予: Cleanly rendered Rodger Young, with title