簡體   English   中英

在python上繪制多個圖形

[英]plotting multiple figures on python

我正在嘗試在同一圖形上繪制兩個地塊的多個地塊,但是僅包括前兩個地塊。 我原本希望有5個地塊

from numpy import *

from matplotlib.pyplot import *

x = linspace(-1, 1, 200)

y_0 = ones(len(x))
y_1 = x
y_2 = 1/2*(3*x**2-1)
y_3 = 1/2*(5*x**3-3*x)
y_4 = 1/8*(35*x**4-30*x**2+3)

figure()
plot(x, y_0)   # Plot some data
plot(x, y_1)   # Plot some data
plot(x, y_2)   # Plot some data
plot(x, y_3)   # Plot some data
plot(x, y_4)   # Plot some data

grid(True)   # Set the thin grid lines

savefig("plot_1.png")   # Save the figure to a file "png",
                                # "pdf", "eps" and some more file types are valid
show("plot_1.png")

當像y_1 = x之類的東西時,您並沒有在復制x,而只是指向同一對象的指針。

更重要的是,如果您使用的是python2

>>> 1/2*(3*x**2-1)
array([ 0.,  0.,  0.,  0.,  0., -0., -0., -0., -0., -0., -0., -0., -0.,
       -0., -0.,  0.,  0.,  0.,  0.,  0.])

因為1/2變成了零(我知道,關於python最煩人的事情之一)

因此,matplotlib可能會繪制多個圖形,但不會繪制您期望的位置。 放入一些print()命令以查看所擁有的內容並進行一般調試

如果是python 2問題,它將解決此問題

y_2 = 1.0/2*(3*x**2-1)
y_3 = 1.0/2*(5*x**3-3*x)
y_4 = 1.0/8*(35*x**4-30*x**2+3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM