本文介绍了在单个matplotlib图形上嵌入多个gridspec布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 图形库 matplotlib 来绘制报告中的一些内容,我发现自己需要在较小图形的任意网格上方有几个固定计数的图形.我四处搜索,但找不到任何可以让我在单个 matplotlib 图上使用两个 gridspec 布局的东西.我本质上想要的是:

I am using the python graphing library matplotlib to graph several things in a report, and I found myself needing to have several fixed-count graphs above an arbitrary grid of smaller graphs. I searched around, but was unable to find anything that would let me use two gridspec layouts on a single matplotlib figure. What I want essentially:

我知道如果第二组中每行的图形数是偶数,我可以破解一些解决方案.但是如果每一行的计数都是奇数,那么这样的解决方案是不可能的.例如,假设我在小图形部分每行有 5 个图形,那么在它们上方并排放置两个大小相同的图形是不可能的,并且 gridspec 不允许您指定分数索引(它不应该).

I know I could hack some solution if the number of graphs per row in the second group is an even number. but if each row has an odd count, then such a solution is not possible. For example, imagine I have 5 graphs per row in the small graph section, then it would be impossible to have two equal size graphs side by side above them, and the gridspec does not let you specify fractional indices(and it shouldn't).

在我看来,正确的解决方案是在单个图形上有两个单独的 gridspec 布局,一个用于上半部分的固定计数图,然后一个用于下半部分较小图形的编程缩放 gridspec.我在带有gridspec或subplot的matplotlib中没有找到这样的解决方案,那么有人对使用matplotlib进行这种图形设置有任何建议吗?

In my mind the proper solution would be to have two separate gridspec layouts on the single figure, one for the fixed count graphs on the top half, and then a programmatically scaled gridspec for the smaller graphs on the bottom half. I have found no such solution in matplotlib with gridspec or subplots, so does anyone have any suggestions for such a graph setup using matplotlib?

推荐答案

Matplotlib的 matplotlib.gridspec 模块包含一个名为 gridspec.GridSpecFromSubplotSpec 的类.与 gridspec.GridSpec 一样,它采用 nrow 和 ncols 参数,允许您指定子图将占据/跨越的单元格,但它也需要一个 SubplotSpec 对象,可以是 GridSpec 对象中的一个单元格或单元格跨度.返回的对象是一个 GridSpec,它取决于用于创建它的 SubplotSpec 的单元格尺寸.

Matplotlib's matplotlib.gridspec module contains a class called gridspec.GridSpecFromSubplotSpec. Like gridspec.GridSpec, it takes nrow and ncols parameters which allow you to specify the cells that subplots will occupy/span, however it also requires a SubplotSpec object, which can be a cell or cell span from a GridSpec object. The returned object is a GridSpec which is dependent on the cell dimensions of the SubplotSpec that was used to create it.

示例:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

outer_grid = gridspec.GridSpec(1,2) # gridspec with two adjacent horizontal cells
left_cell = outer_grid[0,1] # the left SubplotSpec within outer_grid

inner_grid = gridspec.GridSpecFromSubplotSpec(2,1, left_cell)

# From here we can plot usinginner_grid's SubplotSpecs
ax1 = plt.subplot(inner_grid[0,0])
ax2 = plt.subplot(inner_grid[1,0])

ax1.plot(data)
ax2.plot(other_data)

plt.show()

这段代码的结果是:

---------------------------------
|               |---------------|
| We didn't plot| |     |other| |
| anything here.| |data |data | |
|               | |     |     | |
|               |---------------|
---------------------------------

这篇关于在单个matplotlib图形上嵌入多个gridspec布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:37