Is there a better way to re-use plots Matplotlib?

Drew Verlee

The below code block (A) solves my overall problem of being able to re-use plots, but i'm wondering if there is a better way (one that doesn't involve creating a function for each plt.plot, as is shown below.

Code block A:

import maptplotlib.pyplot as plt

#create a function just to call the plt.plot
def first_plot(): plt.plot(x,y) 
first_plot()
# Now i can just show the first plot
plt.show()

def second_plot(): plt.plot(x,z)

first_plot() # instead of plt.plot(x,y)
second_plot() # instead of plt.plot(x,z)
# now i can show both plots
plt.show()

if the plots are complicated:

plot.plot(lots of details)

and their are many:

plots = [first,second,third,fourth,...]

I would think this would be advantageous because it avoids code re-use.

However, creating a function just to call plt.plot() indicates to me their might be a better way.what i would like to be doing is something like

first_plot = plt.plot(x,y)
first_plot()
#now i want to show just the first plot
plt.show() # first call

second_plot = plt.plot(x,z)
# now i want to show them together combined
first_plot() 
second_plot()
plt.show() # second call

But this doesn't seem to work/e.g the second call to plt.show() wouldn't produce the first plot. (even if you unpack first_plot (which in reality, from code block B, is actual a list):

In [17]: first_plot
Out[17]: [<matplotlib.lines.Line2D at 0x7fd33ac8ff50>]

This cannot be used to produce the plot again in anyway I can see. This might be because of the relationship between plt.show and plt.plot, which i don't fully understand. Plt.plot seems to put the plot in a que, that then plt.show pops out. However, plt.shows description talks about a blocking and unblocking mode and interactive and non-interactive mode:

show(*args, **kw)
    When running in ipython with its pylab mode, display all
    figures and return to the ipython prompt.

    In non-interactive mode, display all figures and block until
    the figures have been closed; in interactive mode it has no
    effect unless figures were created prior to a change from
    non-interactive to interactive mode (not recommended).  In
    that case it displays the figures but does not block.

    A single experimental keyword argument, *block*, may be
    set to True or False to override the blocking behavior
    described above.

Which i don't understand. But regardless of how i call plt.show() vs plt.show(False) (blocking?) it doesn't seem to have an impact e.g the output is the same in the context of code block A and B.

So put another way, is there a way to select which plots created to show/overlay at different points in the code?

Drew Verlee

However awkward, it seems the best way to "re-use" plots as described in my question is to do as I original suggested and put it inside a function.

def some_plot(): return plot(x,y)

and then if you want to re-use the plot simply call the function again:

some_plot()

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to use mouse to rotate matplotlib 3D plots in wxPython?

分類Dev

Unexpected plots on matplotlib histograms

分類Dev

Better way to use findstr to get packet loss, batch

分類Dev

What's a better way to use df.apply on multiple columns?

分類Dev

Angular: a better way to filter when you're only expecting one result

分類Dev

Is there a better way of comparing dates

分類Dev

A better way to detect the cursor?

分類Dev

Better way to return boolean

分類Dev

A better way to introspect a capture

分類Dev

Is there a better way to do this? BASH

分類Dev

better way to do this in MATLAB?

分類Dev

Is there a better way to loop code?

分類Dev

Is there a better way to model this access pattern than to use two global secondary indexes (GSI)?

分類Dev

Matplotlib: Spaces in between hexagons in hexbin plots?

分類Dev

Rmarkdown automatically use function on plots

分類Dev

Is it better to use $(pwd) or $PWD?

分類Dev

Is it better to use !! or history?

分類Dev

Use Asynchronous IO better

分類Dev

Is there a better way to define a global variable?

分類Dev

Decorator with Arguments: Would this be a better way?

分類Dev

Is this possible using LEAD or is there a better way?

分類Dev

Is there a way of making discord embeds better?

分類Dev

Is there a better way to do Insertion Sort?

分類Dev

Is there a better way to do this than echo?

分類Dev

Better way to override a function definition

分類Dev

Is there a better way to divide this string into substrings?

分類Dev

What is the better way to use IP field (IPV4 or IPV6) in proto3 file for Golang and C# usage

分類Dev

Use VPN to make upload better

分類Dev

matplotlib scatter plots do not display when populated using for loop

Related 関連記事

  1. 1

    How to use mouse to rotate matplotlib 3D plots in wxPython?

  2. 2

    Unexpected plots on matplotlib histograms

  3. 3

    Better way to use findstr to get packet loss, batch

  4. 4

    What's a better way to use df.apply on multiple columns?

  5. 5

    Angular: a better way to filter when you're only expecting one result

  6. 6

    Is there a better way of comparing dates

  7. 7

    A better way to detect the cursor?

  8. 8

    Better way to return boolean

  9. 9

    A better way to introspect a capture

  10. 10

    Is there a better way to do this? BASH

  11. 11

    better way to do this in MATLAB?

  12. 12

    Is there a better way to loop code?

  13. 13

    Is there a better way to model this access pattern than to use two global secondary indexes (GSI)?

  14. 14

    Matplotlib: Spaces in between hexagons in hexbin plots?

  15. 15

    Rmarkdown automatically use function on plots

  16. 16

    Is it better to use $(pwd) or $PWD?

  17. 17

    Is it better to use !! or history?

  18. 18

    Use Asynchronous IO better

  19. 19

    Is there a better way to define a global variable?

  20. 20

    Decorator with Arguments: Would this be a better way?

  21. 21

    Is this possible using LEAD or is there a better way?

  22. 22

    Is there a way of making discord embeds better?

  23. 23

    Is there a better way to do Insertion Sort?

  24. 24

    Is there a better way to do this than echo?

  25. 25

    Better way to override a function definition

  26. 26

    Is there a better way to divide this string into substrings?

  27. 27

    What is the better way to use IP field (IPV4 or IPV6) in proto3 file for Golang and C# usage

  28. 28

    Use VPN to make upload better

  29. 29

    matplotlib scatter plots do not display when populated using for loop

ホットタグ

アーカイブ