- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在网格中显示20张图像,我的代码如下
def plot_matric_demo(img, nrows, ncols):
fig, ax = plt.subplots(nrows=nrows, ncols=ncols)
cur_index = 0
for row in ax:
for col in row:
col.imshow(img)
cur_index = cur_index + 1
col.axis('off')
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()
subplot_img = cv2.imread("subplots.png")
plot_matric_demo(subplot_img, 5, 4)
似乎子图中的图像太小,同时距离又很大,我想知道如何才能使子图中的图像变大?
最佳答案
TL;DR 使用 plt.subplots(nrows=nr, ncols=nc, Figsize=(..., ...))
调整图形大小以便各个子图至少具有与要显示的图像相同的纵横比。
关键点是,imshow
将使用方形像素,因此如果您的图像具有 1:2 的长宽比,则绘制的图像将具有 1:2 的长宽比,并且每个图像都会站立在其自己的子图的中间 - 如果子图的长宽比与图像的长宽比不同,您将遇到“大白边综合症”。
让我们从导入和假图像开始,长宽比为 1:2
In [1]: import numpy as np
...: import matplotlib.pyplot as plt
In [2]: img = np.arange(54*108).reshape(108,54)
并复制您的安排,其中您将 8x6 (x:y) 图形分割为 4x5 (x:y) 子图 - 您的子图水平宽 (8/4=2) 且垂直短 (6/5 =1.2)并且每个图像在其子图居中时具有较宽的水平边距。
In [3]: f, axs = plt.subplots(5, 4)
...: for x in axs.flatten():
...: x.imshow(img) ; x.axis('off')
现在恢复行和列的角色,现在您的子图在水平方向上更小(8/5=1.6)并且更高(6/4=1.5),由于水平白边距的减少和图像的放置明显更好图像尺寸的增加,因为可用高度更大
In [4]: f, axs = plt.subplots(4, 5)
...: for x in axs.flatten():
...: x.imshow(img) ; x.axis('off')
要结束故事,关键是让子图与您使用的图像具有(至少大约)相同的纵横比,为此,我们必须对 figsize
进行干预> 参数,指定宽度:高度等于 (ncols×1):(nrows×2),在下面的示例中 figsize=(5,8)
In [5]: f, axs = plt.subplots(4, 5, figsize=(5,8))
...: for x in axs.flatten():
...: x.imshow(img) ; x.axis('off')
关于python - 如何使 pyplot.subplots 内的图像更大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59838360/
从 matplotlib 库中我导入了 pyplot 模块。在该模块中有一个我使用过的函数plot()。现在我的问题是: 为什么plot()函数不在任何类中?如果它在任何类中,为什么我们不创建该类的任
过去我可以使用带有 for 循环的 matplotlib 来制作简单的动画,但现在已经有一段时间没用了。 标准答案是您必须打开交互模式和/或使用matplotlib.pyplot.draw()强制重绘
我正在为一个研究项目绘制一些天气数据。该图由 18 个时间步组成。我认为实现此目的的最佳方法是为每个时间步长创建一个新图,将其保存为文件,然后为下一个时间步长创建一个新图(使用 for 循环)。 例如
from matplotlib import pyplot as plt import matplotlib.pyplot as plt 以上说法是否等价?哪种形式更具可读性/更好? 最佳答案 尽管它
我希望负条朝下,正条朝上,x轴(0线)正好在它们之间通过。我试过了 chart = fig.bar(x, negative_data, width=35, color='r') ax2 = plt.g
所以我想要的是让我的 pyplot 以科学记数法记号。所以每个刻度看起来像 1x10^6 而不是 1,然后是轴上的 10^6。到目前为止,我能够做到这一点的唯一方法是手动将每个刻度标签设置为 r'$1
我正在尝试针对 datetime 的列表绘制一些数据pyplot 在 x 轴上的对象。然而,日期显示为标准格式,即 %Y-%m-%d %H:%M:%S (太长了)。我可以通过使用 strftime 创
假设我有这个代码: num_rows = 10 num_cols = 1 fig, axs = plt.subplots(num_rows, num_cols, sharex=True) for i
我正在尝试绘制带有拟合虚线的散点图。 plt.scatter(x, y, s=z, alpha=0.5) m, b = np.polyfit(x, y, 1) y = [(m*i) + b for i
我想在 pyplot 图形中包含一些文本,并且能够在不改变比例的情况下放大它。请参阅下面的情节示例、我希望发生的事情以及实际发生的事情。 重现错误的代码: import matplotlib.pypl
我想显示第一次显示时放大的图像,但仍然可以使用图形工具栏中的交互式“重置原始 View ”按钮缩小到全比例。裁剪是完全 Not Acceptable 。使用 plt.axis([x0, x1, y0,
我正在尝试绘制带有拟合虚线的散点图。 plt.scatter(x, y, s=z, alpha=0.5) m, b = np.polyfit(x, y, 1) y = [(m*i) + b for i
我想在 pyplot 图形中包含一些文本,并且能够在不改变比例的情况下放大它。请参阅下面的情节示例、我希望发生的事情以及实际发生的事情。 重现错误的代码: import matplotlib.pypl
我正在编写一门类(class),以便制作符合我的多种需求的情节人物!我特别关注每个 pyplot 用户都知道的线型的循环(颜色和线型),有 4 种线型('-'、'--'、'-.'、':')但有一个选项
我特别想让其中一行比另一行早结束。它们是按时间顺序排列的(逐月和逐年)。 months= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] pass_2015 = [0
给定一个简单的负值列表:l = [0, -1, -1, -1, -10, -100] 使用标准方法 plt.hist(l) 可视化直方图的最快方法是什么? 我希望能够查看列表中的所有条目及其相对频率。
我有一个包含数据的文件,我将其分为三类。我想显示三个不同的“垃圾箱”,它们都只显示一个数字(该类别的平均值)。 import csv import matplotlib.pyplot as plt i
我不确定我的措辞是否正确,但我想做的是创建两个子图的图形,其中两个图有不同的限制,但它们的大小使得物理比例(如,y -人物高度每厘米的距离)是相同的。为了澄清起见,假设子图 1 显示从 -3 到 3
有谁知道为什么这段代码不能正确绘制框颜色。我希望每个组件都有不同的颜色,但它们都是黑色的,带有蓝色图例。 from numpy import array, zeros import matplotli
我在尝试使用 pyplot 标记数据时遇到了一场噩梦。 我目前正在绘制所有数据,如下所示:plt.plot(data)。 data 是一个数组,其中有一列是总成本,其他列是子成本 我想添加图例并适本地
我是一名优秀的程序员,十分优秀!