Plots with Good Resolution for Printing and Screen Display

Plots with good resolution for printing and screen display

There is a small error in your original png command. Try this:

png(file="mag_feb.png", units="in", width=11, height=8.5, res=300)

Now, width and height are in inches, and res is in pixels/inch. Before, the res parameter was being ignored.

How to display python plots at real resolution on screen

I have found a solution that works for me. As mentioned in the comments of my questions the Windows scaling factor plays a role in the a gui's size regardless of dpi. Taking this into account by multiplying the dpi by say 100/150 for a 150% scaling factor resolves the issue I had on the 4k screen. I did notice on a 1080p screen this scaling factor does not apply.

The standard plt.show() function seems to work well so long as there is enough room on the screen to present the plot, otherwise it will scale down.

I did figure out a way to lock the resolution in by using the tkinter library. By simply getting the image resolution, and then setting the tkinter gui to that size. Then setting image as the background. The window can of course be locked so it cannot be scaled or stretched. Even if the image is larger than the screen it will not scale window fit which is exactly what I need. I've included my code below.

import tkinter as tk
from PIL import Image

window = tk.Tk()

window.title("Image Viewer")

backPath = 'plot.png'
img = Image.open(backPath)
w, h = img.size

window.resizable(width = False, height = False)
window.geometry(str(w) + 'x' + str(h))

background_image = tk.PhotoImage(file = backPath)
background_label = tk.Label(window, image=background_image)
background_label.place(x=0, y=0)

window.mainloop()

How can I increase the resolution of my plot in R?

There are several possible answers to this question, and the comments are being careful to try to teach you where to find the answer vice just giving it to you. I'll spoil that by providing that to which they allude, but also present another question:

  1. The answer: use res=300 (or higher) in your call to png. From help(png), it states that the default ppi (pixels per inch) is 72, below what you want for fine-resolution graphs. 300 is decent for most printers and screens, YMMV.

    png(filename="simple_graphic.png", res=300) # perhaps width/height as well
    # ...
    dev.off()
  2. Another question: in what format can I best show my graph? This is often overlooked by many people making graphs with R, and is unfortunate but easy to remedy: use a vector-based image format. If you are using Powerpoint or Word, then you may be better off making an enhanced metafile format (EMF) file.

    install.packages('devEMF') # just once
    library(devEMF)
    emf(filename="simple_graphic.emf", width=1024, height=800)
    # ...
    dev.off()

    If you are able to use the image in a scalable vector graphic (SVG) format (e.g., Inkscape, Scribus, ...), then merely use svg(...) in place of emf(...). Same options/rules apply.

    Similarly, if you're LaTeX, you can go straight to pdf using pdf(...) (same syntax).

NB: one benefit of going to one of these vector-based (vice raster-based such as PNG) image formats is that if you want/need to finesse the graph in ways that you don't know how in R, you can open the EMF/SVG/PDF in something like Inkscape in order to easily modify lines (color, thickness), text (font, size, location), boxes, arrows, etc. And Inkscape will allow you to export into EMF, SVG, PDF, PNG, ...

Exporting high resolution plots

You can try exporting plots as bitmap images; e.g. as a PNG

png("example.png", height = 4, width = 7, units = "in", res = 300)
plot(density(rnorm(100)))
dev.off()

Try playing around with the parameters height, width and res to achieve dimensions and a resolution conform with the publishing guidelines.

Also see ?png for more details.

R plot: size and resolution

A reproducible example:

the_plot <- function()
{
x <- seq(0, 1, length.out = 100)
y <- pbeta(x, 1, 10)
plot(
x,
y,
xlab = "False Positive Rate",
ylab = "Average true positive rate",
type = "l"
)
}

James's suggestion of using pointsize, in combination with the various cex parameters, can produce reasonable results.

png(
"test.png",
width = 3.25,
height = 3.25,
units = "in",
res = 1200,
pointsize = 4
)
par(
mar = c(5, 5, 2, 2),
xaxs = "i",
yaxs = "i",
cex.axis = 2,
cex.lab = 2
)
the_plot()
dev.off()

Of course the better solution is to abandon this fiddling with base graphics and use a system that will handle the resolution scaling for you. For example,

library(ggplot2)

ggplot_alternative <- function()
{
the_data <- data.frame(
x <- seq(0, 1, length.out = 100),
y = pbeta(x, 1, 10)
)

ggplot(the_data, aes(x, y)) +
geom_line() +
xlab("False Positive Rate") +
ylab("Average true positive rate") +
coord_cartesian(0:1, 0:1)
}

ggsave(
"ggtest.png",
ggplot_alternative(),
width = 3.25,
height = 3.25,
dpi = 1200
)

Saving images in Python at a very high quality

If you are using Matplotlib and are trying to get good figures in a LaTeX document, save as an EPS. Specifically, try something like this after running the commands to plot the image:

plt.savefig('destination_path.eps', format='eps')

I have found that EPS files work best and the dpi parameter is what really makes them look good in a document.

To specify the orientation of the figure before saving, simply call the following before the plt.savefig call, but after creating the plot (assuming you have plotted using an axes with the name ax):

ax.view_init(elev=elevation_angle, azim=azimuthal_angle)

Where elevation_angle is a number (in degrees) specifying the polar angle (down from vertical z axis) and the azimuthal_angle specifies the azimuthal angle (around the z axis).

I find that it is easiest to determine these values by first plotting the image and then rotating it and watching the current values of the angles appear towards the bottom of the window just below the actual plot. Keep in mind that the x, y, z, positions appear by default, but they are replaced with the two angles when you start to click+drag+rotate the image.



Related Topics



Leave a reply



Submit