java 画图保存图片_将绘图保存到图像文件,而不是使用Matplotlib显示它

回答(15)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我使用了以下内容:

import matplotlib.pyplot as plt

p1 = plt.plot(dates, temp, 'r-', label="Temperature (celsius)")

p2 = plt.plot(dates, psal, 'b-', label="Salinity (psu)")

plt.legend(loc='upper center', numpoints=1, bbox_to_anchor=(0.5, -0.05), ncol=2, fancybox=True, shadow=True)

plt.savefig('data.png')

plt.show()

f.close()

plt.close()

我发现在保存图形后使用plt.show非常重要,否则它将无法工作 . figure exported in png

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果像我一样使用Spyder IDE,则必须禁用交互模式:

plt.ioff()

(此命令随科学启动自动启动)

如果要再次启用它,请使用:

plt.ion()

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

#write the code for the plot

plt.savefig("filename.png")

该文件将保存在与运行的python / Jupyter文件相同的目录中

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

import datetime

import numpy as np

from matplotlib.backends.backend_pdf import PdfPages

import matplotlib.pyplot as plt

# Create the PdfPages object to which we will save the pages:

# The with statement makes sure that the PdfPages object is closed properly at

# the end of the block, even if an Exception occurs.

with PdfPages('multipage_pdf.pdf') as pdf:

plt.fi

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值