Can I tell python to put an existing figure in a new figure?

2024/9/8 10:34:00

Creating a certain plot is a lot of work, so I would like to automate this by create a function f() that returns a figure.

I would like to call this function so that I can put the result in a subplot. Is there anyway I can do this? Below is some psuedo code explaining what I mean

figure_of_interest = f()fig,ax = plt.subplots(nrows = 4,cols = 1)ax[1].replace_with(figure_of_interest)
Answer

This was asked before here and here.

Short answer: It is not possible.

But you can always modify the axes instance or use a function to create/modify the current axes:

import matplotlib.pyplot as plt
import numpy as npdef test():x = np.linspace(0, 2, 100)# With subplotsfig1, (ax1, ax2) = plt.subplots(2)plot(x, x, ax1)plot(x, x*x, ax2)# Another Figure without using axesfig2 = plt.figure()plot(x, np.exp(x))plt.show()def plot(x, y, ax=None):if ax is None:ax = plt.gca()line, = ax.plot(x, y)return linetest()
https://en.xdnf.cn/q/73149.html

Related Q&A

django-crispy-forms have field and button on same row

I am needing to have a bootstrap PrependedText field with a button on the same row. I can get it on the same row but it shows the button before the textbox and I want it after. What am I doing wrong an…

Uploading file in python flask

I am trying to incorporate uploading a basic text/csv file on my web app which runs flask to handle http requests. I tried to follow the baby example in flasks documentation running on localhost here. …

Diagonal stacking in numpy?

So numpy has some convenience functions for combining several arrays into one, e.g. hstack and vstack. Im wondering if theres something similar but for stacking the component arrays diagonally?Say I h…

Rules Engine in C or Python [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Draw arrows between 3 points

I am trying to draw arrows between three points in matplotlib.Lets assume we have 3 arbitrary points (A1,A2,A3) in 2d and we want to draw arrows from A1 to A2 and from A2 to A3.Some code to make it cle…

Make an animated wave with drawPolyline in PySide/PyQt

Im trying to animate a polyline (it have to act like a wave). Ive tried this way:from PySide.QtCore import * from PySide.QtGui import * import sys, timeclass Test(QMainWindow):def __init__(self, parent…

dateutil.tz package apparently missing when using Pandas?

My python 2.7 code is as follows:import pandas as pd from pandas import DataFrameDF_rando = DataFrame([1,2,3])...and then when I execute, I get a strange error regarding dateutil.tz./Library/Frameworks…

Importing SPSS dataset into Python

Is there any way to import SPSS dataset into Python, preferably NumPy recarray format? I have looked around but could not find any answer.Joon

Given a set of points defined in (X, Y, Z) coordinates, interpolate Z-value at arbitrary (X, Y)

Given a set of points in (X, Y, Z) coordinates that are points on a surface, I would like to be able to interpolate Z-values at arbitrary (X, Y) coordinates. Ive found some success using mlab.griddata …

Python multiprocessing speed

I wrote this bit of code to test out Pythons multiprocessing on my computer:from multiprocessing import Poolvar = range(5000000) def test_func(i):return i+1if __name__ == __main__:p = Pool()var = p.map…