嵌套或组合 matplotlib 图形和绘图? [英] Nesting or combining matplotlib figures and plots?

查看:72
本文介绍了嵌套或组合 matplotlib 图形和绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它采用日期、价格(浮点数)和一些结果值(浮点数)的任意长度 3D 数据集,并制作一组按年份拆分的 seaborn 热图.伪代码如下(注意年数因数据集而异,所以我需要它任意缩放):

I have a function that takes an arbitrary length 3D data set of dates, prices(float), and some resulting value(float) and makes a set of seaborn heatmaps split by year. The pseudocode is as follows (note the number of years varies by dataset so I need it to arbitrarily scale):

def makePlots(data):
   split data by year
   fig,axs=plt.subplots(1, numYears)
   x=0
   for year in years
      sns.heatmap(data[year], ax = axs[x++])

   return axs

这会输出一个单独的 matplotlib 图,其中每一年的热图在一行中彼此相邻,如下例所示:单个绘图数据集

this outputs a single matplotlib figure with a heatmap for each year next to each other on a single line, as shown in this example: single plotted dataset

现在我有一个更高级别的函数,在该函数中我提供两个数据集(每个数据集是任意数量的年份),并让它打印热图以供比较.我希望它以某种方式获取由 makePlots 方法制作的图形,并将它们堆叠在一起,如下例所示:两个绘图数据集

Now I have a higher level function in which I feed two data sets (each arbitrary amount of years) and have it print the heatmap plots for each above one another for comparison. I would like it to somehow take the figures made by the makePlots method and just stack them on top of one another, as in this example: two plotted datasets

def compareData(data1,data2):
   fig1 = makePlots(data1)
   fig2 = makePlots(data2)
   fig, (ax1,ax2) = plt.subplots(2,1)
   ax1 = fig1
   ax2 = fig2
   plt.show()

现在这段代码有效,但不是预期的.它打开了 3 个新的绘图窗口,一个正确绘制了 data1,一个正确绘制了 data2,一个带有空的 2 行子图.有没有办法将 makePlots 绘图嵌套在一个新的子绘图中?我也试过返回 plt.gcf().堆栈溢出的所有其他答案都取决于将轴传递给 plot 方法,但鉴于我每个数据集有任意数量的轴(年)并且最终想要比较任意数量的数据集,这似乎并不理想(不是那个无论如何,我可以找出一个实现,因为每一行都可以有任意的年数).

Now this code works, however not as intended. It opens up 3 new plot windows, one with data1 plotted correctly, one with data2 plotted correctly, and one with an empty 2 row subplot. Is there any way to nest the makePlots plots within a new subplot one on top of the other? I have also tried returning plt.gcf(). All the other answers on stack overflow depend on passing the axes to the plot method but given that I have an arbitrary amount of axes (years) per dataset and eventually would like to compare an arbitrary amount of datasets, this seems not ideal (not that I can figure out an implementation of that anyways since each row can have an arbitrary amount of years).

推荐答案

我不推荐它,但您可以使用 fig.add_subplot(nrow, ncol, index) 增量添加子图.

I wouldn't recommend it but you can add subplots incrementally by using fig.add_subplot(nrow, ncol, index).

所以你的两个函数看起来像这样:

So your two functions would look something like this:

def compareData(data1, data2):
    fig = plt.figure()
    makePlots(data1, row=0, fig=fig)
    makePlots(data2, row=1, fig=fig)

def makePlots(data, row, fig):
    years = ... # parse data here
    for ii, year in enumerate(years):
        ax = fig.add_subplot(2, len(years), row * len(years) + ii + 1)
        sns.heatmap(data[year], ax=ax)

这有望解决您的问题.

但是,您遇到这个问题只是因为您在同一个函数中混合了数据解析和绘图.我的建议是首先解析数据,然后将新的数据结构传递给一些绘图函数.

However, you are only having this problem because your are mixing data parsing and plotting in the same function. My advice would be to first parse the data, then pass the new data structure into some plotting functions.

这篇关于嵌套或组合 matplotlib 图形和绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆