Python 批处理matplotlib脚本不';不保存轴

Python 批处理matplotlib脚本不';不保存轴,python,batch-file,matplotlib,save,axes,Python,Batch File,Matplotlib,Save,Axes,更新:已解决;见评论 我对matplotlib有点陌生,在使用批处理脚本时,很难输出包含轴的绘图的图像或pdf文件 相关代码如下: fig=plt.figure() ax=fig.add_axes([0.1,0.1,0.75,0.75]) ax.set_xlabel("Radius") ax.set_ylabel("Average density") ax.plot(nr,ravs,'k-') # plt.plot(nr,ravs) # plt.ylabel("Ave

更新:已解决;见评论

我对matplotlib有点陌生,在使用批处理脚本时,很难输出包含轴的绘图的图像或pdf文件

相关代码如下:

  fig=plt.figure()  
  ax=fig.add_axes([0.1,0.1,0.75,0.75])
  ax.set_xlabel("Radius")
  ax.set_ylabel("Average density")
  ax.plot(nr,ravs,'k-')
#  plt.plot(nr,ravs)
#  plt.ylabel("Average density")
#  plt.xlabel("Radius")
  plt.savefig("outputs/diskgap/rl"+str(ng)+".jpg") 
nr是一个仅包含线性空间半径值的NumPy数组,ravs是一个包含平均密度值的标准1D数组;你可以用假数据进行复制。ng只是循环计数器。这是一个批处理脚本,用于从一系列输出文件夹中获取数据、打印并保存打印,同时进行一些分析和输出

我发现,当从IDLE或交互式iPython会话中运行时,这会产生与预期完全相同的输出,但当作为批处理脚本运行时(在终端中,键入“python polardisk.py”以运行脚本),我会得到以下输出:

我应该得到的是:

我尝试过摆弄add_axes或figure()参数,如alpha、transparency、frameon、visibility、edgecolor、linewidth等,但都没有用——我得到了相同的结果

我的直觉是,也许轴和标签正在绘制中,但是出于任何原因,它们被绘制为白色,而不是黑色。这是我以前在IDL中遇到过的问题。有什么想法吗

编辑:

这是这个例程访问的数据(只是样本运行,没有真正的科学价值),目的是试图完美地复制我的问题:

下面是完整的脚本:

from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np

# First, load data from density file into a 2D numpy array

plt.ioff()

ng = 1

masses = []
d=1
while d<=20:
  masses.append(d*0.0001)
  d+=1

gaps=[]

while ng <= 20:

  rho = np.fromfile("outputs/diskgap/gr"+str(ng)+"/gasdens1.dat").reshape(128,384) #or whatever your dimensions are--reshape is r,phi

# Now we need 1D arrays of radii and angles corresponding to each row and column of rho

  nphi = np.linspace(-3.14159265358979323844,3.14159265358979323844,num=384) #1D array of phi values
  nr = np.linspace(0.4,2.5,num=128) #1D array of r values -- set to whatever radial span you used

# Create our plot

  ax = plt.subplot(111,polar=True)
  ax.set_yticklabels([]) #Get rid of radii labels (optional)

# Create polar image

  ctf = ax.contourf(nphi,nr,rho,256,cmap=cm.jet) #256 is the number of colors--using fewer will mean fewer contour levels, and cm.jet is just the colormap.

# Make sure we have a central hole where we didn't have data, rather than drawing nonzero rmin down to center
# This will also plot the polar image onto the plot.

  plt.ylim(0,2.5) #2.5 is just rmax, set to whatever you used

# Add a colorbar (optional)

#plt.colorbar(ctf)

#Save the image, and you're done!

  plt.savefig("outputs/diskgap/gr"+str(ng)+".jpg",bbox_inches='tight')
  plt.savefig("outputs/diskgap/gr"+str(ng)+".pdf",bbox_inches='tight')
  print "Finished disk number "+str(ng)+"!"

  plt.close("all")

  ravs = []

  d=0
  while d<len(rho[:,0]):
    ravs.append(np.mean(rho[d,:]))
    d+=1

  fig=plt.figure()  
  ax=fig.add_axes([0.1,0.1,0.75,0.75])
  ax.set_xlabel("Radius")
  ax.set_ylabel("Average density")
  ax.plot(nr,ravs,'k-')
#  plt.plot(nr,ravs)
#  plt.ylabel("Average density")
#  plt.xlabel("Radius")
  plt.savefig("outputs/diskgap/rl"+str(ng)+".jpg")   
  fig=plt.figure()  
  ax=fig.add_axes([0.1,0.1,0.75,0.75])
  ax.set_xlabel("Radius")
  ax.set_ylabel("Average density")
  ax.plot(nr,ravs,'k-')  
#  plt.plot(nr,ravs)
#  plt.ylabel("Average density")
#  plt.xlabel("Radius")
  plt.savefig("outputs/diskgap/rl"+str(ng)+".pdf",bbox_inches='tight')

  gaps.append(ravs[38]/ravs[100])

  print "Finished analyzing radial profile and gap for set "+str(ng)+"!"
  plt.close("all")
  ng+=1

plt.plot(masses,gaps)
plt.ylabel("Percent deficiency")
plt.xlabel("Planet mass")
plt.savefig("outputs/diskgap/gaps.jpg",bbox_inches='tight')
plt.plot(masses,gaps)
plt.ylabel("Percent deficiency")
plt.xlabel("Planet mass")
plt.savefig("outputs/diskgap/gaps.pdf",bbox_inches='tight') 
从matplotlib导入pyplot作为plt
从matplotlib导入cm
将numpy作为np导入
#首先,将数据从密度文件加载到2D numpy数组中
plt.ioff()
ng=1
质量=[]
d=1

虽然dA评论:我试着在Ubuntu 14.04(Python 2.7.6、matplotlib 1.3.1、numpy 1.8.2)上复制这一点,但对我来说效果很好。可能尝试删除matplotlib可能存在的任何配置文件:
rm-r~/.cache/matplotlib~/.config/matplotlib
Hmm,这对我没有帮助。我也在使用matplotlib 1.3.1、numpy 1.8.2、python 2.7.6和Ubuntu 14.04。我将看看是否可以添加完整代码和一个数据文件;也许在我认为是相关代码之外还有其他东西把事情搞砸了。我发现了。我用来运行代码的包中包含一个matplotlib配置文件,当我在包的目录树中运行脚本时,该文件最终位于Python的搜索路径中。我在那个文件的末尾插入了一个.backup,问题就解决了。经验教训,孩子们:总是寻找隐藏在你最不希望看到的地方的偷偷摸摸的配置文件。评论:我曾试图在Ubuntu 14.04(Python 2.7.6、matplotlib 1.3.1、numpy 1.8.2)上复制这一点,但对我来说效果很好。可能尝试删除matplotlib可能存在的任何配置文件:
rm-r~/.cache/matplotlib~/.config/matplotlib
Hmm,这对我没有帮助。我也在使用matplotlib 1.3.1、numpy 1.8.2、python 2.7.6和Ubuntu 14.04。我将看看是否可以添加完整代码和一个数据文件;也许在我认为是相关代码之外还有其他东西把事情搞砸了。我发现了。我用来运行代码的包中包含一个matplotlib配置文件,当我在包的目录树中运行脚本时,该文件最终位于Python的搜索路径中。我在那个文件的末尾插入了一个.backup,问题就解决了。经验教训,孩子们:总是寻找隐藏在你最意想不到的地方的秘密配置文件。