简体   繁体   English

如何显示 matplotlib 图?

[英]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.我确信 python 的matplotlib配置是正确的,因为我用它来绘制一些数字。

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我在 Ubuntu 中使用 python 2.6,Eclipse

In matplotlib you have two main options: 在matplotlib中,您有两个主要选项:

  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 如果其他人在这里使用Jupyter笔记本电脑,你只需要

%matplotlib inline

Purpose of "%matplotlib inline" “%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: 当您在代码中完成所有初始化以便查看plot的complet版本时,必须使用show()方法:

import matplotlib.pyplot as plt

plt.plot(x, y)
................
................
plot.show()

plt.plot(X,y) function just draws the plot on the canvas. plt.plot(X,y)函数只是在画布上绘制图形。 In order to view the plot, you have to specify plt.show() after plt.plot(X,y) . 要查看绘图,必须在plt.plot(X,y)之后指定plt.show() 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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