如何加入两个matplotlib人物 [英] How to join two matplotlib figures

查看:44
本文介绍了如何加入两个matplotlib人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以从数据生成matplotlib图形.这些图将保存到磁盘,如下所示:

I have a script that generates matplotlib figures from data. Those plots are saved to disk as follows:

fig, ax = plt.subplots() # create the plot # ... pickle.dump(ax, open(of, 'wb'))

fig, ax = plt.subplots() # create the plot # ... pickle.dump(ax, open(of, 'wb'))

在另一个脚本中,我想加入其中的某些情节.我可以使用以下方式回读数据:

In another script, I want to join certain of these plots. I can read the data back using:

figures = [pickle.load(file) for file in files]

(FWIW,我读回的数字的类型为AxesSubplot.)

(FWIW, figures that I read back have the type AxesSubplot.)

到目前为止,一切都很好.现在,我想使用可用图的最大或最小比例将两个(或多个)图形的数据放在一起.由于缺乏经验,我完全不知道如何实现这一目标.我确实发现了有关加入地块的问题,并且共识是首先绘制一个图.在我的情况下,这将非常困难,因为单个数据集的绘制逻辑已经很复杂. (还有其他原因,为什么每个数据集都应在第一步中自行绘制,然后才可能与其他数据集连接.)

So far so good. Now I want to put the data of two (or more) figures together, using either the largest or smallest scale of the available plots. Due to my lack of experience, I have absolutely no idea how to accomplish that. I did find questions about joining plots and the consensus was to plot in one figure in the first place. In my case that would be rather difficult as the plotting logic for a single data set is already complex. (There are other reasons why each dataset should be plotted on its own in a first step, and only then be potentially joined with others).

我要加入的图以相同的方式表示它们的数据-即所有图都是线图或直方图(不确定要如何有效地连接那些图)或QQPlot(请参阅statsmodels.api).它们可能具有相同的数据大小,也可能不具有相同的数据大小.

The plots I want to join represent their data in the same way - i.e. all of the plots are line plots or histograms (not really sure how to join those meaningfully) or QQPlots (see statsmodels.api). They may or may not have the same size of data.

如何加入不同数字的地块?

How can I join the plots that are in different figures?

推荐答案

我认为您会发现将数据保存到文件中更容易,以后可以从该文件中生成新图.您甚至可以使用 np.savez 保存一个文件中只有数据,还有plot方法及其参数.这是您稍后如何加载这些文件到在新图中生成连接"图:

I think you'll find it easier to save the data to a file from which you can later generate new plots. You could even use np.savez to save not only the data, but also the plot method and its arguments in one file. Here is how you could later load those files to generate "joined" plots in a new figure:

import matplotlib.pyplot as plt
import numpy as np

def join(ax, files):
    data = [np.load(filename) for filename in files]
    for datum in data:
        method = getattr(ax, datum['method'].item())
        args = tuple(datum['args'])
        kwargs = datum['kwargs'].item()
        method(*args, **kwargs)

x = np.linspace(-3, 3, 100)
y = np.exp(-x**2/2)/np.sqrt(2*np.pi)
a = np.random.normal(size=10000)

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
np.savez('/tmp/a.npz', method='plot', args=(x, y), kwargs=dict())

fig, ax = plt.subplots()
ax.hist(a, bins=100, density=True)
plt.show()
np.savez('/tmp/b.npz', method='hist', args=(a,), 
         kwargs=dict(bins=100, density=True))

fig, ax = plt.subplots()
join(ax, ['/tmp/a.npz', '/tmp/b.npz'])
plt.show()

以上,我使用np.saveznp.load而不是pickle来保存和恢复数据.或者,您可以腌制包含数据,方法及其参数的字典,元组或列表.但是,由于数据主要是数字数据,因此使用np.savez可以效率更高,而且效率更低比泡菜有安全隐患.

Above I used np.savez and np.load instead of pickle to save and restore the data. Alternatively, you could pickle a dict, tuple or list containing the data, the method and its arguments. However, since the data is mainly numeric, using np.savez is more efficient and less of a security risk than pickle.

这篇关于如何加入两个matplotlib人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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