问题描述
我想知道是否有办法改变 sympy 图的像素密度/分辨率.例如,让我们考虑下面的简单代码片段:
I am wondering if there is a way to change the pixel density/resolution of sympy plots. For example, let's consider the simple code snippet below:
import sympy as syp
x = syp.Symbol('x')
miles_to_km = x * 1.609344
miles_to_km.evalf()
graph = syp.plot(miles_to_km, show=False)
graph.save('./figures/miles_to_km.png')
graph.show()
当我尝试使用
graph.savefig
时,我得到了一个AttributeError: 'Plot' object has no attribute 'saveimage'
我偶然发现了一些在线资源中的 saveimage
方法,听起来这是常见的方法——我猜 API 改变了吗?
When I tried using
graph.savefig
, I got anAttributeError: 'Plot' object has no attribute 'saveimage'
I stumbled upon thesaveimage
method in some online resource, and it sounded like that this was the common approach -- I guess the API changed?
graph.save('./figures/miles_to_km.png', dpi=300)
产生类型错误:TypeError: save() got an unexpected keyword argument 'dpi'
在 plot
中使用 dpi
属性不会引发任何错误,但也不会影响图像质量:graph = syp.plot(英里到公里,dpi=300,显示=假)
Using the dpi
attribute in plot
does not throw any error but doesn't affect the image quality either: graph = syp.plot(miles_to_km, dpi=300, show=False)
我也尝试过使用 matplotlib 后端:
I also tried using the matplotlib backend:
plt.figure()
graph = syp.plot(miles_to_km, show=False)
#graph.save('./figures/miles_to_km.png')
plt.savefig('./figures/miles_to_km.png')
graph.show()
其中 plt
= matplotlib.pyplot
.但是,画布是空白的.另外相关信息可能是我在启用 %matplotlib inline
的 IPython notebook 中运行它.
where plt
= matplotlib.pyplot
. However, the canvas is blank. Also relevant info may be that I am running it in an IPython notebook with %matplotlib inline
enabled.
- 我使用的是 SymPy v. 0.7.6
下面的 backend
解决方法显示了 IPython 笔记本中的绘图,但它也会生成一个白色画布(作为 png)
the backend
workaround below shows the plot in the IPython notebook, but it also produces a white canvas (as png)
graph = syp.plot(miles_to_km, show=False)
backend = graph.backend(graph)
backend.fig.savefig('ch01_2.png', dpi=300)
backend.show()
感谢 Cody Piersall 的回答,问题现已解决.我更新到 IPython 4.0 (Jupyter notebook) 并绘制如下
Thanks to Cody Piersall's answer the issue is now resolved. I updated to IPython 4.0 (Jupyter notebook) and plotted it as follows
graph = syp.plot(miles_to_km, show=False)
backend = graph.backend(graph)
backend.process_series()
backend.fig.savefig('miles_to_km.png', dpi=300)
backend.show()
推荐答案
假设你使用的是 matplotlib 后端,如果你安装了 matplotlib,这是默认的,你只需要 import matplotlib.pyplot
并使用 pyplot.savefig
.
Assuming you are using the matplotlib backend, which is the default if you have matplotlib installed, you just have to import matplotlib.pyplot
and use pyplot.savefig
.
这是有效的,因为 sympy 使用 matplotlib 进行绘图,而且由于 matplotlib 是有状态的,它知道您正在处理哪个绘图.
This works because sympy uses matplotlib to do its plotting, and since matplotlib is stateful, it knows which plot you're working with.
这是您的示例,但使用 savefig 保存为 png.
Here is your example, but using savefig to save to a png.
import sympy as syp
x = syp.Symbol('x')
miles_to_km = x * 1.609344
miles_to_km.evalf()
graph = syp.plot(miles_to_km, show=False)
# Does not work in IPython Notebook, but works in a script.
import matplotlib.pyplot as plt
plt.savefig('./figures/miles_to_km.png', dpi=300)
如果您在 IPython notebook 中,上述操作将不起作用,但您仍然可以使用指定的 dpi 保存它们.你只需要对它有点棘手.
If you are in an IPython notebook, the above will not work, but you can still save them with a specified dpi. You just have to be a little tricky about it.
# works in IPython Notebook
backend = graph.backend(graph)
ackend.fig.savefig('300.png', dpi=300)
backend.fig.savefig('20.png', dpi=20)
这篇关于使用特定分辨率/像素密度保存 Python SymPy 图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!