How to join two matplotlib indicators

I have a script that generates matplotlib numbers from data. These graphs are saved to disk as follows:

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

In another script, I want to join some of these graphs. I can read the data with:

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

(FWIW, the numbers I read are of type AxesSubplot.)

So far so good. Now I want to combine the data of two (or more) digits, using either the largest or the smallest scale of the graphs available. Due to my lack of experience, I have absolutely no idea how to do this. I found questions about joining the plots, and the consensus was to capture one figure first. In my case, this would be rather difficult, since the logic of plotting for one data set was already complex. (There are other reasons why each data set should be built independently in the first step and only then potentially linked to others). A.

The graphs I want to combine represent their data in the same way - i.e. all charts are line charts or histograms (not quite sure how to join these significant ones) or QQPlots (see statsmodels.api). They may or may not have the same data size.

How can I join the charts that are in different pictures?

+1
source share
2 answers

I think it will be easier for you to save the data in a file from which you can later generate new charts. You can even use np.savezto save not only data, but also the graph method and its arguments in one file. Here is how you could load these files to generate β€œmerged” graphics 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()

enter image description here


np.savez np.load pickle . , , , , . , , np.savez , .

+3

, . , .

import numpy as np
import matplotlib.pyplot as plt

def myplot1(data, ax=None, show=False):
    if not ax:
        _, ax = plt.subplots()
    ax.plot(data[0], data[1])
    if show:
        plt.show()

def myplot2(data, ax=None, show=False):
    if not ax:
        _, ax = plt.subplots()
    ax.hist(data, bins=20, density=True)
    if show:
        plt.show()

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

# create figure 1
myplot1((x,y))
#create figure 2
myplot2(a)

# create figure with both
fig, ax = plt.subplots()
myplot1((x,y), ax=ax)
myplot2(a, ax=ax)

plt.show()


, , , :

import matplotlib.pyplot as plt
import numpy as np
import pickle

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)

pickle.dump(fig, open("figA.pickle","wb"))
#plt.show()

fig, ax = plt.subplots()
ax.hist(a, bins=20, density=True, ec="k")

pickle.dump(fig, open("figB.pickle","wb"))
#plt.show()

plt.close("all")

#### No unpickle the figures and create a new figure
#    then add artists to this new figure

figA = pickle.load(open("figA.pickle","rb"))
figB = pickle.load(open("figB.pickle","rb"))

fig, ax = plt.subplots()

for figO in [figA,figB]:
    lists = [figO.axes[0].lines, figO.axes[0].patches]
    addfunc = [ax.add_line, ax.add_patch]
    for lis, func in zip(lists,addfunc):
        for artist in lis[:]:
            artist.remove()
            artist.axes=ax
            artist.set_transform(ax.transData)
            artist.figure=fig
            func(artist)

ax.relim()
ax.autoscale_view()

plt.close(figA)
plt.close(figB)   
plt.show()

, . , , . , , , , , .

+2

Source: https://habr.com/ru/post/1693954/


All Articles