简体   繁体   English

在单个matplotlib图上嵌入多个gridspec布局?

[英]Embedding multiple gridspec layouts on a single matplotlib figure?

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. 我使用python图形库matplotlib来绘制报表中的几个东西,我发现自己需要在任意较小的图形网格上方有几个固定计数图形。 I searched around, but was unable to find anything that would let me use two gridspec layouts on a single matplotlib figure. 我四处搜索,但无法找到任何可以让我在一个matplotlib图上使用两个gridspec布局的东西。 What I want essentially: 我真正想要的是:

具有两个不兼容的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). 例如,假设我在小图形部分中每行有5个图形,那么就不可能在它们上面并排放置两个相同大小的图形,并且gridspec不允许您指定小数索引(并且它不应该) 。

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. 在我看来,正确的解决方案是在单个图形上有两个单独的gridspec布局,一个用于上半部分的固定计数图形,然后是编程缩放的gridspec用于下半部分的较小图形。 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? 我在使用gridspec或子图的matplotlib中找不到这样的解决方案,所以有人对使用matplotlib进行这样的图形设置有什么建议吗?

Matplotlib's matplotlib.gridspec module contains a class called gridspec.GridSpecFromSubplotSpec . Matplotlib的matplotlib.gridspec模块包含一个名为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. gridspec.GridSpec ,它需要nrow和NCOLS参数,它允许你指定的次要情节将占据/ SPAN细胞,但它也需要一个SubplotSpec对象,它可以从一个细胞或细胞跨度GridSpec对象。 The returned object is a GridSpec which is dependent on the cell dimensions of the SubplotSpec that was used to create it. 返回的对象是GridSpec ,它取决于用于创建它的SubplotSpec的单元格尺寸。

Example: 例:

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

The result of this code is: 这段代码的结果是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM