简体   繁体   中英

How to show matplotlib plots?

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

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

您必须在末尾使用plt.show()才能看到图

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

%matplotlib inline

Purpose of "%matplotlib inline"

将图表保存为png

plt.savefig("temp.png")

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

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()
import numpy as np

import matplotlib.pyplot as plt

x1 = 5 * np.random.rand(50)

x2 = 5 * np.random.rand(50) + 25

x3 = 30 * np.random.rand(25)

x = np.concatenate((x1, x2, x3))

y1 = 5 * np.random.rand(50)

y2 = 5 * np.random.rand(50) + 25

y3 = 30 * np.random.rand(25)

y = np.concatenate((y1, y2, y3))

color_array = ['b'] * 50 + ['g'] * 50 + ['r'] * 25

plt.scatter(x, y, s=[50], marker='D', c=color_array)

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM