How to show matplotlib plots in python

59

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

This question is tagged with python matplotlib

~ Asked on 2011-12-20 11:57:50

The Best Answer is


82

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')
    

~ Answered on 2011-12-20 12:38:56


30

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

~ Answered on 2011-12-20 12:00:04


Most Viewed Questions: