简体   繁体   English

matplotlib,savefig:忽略 DPI 设置

[英]matplotlib, savefig: DPI setting is ignored

I cannot find anyone else with this problem.我找不到其他人遇到这个问题。 In matplotlib, you can view your plots using either show() or savefig().在 matplotlib 中,您可以使用 show() 或 savefig() 查看您的绘图。 These generate slightly different images;这些生成的图像略有不同; in my case, the savefig() image is uglier and harder to understand.在我的例子中,savefig() 图像更丑陋且更难理解。 I need to make life easy for my examinator, so..我需要让考官的生活轻松一些,所以..

I found some topics that suggested I set the DPI size to match that of show().我发现一些主题建议我将 DPI 大小设置为与 show() 相匹配。 I have tried:我努力了:

-> Setting savefig.dpi directly with matplotlib.rcParams['savefig.dpi'] = 80. -> 直接使用 matplotlib.rcParams['savefig.dpi'] = 80 设置 savefig.dpi。

-> Setting savefig.dpi directly in ~/.matplotlib/matplotlibrc. -> 直接在 ~/.matplotlib/matplotlibrc 中设置 savefig.dpi。

-> Moving my rc file to CWD. -> 将我的 rc 文件移动到 CWD。

-> Finally, using savefig('image.pdf', dpi=80) -> 最后,使用 savefig('image.pdf', dpi=80)

I can verify that the attribute is indeed getting set;我可以验证该属性确实已设置; but it seems that the setting is igored by savefig().但似乎设置被 savefig() 忽略了。 Can anyone help with this?有人能帮忙吗?

(Simplified) code: (简化)代码:

plt.bar(ind, functimes, yerr=stddev, bottom=0, width=barWidth, align='center', color='b')

ax = plt.gca()
ax.legend(barRcts, barLegend)
plt.title("Function Call Overhead")
plt.xlabel("Function ID")
plt.ylabel("Execution Time [us]")

plt.xticks(ind, funcNames)
figtest.autofmt_xdate()

plt.savefig(out_file, dpi=80, format='pdf')

PDF saving uses the DPI setting for image resolution inside the PDF, not for any line/polygon resolution (which is meaningless in a vector format). PDF保存使用DPI设置进行PDF内的图像分辨率,而不是任何线/多边形分辨率(在矢量格式中无意义)。

If your happy with the output on screen, and don't need to have a scalable graphic, saving as png is probably fine. 如果您对屏幕上的输出感到满意,并且不需要具有可扩展的图形,那么保存为png可能就好了。

On your IDE, when you pass the cursor over the function plt.savefig() it shows the next: (function) savefig: (*args: Any, **kwargs: Any) it means that the function only accepts unlimited arguments (*args) and Keyworded Arguments (**kwargs) .在您的 IDE 上,当您将 cursor 通过 function plt.savefig()时,它会显示下一个: (function) savefig: (*args: Any, **kwargs: Any)这意味着 function 只接受无限制的 884225182 (*args) 004688) (*args)和关键字 Arguments (**kwargs) So, if you go the documentation of Matplotlib you will find the arguments you could use https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html .所以,如果你 go Matplotlib 的文档你会发现 arguments 你可以使用https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html

Now, if you only want to present a good quality plots you could do the next as an example.现在,如果你只想展示高质量的图,你可以做下一个例子。 Open a jupyter notebook on vscode and code plt.savefig("my_image.jpg", dpi = 300) [dpi stands for dots per inch, if you use a higher dpi you will get a high quality image] then with markdown show your plots with the following code:在 vscode 上打开一个 jupyter notebook 并编写代码plt.savefig("my_image.jpg", dpi = 300) [dpi 代表每英寸的点数,如果你使用更高的 dpi,你将获得高质量的图像] 然后用 markdown 显示你的图使用以下代码:

plt.plot(a,v_t_retraso, 'r', label = 'FP - retraso')
plt.plot(a,v_t_adelanto,'b', label = 'FP - adelanto')

plt.title('Caracteristicas de los terminales', fontdict={'fontname': 'Comic Sans MS', 'fontsize': 20})

# Escalando
plt.yticks([400,450,500,550])
plt.grid(True)
plt.legend()
plt.xlabel('Corriente en Linea, $A$')
plt.ylabel('Voltaje en los terminales, $V$')
plt.savefig('terminales_4.jpg', dpi=300)
plt.close()

plt.plot(v_t_retraso,a)
plt.xlabel('Corriente en Linea, $A$')
plt.ylabel('Voltaje en los terminales, $V$')
plt.savefig('terminales_5.jpg', dpi=300)
plt.close()

With the save images you could open a cell on markdown and write the following code:使用保存的图像,您可以在 markdown 上打开一个单元格并编写以下代码:

 <!-- If you want a full-size image then use the following code: -->
 <!-- ![](terminales_4.jpg)  ![](terminales_5.jpg) -->
 <!-- For custom size -->
<img src="terminales_4.jpg" alt="terminales_4" style="width:432px;"/>
<img src="terminales_5.jpg" alt="terminales_5" style="width:432px;"/>

 

Jupyter Notebook 上的代码示例

Finally you could export your code as HTML or Pdf and show as many graphs as you want.最后,您可以将代码导出为 HTML 或 Pdf 并显示任意数量的图表。 An example is shown in the next image.下图中显示了一个示例。

例子的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM