How to save a plot as a jpeg file?

In Matlab, if you want to save your plot (“h” is the figure handle) as a jpg file with a resolution of 600pixel. Below is all it takes:

File_W = "/path/test.jpg";
exportgraphics(h, File_W, 'Resolution', 600);

Does Julia offer such a solution as well? BTW, I’m making these plots using the GMT.jl package.

As a Matlab person, I find it very interesting that the GMT.jl package does not use any kind of figure handle. Is this a Julia thing? Then how do you refer to a figure when you try to save it in an automatic fashion?

I don’t know anything about GMT, but it seems to have pretty extensive documentation which discusses outputformats here, giving the example

psconvert(in_memory=true, adjust=true, format=:f, out_name="myfig.pdf")

As for not having an explicit figure object, that’s not a Julia thing, at least not for the two most widely used plotting packages Plots and Makie. Both allow plotting with or without assigning the plot/figure object explicitly (in Plots it would be something like myplot = plot(x, y) and in Makie I believe it’s myfig, myaxis = plot(x, y))

1 Like

Many thanks for pointing the right direction! I’ll check out the GMT.jl documentation then.

I thought this would be a generic Julia function like in Matlab, but it seems to be package specific.

Unfortunately, it is a solution for Windows only! This is absurd, because I can manually save them as a jpeg file without any issues on my Mac.

Does anyone have a solution for Mac users? I plan to make ~400 of such plots. Manually saving them will be impossible.

Linking this post by @joa-quim.

:astonished::astonished::astonished: ??? Where did you get that idea? Doesn’t the documentation say that one can either use fmt=:jpg or savefig="myPlot.jpg"

2 Likes

As I understand it, GMT.jl is a Julia wrapper around GMT which is written in C / C++ and uses PostScript (Generic Mapping Tools - Wikipedia).

I am relatively new to Julia. What I found is that the Julia Plotting World is very colorful and even packages like Gadfly and Makie that I understand are written fully in Julia are handling the same things slightly different. So in general, you have to familiarize yourself with each package and learn some of their intricacies. As a side not, you may also take a look at Plots.jl which is doing some kind of “integration” over various plotting backends, and is providing a common interface.

I do not know if GMT is better or worse to above mentioned packages, what I think is that it is handling things very different to Gadfly and Makie under the hood as I understand due to its C / C++ origin. As for PostScipt, some time ago, you could hardly go any better than using it in the professional printing world, however, this might have changed over the recent years. Even though, I guess PostScript might offer some advantages. Please see How to create layouts in Julia plotting packages? - #11 by joa-quim

Im not sure if I fully understand what you want to do. As for know I am assuming you want to save a plot. If so, I understand the option savefig="figname" should be working: What is your favorite mapping package? - #22 by joa-quim

EDIT: I just spotted the question was answered by @joa-quim

1 Like

I was referring to the psconvert as mentioned by another member:
https://www.generic-mapping-tools.org/GMT.jl/dev/modules/#The-output-format

Glad to hear this can be done as simple as:
either use fmt=:jpg or savefig="myPlot.jpg"

Just tested it and it works like a charm!

Many thanks as always!

Quoting from that link.

An interesting alternative is to set FMT=“” , that is to not specify any image format. This will result in NOT saving any file on disk but to keep the PS figure internally stored in the program’s memory. In other words, the figure is built and kept in memory only. This allows converting to another format directly without the use of an intermediary disk file.

The issue with this solution, that could be implemented internally without user intervention, is that it currently only works on Windows.

It’s fmt="" that only works on Windows. Isn’t that clear form the text?

2 Likes

Similar as in How to create layouts in Julia plotting packages? - #2 by aramirezreyes
The question about how to do something related to plotting in julia doesnt make a lot of sense. Every package has different design and implementations. So, like in the other thread how do you save a figure will be different depending on the package. So no, not a julia thing.

But in practice they tend to be very similar :slight_smile:

However, it would be probably potentially extremally useful to have a concise, very short tutorial about the basic, most commonly used functions and workflows of the GMT.jl, on the GMT.jl welcome page, especially that the documentation is so extensive. This is of course written with a big smile. :slight_smile:

Yes, I understand that. But I struggle to on the same time add more in depth documentation to many modules that are lacking them, more examples, etc.
Do you think porting the GMT tutorial to GMT.jl syntax would fill that gap?

1 Like

@joa-quim, searching the GMT.jl doc for savefig, there was only one hit and the place seemed crowded. Maybe including some simple example as you provided here?

NB: and sorry if overlooked something in the good docs provided

That would be awesome.

When I search for savefig in the examples of the doc subdirectory, I find several occurrences. But yep, should add more and specially also inform that figname or only name are synonyms of savefig.

1 Like

Frankly, I was thinking about something simpler, like a “two-pager summary”.

Very basic things, opening the file (what are supported formats), making a plot (what are basic plot options), saving a figure (what are supporting output formats) and for example a basic semi-advanced topics like how to make a plot with use of more than one file, how to make a stack of two plots. It would be great to see the examples of corresponding code and output figures.

Something that would make an entry barrier for a new GMT.jl user lower and rally not longer than two pages. I think that is is always great to type a web page address of a new package and be able to do some basics after a short read. Later the user, can always get more knowledge about the package and do more advanced things after reading the full documentation.

Edit: I just spotted Some examples · GMT. I was thinking about this kind of thing but I was looking for it directly at the main github webpage (GitHub - GenericMappingTools/GMT.jl: Generic Mapping Tools Library Wrapper for Julia).

Thanks for this suggestion. I’ll start wondering on the design of that in the betweens of my (but not only me) 4 disciplines this semester. But I’ll have to make tinny figures so that they all fit in those two pages :laughing:

Just the basics. Two pager. I know THE GMT.JL is a specialized package, used predominantly by very well educated people, however, with full respect, there is usually, no need for consultations outside teaching hours. :slight_smile:

@joa-quim, do you know how I could change the resolution of such maps? For example, may I set it as 600 pixel like in my Matlab example?

I tried to use the resolution=:full argument, but got the below error:
the following options were not consumed in grdimage => [:resolution]

I also noticed the dpi option, should I use it only on the last plotting command? I have many grdimage plotting commands, plus scatter, and text.