63

I am sure the configuration of matplotlib for python is correct since I have used it to plot some figures.

But today it just stop working for some reason. I tested it with really simple code like:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 5, 0.1)
y = np.sin(x)
plt.plot(x, y)

There's no error but just no figure shown up.

I am using python 2.6, Eclipse in Ubuntu

martineau
  • 112,593
  • 23
  • 157
  • 280
manxing
  • 2,815
  • 12
  • 43
  • 52
  • 1
    @joaquin I like that, maybe also: How do I show, display matplotlib plots in python? – Yann Dec 20 '11 at 16:02

6 Answers6

85

In matplotlib you have two main options:

  1. Create your plots and draw them at the end:

    import matplotlib.pyplot as plt
    
    plt.plot(x, y)
    plt.plot(z, t)
    plt.show()
    
  2. Create your plots and draw them as soon as they are created:

    import matplotlib.pyplot as plt
    from matplotlib import interactive
    interactive(True)
    
    plt.plot(x, y)
    raw_input('press return to continue')
    
    plt.plot(z, t)
    raw_input('press return to end')
    
joaquin
  • 78,380
  • 27
  • 136
  • 151
  • 3
    For python 3.x users, use `input` in place of `raw_input`: https://stackoverflow.com/a/35168534/2711811. –  Feb 01 '20 at 16:17
34

You must use plt.show() at the end in order to see the plot

Ani Menon
  • 25,420
  • 16
  • 92
  • 119
George
  • 5,098
  • 15
  • 78
  • 148
12

In case anyone else ends up here using Jupyter Notebooks, you just need

%matplotlib inline

Purpose of "%matplotlib inline"

Wassadamo
  • 874
  • 8
  • 19
10

Save the plot as png

plt.savefig("temp.png")
Ani Menon
  • 25,420
  • 16
  • 92
  • 119
Lava Sangeetham
  • 2,474
  • 2
  • 30
  • 46
3

plt.plot(X,y) function just draws the plot on the canvas. In order to view the plot, you have to specify plt.show() after plt.plot(X,y). So,

import matplotlib.pyplot as plt
X = //your x
y = //your y
plt.plot(X,y)
plt.show()
Akshaya Natarajan
  • 1,605
  • 13
  • 16
2

You have to use show() methode when you done all initialisations in your code in order to see the complet version of plot:

import matplotlib.pyplot as plt

plt.plot(x, y)
................
................
plot.show()
HISI
  • 4,209
  • 3
  • 30
  • 45