[英]Saving high-resolution images with plotnine
I'm trying to use plotnine to save a high-resolution png image.我正在尝试使用 plotnine 来保存高分辨率的 png 图像。
With a test dataset, this looks like:使用测试数据集,这看起来像:
from plotnine import *
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['x'] = np.arange(0,10,0.01)
df['y'] = np.sin(df['x'])
p = ggplot(df, aes(x='x',y='y')) + labs(x='x', y='y') + geom_point(size=0.1)
p.save(filename = 'test3.png', height=5, width=5, units = 'in', dpi=1000)
This produces a low-resolution .png file containing my plot, which is not improved when I increase the specified dpi.这会生成一个包含我的绘图的低分辨率 .png 文件,当我增加指定的 dpi 时,它不会得到改善。
I've also tried saving with:我也尝试过保存:
ggsave(plot=p, filename='test.png', dpi=1000)
and replacing dpi=1000
with res=1000
.并将
dpi=1000
替换为res=1000
。 This produces identical low-resolution png files.这会产生相同的低分辨率 png 文件。
How can I save my plot at the resolution I want?如何以我想要的分辨率保存我的绘图?
Edit: This bug is resolved in plotnine version 0.3.0.编辑:此错误已在 plotnine 0.3.0 版中解决。 and the above code works correctly.
并且上面的代码可以正常工作。
Since this still isn't answered and I just got directed here, too... 既然这还没有回答,我也被引导到这里......
According to @has2k1 (the author of plotnine
), this was a bug and is now resolved. 据@ has2k1(笔者
plotnine
),这是一个错误,现在已经得到解决。 This commit looks like it might be the referenced fix. 此提交看起来可能是引用的修复。
To solve the issue, ensure you are using the git version or at least version 0.3.0 . 要解决此问题,请确保您使用的是git版本或至少使用0.3.0版 。
There is also the possibility to save the matplotlib figure也有可能保存 matplotlib 图
import plotnine as pn
fig, plot = (pn.ggplot()
+ ...
+ pn.theme(panel_background=pn.element_blank())
+ pn.theme(axis_title_y=pn.element_blank())
+ pn.theme(axis_ticks_major_y=pn.element_blank())
+ pn.theme(figure_size=(12, 8))
).draw(show=False, return_ggplot=True)
fig.savefig('image.png', dpi=300)
which worked ok for me.这对我来说没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.