Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(). These generate slightly different images; in my case, the savefig() image is uglier and harder to understand. 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(). I have tried:

-> Setting savefig.dpi directly with matplotlib.rcParams['savefig.dpi'] = 80.

-> Setting savefig.dpi directly in ~/.matplotlib/matplotlibrc.

-> Moving my rc file to CWD.

-> Finally, using 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(). 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')
like image 905
memstick Avatar asked Apr 12 '12 06:04

memstick


People also ask

What is dpi in Savefig?

What is dpi in Savefig? savefig(filename, dpi=None) to save a matplotlib. pyplot figure as an image named filename with a resolution of dpi in dots per inch. A high dpi value such as 200 or more indicates a high resolution image, while a low dpi value such as 10 indicates a lower resolution image.

What is matplotlib default dpi?

resolution of the figure. If not provided, defaults to rcParams["figure. dpi"] (default: 100.0) = 100 .

How do I increase resolution in matplotlib?

To improve matplotlib image quality we can use greater dot per inch i.e dpi value (greater than 600) and pdf or . eps format can be recommended.


2 Answers

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).

If your happy with the output on screen, and don't need to have a scalable graphic, saving as png is probably fine.

like image 126
pelson Avatar answered Sep 27 '22 21:09

pelson


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). 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.

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:

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:

 <!-- 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;"/>

 

Code Example on Jupyter Notebook

Finally you could export your code as HTML or Pdf and show as many graphs as you want. An example is shown in the next image.

Result of the example

like image 39
Franko Dimitrios Sanchez Betan Avatar answered Sep 27 '22 21:09

Franko Dimitrios Sanchez Betan