如何在Matplotlib中将子图旋转45度?

问题描述 投票:0回答:1

我正在尝试探索一个子图2地块,其正方形旋转了45度。

import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np

data = np.random.rand(10, 10) * 20

# create discrete colormap
cmap = colors.ListedColormap(['red', 'blue','green'])
bounds = [0,5,10,15]
norm = colors.BoundaryNorm(bounds, cmap.N)

fig, ax= plt.subplots(1,2)

ax[0].imshow(data, cmap=cmap, norm=norm)

# draw gridlines
ax[0].grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
ax[0].set_xticks(np.arange(-.5, 10, 1));
ax[0].set_yticks(np.arange(-.5, 10, 1));


ax[1].imshow(data, cmap=cmap, norm=norm)

# draw gridlines
ax[1].grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
ax[1].set_xticks(np.arange(-.5, 10, 1));
ax[1].set_yticks(np.arange(-.5, 10, 1));

plt.show()

实际结果是:-

enter image description here

我想将单个图旋转45度。类似于:-enter image description here

我正在尝试在Matplotlib文档中找到。仍然没有。有帮助吗?

请注意,这不是重复项

Is there a way to rotate a matplotlib plot by 45 degrees?

提到的URL是用于绘图的。解决方案是旋转图像。但是,这与子图有关。我想旋转PLOT而不是整个图像。

python matplotlib graph subplot imshow
1个回答
0
投票

您可以签出此问题:Is there a way to rotate a matplotlib plot by 45 degrees?。我没有找到实现1的方法,所以我发现了一个棘手的方法,它是将图形捕获到缓冲区中,将它们旋转-45度,然后将它们合并为一个图像,并且您具有相同的两个图像,可以尝试如下操作:

import io
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np


##PLOTING THE FIGURE##

data = np.random.rand(10, 10) * 20

# create discrete colormap
cmap = colors.ListedColormap(['red', 'blue','green'])
bounds = [0,5,10,15]
norm = colors.BoundaryNorm(bounds, cmap.N)

#We change style values to get the image with better quality
fig=plt.figure(frameon=False)
plt.rcParams.update({'font.size': 46})
plt.figure(figsize=(20,20))

plt.imshow(data, cmap=cmap, norm=norm)

# draw gridlines
plt.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0)
plt.gca().set_xticks(np.arange(-.5, 10, 1));
plt.gca().set_yticks(np.arange(-.5, 10, 1));


##SAVING THE FIGURE INTO AN IMAGE##

#We save the current figure into an image 
buf = io.BytesIO()
plt.savefig(buf, format='png',bbox_inches='tight')
buf.seek(0)
im = Image.open(buf)                   #get the current ploted figure as an image
img_01 = im.rotate(-45, expand=True)   #we rotate the image -45 degrees
img_02 = img_01
buf.close()

##MERGING THE TWO FIGURES##

#Since two figures are equal, you merge the figure and a copy into a single image
new_im = Image.new('RGB', (2*img_01.size[0],img_01.size[1]), (250,250,250))

new_im.paste(img_01, (0,0))
new_im.paste(img_02, (img_01.size[0],0)) 
new_im.save("merged_images.png", "PNG")    #Important(just to clarify): save the image, since the buffer is renewed every time you run the script
new_im.show()

输出:enter image description here

我仍在弄清楚为什么当我定义为白色时背景变成黑色。但是,至少这是一个开始。我通过以下链接帮助自己:

使用plt.figure(figsize=(20,20),frameon=False)plt.gca().tick_params(axis='x', colors='white')plt.gca().tick_params(axis='y', colors='white'),您可以获得“更清洁”的输出图像:enter image description here

并且,使用new_im.putalpha(0)中的##MERGING THE TWO FIGURES##,您可以获得以下结果:enter image description here

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.