减小矢量化轮廓图的大小 [英] Reducing size of vectorized contourplot

查看:129
本文介绍了减小矢量化轮廓图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在pdf文档中包含一个填充轮廓图(例如TeX文档)。
目前我正在使用 pyplot s contourf ,并使用 pdf > pyplot s savefig 。问题在于,与高分辨率 png 相比,图的大小变得相当大。



减小尺寸的一种方法当然是减少情节中的等级数量,但是太少的情节会导致情节不佳。我正在寻找一种简单的方法,例如让绘图的颜色保存为png,并将轴,刻度等以向量化方式保存。

解决方案

您可以使用 Axes 选项

任何与一个 zorder 小于设置的值将被保存为光栅化图形,即使保存为 pdf

例如:

pre $ import matplotlib.pyplot as plt
导入numpy为np

data = np.random.rand(500,500)

#fig1将contourf保存为矢量
fig1, ax1 = plt.subplots(1)
ax1.contourf(data)
fig1.savefig('vector.pdf')

#fig2将contourf保存为栅格
fig2,ax2 = plt.subplots(1)
ax2.contourf(data,zorde r = -20)
ax2.set_rasterization_zorder(-10)
fig2.savefig('raster.pdf')

#显示文件大小的差异。 os.stat().st_size以字节为单位给出文件大小。
print os.stat('vector.pdf')。st_size
#15998481
print os.stat('raster.pdf')。st_size
#1186334

您可以看到这个matplotlib示例以获取更多背景信息。

正如@tcaswell所指出的那样,




在不影响它的 zorder 的情况下光栅化一个艺术家,您可以使用 .set_rasterized 。但是,对于 contourf ,这似乎不是一个选项,所以您需要循环创建的 PathCollections 。由 contourf set_rasterized 在它们中的每一个上。就像这样:

  contours = ax.contourf(data)
for contours.collections中的pathcoll:
pathcoll.set_rasterized(True)


I would like to include a filled contour plot to a pdf document (for example a TeX document). Currently I am using pyplots contourf, and saving to pdf with pyplots savefig. The problem with this is that the size of the plots becomes rather big as compared to a high resolution png.

One way to reduce the size is of course to reduce the number of levels in the plot, but too few levels gives a poor plot. I'm searching for a simple way to for example let the colors of the plot be saved as a png, with the axes, ticks etc. to be saved vectorized.

解决方案

You can do this using the Axes option set_rasterization_zorder.

Anything with a zorder less than what you set that to be will be saved as rasterized graphics, even when saving to a vector format like pdf.

For example:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(500,500)

# fig1 will save the contourf as a vector
fig1,ax1 = plt.subplots(1)
ax1.contourf(data)
fig1.savefig('vector.pdf')

# fig2 will save the contourf as a raster
fig2,ax2 = plt.subplots(1)
ax2.contourf(data,zorder=-20)
ax2.set_rasterization_zorder(-10)
fig2.savefig('raster.pdf')

# Show the difference in file size. "os.stat().st_size" gives the file size in bytes.
print os.stat('vector.pdf').st_size
# 15998481
print os.stat('raster.pdf').st_size
# 1186334

You can see this matplotlib example for more background info.


As pointed out by @tcaswell, to rasterise just one artist without having to affect its zorder, you can use .set_rasterized. However, this doesn't appear to be an option with contourf, so you would need to loop over the PathCollections created by contourf and set_rasterized on each of them. Something like this:

contours = ax.contourf(data)
for pathcoll in contours.collections:
    pathcoll.set_rasterized(True)

这篇关于减小矢量化轮廓图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆