Skip to main content
 首页 » 编程设计

python之保存在 python 中使用 peek() 显示的图像

2025年05月04日27mq0036

  import matplotlib.pyplot as plt 
  import sunpy.spectra 
  import sunpy.data.sample 
  from sunpy.spectra.sources.callisto import CallistoSpectrogram 
  image = CallistoSpectrogram.read(sunpy.data.sample.CALLISTO_IMAGE) 
  image.peek() 

那么如何使用命令保存这张图片呢?

请您参考如下方法:

我没有 SunPy 环境来测试它,但你能试一试吗?

import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
import sunpy.spectra 
import sunpy.data.sample 
from sunpy.spectra.sources.callisto import CallistoSpectrogram 
image = CallistoSpectrogram.read(sunpy.data.sample.CALLISTO_IMAGE) 
image.plot() 
plt.savefig('myfig') 

基于 Generate images without having a window appear :

The easiest way to do this is use a non-interactive backend (see What is a backend?) such as Agg (for PNGs), PDF, SVG or PS. In your figure-generating script, just call the matplotlib.use() directive before importing pylab or pyplot:

import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
plt.plot([1,2,3]) 
plt.savefig('myfig')