# Show PyPlot

**URL:** <https://discourse.julialang.org/t/show-pyplot/91166>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 3, 2022, 1:27am UTC](https://discourse.julialang.org/t/show-pyplot/91166 "2022-12-03T01:27:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [December 3, 2022, 1:27am UTC](https://discourse.julialang.org/t/show-pyplot/91166/1 "2022-12-03T01:27:21Z")

</div>

Hello everyone! Please, I need help with a simple task:  
x = plot(…); …; close()  
How to show the graph again using x?  
I use Julia 1.8.3 e PyPlot

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [December 3, 2022, 2:05am UTC](https://discourse.julialang.org/t/show-pyplot/91166/2 "2022-12-03T02:05:23Z")

</div>

`display(x)` should show the plot again.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [December 3, 2022, 2:11am UTC](https://discourse.julialang.org/t/show-pyplot/91166/3 "2022-12-03T02:11:02Z")

</div>

This text appears in the terminal:  
1-element Vector{PyCall.PyObject}:  
PyObject \<matplotlib.lines.Line2D object at 0x7f6dbc6dadd0\>  
But it does not show the graph window.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [December 3, 2022, 2:04pm UTC](https://discourse.julialang.org/t/show-pyplot/91166/4 "2022-12-03T14:04:06Z")

</div>

Note: I had to install PyCall with the ENV[“PYTHON”]=“” option

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [December 4, 2022, 7:45am UTC](https://discourse.julialang.org/t/show-pyplot/91166/5 "2022-12-04T07:45:34Z")

</div>

If you are calling your plotting code directly in Python, I think you could do something like:

```julia
using PyCall

plt=pyimport("matplotlib.pyplot")
plt.show(x)

```

Which should work as your plot looks like it’s a `PyObject`.

p.s. I would recommend looking into using `Plots.jl` instead, as you can use the `PyPlot` backend easily:

```julia
using Plots
pyplot()

plt = plot(...)
display(plt)

```

And you can also switch to other backends if you need, usually without changing any of your code.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [December 4, 2022, 5:58pm UTC](https://discourse.julialang.org/t/show-pyplot/91166/6 "2022-12-04T17:58:41Z")

</div>

> [@jmair](#):
>
> If you are calling your plotting code directly in Python,

```julia
using PyPlot # At the REPL
x = plot(rand(10), rand(10))

```

My question is more to understand than to do. I found that show(x) or display(x) could redisplay a graph from the variable x. I think I already did it using PyPlot, but I don’t remember exactly. I used Plots when I started in Julia, today I prefer not to use it anymore. Thank you so much.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 4, 2022, 7:06pm UTC](https://discourse.julialang.org/t/show-pyplot/91166/7 "2022-12-04T19:06:36Z")

</div>

> [@marllos](#):
>
> My question is more to understand than to do. I found that show(x) or display(x) could redisplay a graph from the variable x. I think I already did it using PyPlot, but I don’t remember exactly. I used Plots when I started in Julia, today I prefer not to use it anymore. Thank you so much.

With PyPlot, you need to call `display` on the _figure_ object, which is _not_ what is returned by `plot` in Matplotlib. You can get the current figure with `gcf()`, for example.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [December 5, 2022, 4:43am UTC](https://discourse.julialang.org/t/show-pyplot/91166/8 "2022-12-05T04:43:45Z")

</div>

I will answer a little later.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [December 6, 2022, 5:28pm UTC](https://discourse.julialang.org/t/show-pyplot/91166/9 "2022-12-06T17:28:02Z")

</div>

I updated to Fedora 37 and it looks like PyCall can’t use python 3.11 so I did the ENV[“PYTHON”] = “” procedure, suggested by ufechner7 ([PyPlot stopped working when upgrading to Fedora 37 - #2 by ufechner7](https://discourse.julialang.org/t/pyplot-stopped-working-when-upgrading-to-fedora-37/90485/2)) and it worked. Then I noticed that show() didn’t redisplay graphs. I don’t know if it’s related!

```julia
# at the bash terminal
using PyPlot

# try 1
plot( collect(1:100), rand(100) .^ 50 )
# close the figure manually
# I type show(): blank line

# try 2
x = plot( collect(1:100), rand(100) .^ 50 )
# close the figure manually
# I type show(), ans: blank line
# I type show(x), ans: PyCall.PyObject[PyObject <matplotlib.lines.Line2D object at 0x7f9f4eed92d0>]
# I type display(x), ans: 1-element Vector{PyObject}: \n PyObject <matplotlib.lines.Line2D object at 0x7f9f4eed92d0>

# try 3
x = figure()
plot( collect(1:100), rand(100) .^ 50 )
# close the figure manually
# I type display(x), show(x), show(), ans: almost the same, but no figure

# a curious fact (for me)
using Serialization

x = plot( collect(1:100), rand(100) .^ 50 )
data = (text="tests", img=x)
serialize("data.bin", data)
# close the figure manually
data = deserialize("data.bin")
# The figure is redisplayed, without any other command!

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [December 6, 2022, 5:55pm UTC](https://discourse.julialang.org/t/show-pyplot/91166/10 "2022-12-06T17:55:51Z")

</div>

I know you can use Python 3.11 at least with PythonCall.jl (it downloads it for you by default, you can also point to it). I would use it for most everything now, unless some package uses PyCall (you can use both).

PythonPlot.jl uses PythonCall.jl, and it’s a fork of PyCall.jl, and apparently the future too. I tried plotting, with it it worked for me.
