matplotlib:使用标记时的丑陋绘图
我有一个小问题。我有一个程序可以绘制势能的波函数,当我使用绘图(使用 pylab)'-' 中的选项时,它看起来很好,例如: http://img41.imageshack.us/img41/8798/59138635.png
如果我使用 'o' 我会得到: http://img16.imageshack.us/img16/3741/22378006.png
你看它看起来很丑:\
是否有一种简单的方法可以使圆圈间隔更大,或者这取决于代码的细节?
代码是:
from math import *
from scipy.special import *
from pylab import *
from scipy.linalg import *
firebrick=(178./255.,34./255.,34./255.)
indianred=(176./255.,23./255.,31./255.)
steelblue=(70./255.,130./255.,180./255.)
slategray1=(198./255.,226./255.,255./255.)
slategray4=(108./255.,123./255.,139./255.)
lavender=(230./255.,230./255.,230./255.)
cobalt=(61./255.,89./255.,171./255.)
midnightblue=(25./255.,25./255.,112./255.)
forestgreen=(34./255.,139./255.,34./255.)
#grid
Nmesh=512
L=4.0
dx=L/Nmesh
Xmax=L
x=arange(-L,L+0.0001,dx)
Npts=len(x)
numwav=2 #number of wave function that is being drawn
V=zeros([Npts],float)
for i in range(Npts):
V[i]=x[i]**4
a=zeros([2,Npts-2],float)
wave=zeros([Npts],float)
wave1=zeros([Npts],float)
encor=3.0/4*(3.0/4)**(1.0/3)
#numerical solution
for i in range(1,Npts-1,1):
a[0,i-1]= 1.0/dx**2+V[i] #diagonal elements
a[1,i-1]=-1.0/dx**2/2 #the elements below the diagonal
a[1,Npts-3]=-99.0 #element is not used
eig,vec=eig_banded(a,lower=1) #routine that diagonalizes the tridiagonal matrix
for i in range(1,Npts-1,1):
wave[i]=vec[i-1,numwav]
wave[0]=0.0 #wave function has the value zero on the first point on the grid
wave[Npts-1]=0.0 #wave function has the value zero on the last point on the grid
wave=150*wave+eig[numwav]
#potential graph
line=plt.plot(x,V)
plt.setp(line,color='firebrick',linewidth=2)
#plot of the selected level and wave function
plt.axhline(y=eig[numwav],linewidth=2,color='steelblue')
#plot of the points of the wave function
plt.plot(x,wave,"b-",linewidth=2,color='forestgreen')
plt.xlabel('x',size=16)
plt.ylabel('V(x)',size=16)
plt.axis([-4.0,4.0,-5.0,16.0]) #x and y axes range
plt.grid(True)
plt.show()
I have a little problem. I have a program that will draw wave function for a potential, and it looks fine when I use the option in plot (using pylab) '-' for instance: http://img41.imageshack.us/img41/8798/59138635.png
If I use 'o' i'll get:
http://img16.imageshack.us/img16/3741/22378006.png
You see that it looks ugly :\
Is there a simple way to make the circles more spaced, or does that depends on the details of the code?
The code is:
from math import *
from scipy.special import *
from pylab import *
from scipy.linalg import *
firebrick=(178./255.,34./255.,34./255.)
indianred=(176./255.,23./255.,31./255.)
steelblue=(70./255.,130./255.,180./255.)
slategray1=(198./255.,226./255.,255./255.)
slategray4=(108./255.,123./255.,139./255.)
lavender=(230./255.,230./255.,230./255.)
cobalt=(61./255.,89./255.,171./255.)
midnightblue=(25./255.,25./255.,112./255.)
forestgreen=(34./255.,139./255.,34./255.)
#grid
Nmesh=512
L=4.0
dx=L/Nmesh
Xmax=L
x=arange(-L,L+0.0001,dx)
Npts=len(x)
numwav=2 #number of wave function that is being drawn
V=zeros([Npts],float)
for i in range(Npts):
V[i]=x[i]**4
a=zeros([2,Npts-2],float)
wave=zeros([Npts],float)
wave1=zeros([Npts],float)
encor=3.0/4*(3.0/4)**(1.0/3)
#numerical solution
for i in range(1,Npts-1,1):
a[0,i-1]= 1.0/dx**2+V[i] #diagonal elements
a[1,i-1]=-1.0/dx**2/2 #the elements below the diagonal
a[1,Npts-3]=-99.0 #element is not used
eig,vec=eig_banded(a,lower=1) #routine that diagonalizes the tridiagonal matrix
for i in range(1,Npts-1,1):
wave[i]=vec[i-1,numwav]
wave[0]=0.0 #wave function has the value zero on the first point on the grid
wave[Npts-1]=0.0 #wave function has the value zero on the last point on the grid
wave=150*wave+eig[numwav]
#potential graph
line=plt.plot(x,V)
plt.setp(line,color='firebrick',linewidth=2)
#plot of the selected level and wave function
plt.axhline(y=eig[numwav],linewidth=2,color='steelblue')
#plot of the points of the wave function
plt.plot(x,wave,"b-",linewidth=2,color='forestgreen')
plt.xlabel('x',size=16)
plt.ylabel('V(x)',size=16)
plt.axis([-4.0,4.0,-5.0,16.0]) #x and y axes range
plt.grid(True)
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过进一步调查,我有一个更好的(但不同的)答案。 Matplotlib 提供了一个
markevery
关键字来允许在放置标记时取得进展。因此,如果您想要 20 个左右的可见点,则在绿线之上:如果您只需要标记,我之前的答案效果很好,但如果您同时想要线条和标记,则效果会更好。
After investigating further, I have a better (but different) answer. Matplotlib provides a
markevery
keyword to allow a stride in placing markers. So I would recommend, if you want 20 or so points for visibility, on top of a green line:My previous answer works fine if you only want markers, but this works much better if you want both lines and markers.
它看起来丑陋的原因是你的网格间距太细,无法使用标记进行绘制。要向线条添加标记,您可以做的就是仅将它们添加到每 10 个(或其他)点:
The reason it looks ugly is that your grid is too finely spaced for plotting with markers. What you can do to add markers to your line is to only add them to every 10 (or whatever) points: