14
$\begingroup$

Bug introduced in 9.0 persisting through 13.2 or later


I've evaluated this code:

plot = Plot[x, {x, 0, 10}];
g = Rasterize[plot, ImageResolution -> 300];

Everything looks good except the ticks (on the axes); they are invisible! It seems that Mathematica draws ticks in a default size on the scaled image, and after the image is scaled down, they are made too small to be distinguished at all.

The more ImageResoltion I specify, the smaller the ticks are and the less pleasing the image is.

How can I get the axes ticks to look right?

$\endgroup$
3
  • 2
    $\begingroup$ You've tried tweaking ImageResolution and RasterSize at the same time? Rasterize[Plot[x, {x, 0, 10}], ImageResolution -> 512, RasterSize -> 512] looks good to me... $\endgroup$ Commented Nov 18, 2012 at 23:42
  • $\begingroup$ Well, I don't really know why and don't know whether it's intentional but simultanesous use of ImageResolution and RasterSize have no use in my case, seems that Raster Size "kills" ImageResolution. $\endgroup$
    – DimG
    Commented Nov 19, 2012 at 0:43
  • 4
    $\begingroup$ Current thread with solutions for recent Mathematica versions: "Export high resolution figure causes missing ticks." $\endgroup$ Commented Mar 10, 2016 at 14:25

2 Answers 2

18
$\begingroup$

There is a hack that has some added advantages - in particular as a by-product you also get a vector graphics version of your plot that can be resized while keeping all relative sizes at the correct proportions:

plot = Plot[x, {x, 0, 10}];
h = First@ImportString[ExportString[plot, "PDF"]];

Rasterize[h, ImageResolution -> 300]

raster

The result has all the ticks at the same relative size as in the notebook. Moreover, the intermediate graphic in h contains the plot as if it had been exported to PDF and re-imported, i.e., it's a vector graphic but structured in a way that doesn't allow Mathematica to revise its tick marks etc.

$\endgroup$
6
  • $\begingroup$ A great trick:) One question: what can I do with axe/ticks color intensity of rasterized version? As I see that, if, let's say, I run this code: Rasterize[ First@ImportString[ExportString[Show[plot, ImageSize -> 100], "PDF"]] , ImageResolution -> 500] I get image with preserved proportion but axe ant ticks line width is not the same proportional. If I scale the rasterized version up to 5 times I see axes and ticks with line width as If they appear on default plot output. In other words, everything is great except the fact that axes and ticks get "transparent" due to antialiasing. $\endgroup$
    – DimG
    Commented Nov 19, 2012 at 8:52
  • 2
    $\begingroup$ @DimG Thanks for the accept - as to the axis thickness, the problem seems to be that the thickness of axes and frames is by default given as AbsoluteThickness[x] and the latter isn't translated correctly in the Export. But this can be fixed by explicitly adding an option AxesStyle->Thickness[n] or FrameStyle->Thickness[n] to your plot. Here, Thickness is a relative quantity that you can calculate from n = x/w where w is the width of the plot. For some reason, these relative thickness specifications are translated correctly. $\endgroup$
    – Jens
    Commented Nov 25, 2012 at 17:52
  • $\begingroup$ @Jens: I used your suggestion, too, to solve a similar problem. I used FrameStyle->Thickness[0.003], FrameTicksStyle->Thickness[0.003] in my framed plot to get both the frame and the ticks in the right width. $\endgroup$
    – Apatura
    Commented Feb 9, 2013 at 23:18
  • $\begingroup$ This is great in many cases, but if you specify a font for the plot (e.g. to use the LaTeX font), then the initial output as a PDF removes that formatting and the rasterized version will have the default Mathematica font. $\endgroup$ Commented Apr 26, 2017 at 17:24
  • $\begingroup$ Hi, @Jens. I tried your solution on my plots in my post mathematica.stackexchange.com/q/172192/4742 . It seems that your solution doesn't support densityplot. Is there a workaround? $\endgroup$
    – matheorem
    Commented Apr 30, 2018 at 1:58
2
$\begingroup$

You can try to regulate ImageSize together with ImageResolution. There is one example: You get:

plot = Plot[x, {x, 0, 10}, ImageSize -> 250];
g = Rasterize[plot, ImageResolution -> #] & /@ {25, 50, 100, 200};
ImageDimensions /@ g
TableForm[Partition[g, 2]]

enter image description here

And yes, the quality is not as good as the vectorial form. Where we get: enter image description here

I think that it's due different way Mathematica render vector and images.

$\endgroup$
4
  • $\begingroup$ First of all, ImageSize and ImageResolution used together at the same level have no sense (seems that ImageSize has more priority), try to change ImageResolution and you will get no difference. You would rather have said something of: Rasterize[Show[plot, ImageSize-> 500], ImageResolution -> 150] However the main problem still remains as I've described: look at the axes ticks and you will notice dramatical difference between what Mathematica draws on the screen by default and what you get after Rasterize[]. $\endgroup$
    – DimG
    Commented Nov 19, 2012 at 0:27
  • $\begingroup$ I get your point. But just for curiosity, why you want that? Why use rastered form in your notebook? $\endgroup$
    – Murta
    Commented Nov 19, 2012 at 1:15
  • $\begingroup$ Firstly I got this problem while Export[] (I have to make high-resolution .png for including in LaTeX). But then I found similar behaviour of Rasterize[] and since it's simpler to reproduce in Mathematica (nothing to do with creating files and littering the filesystem, everything is seen on the screen), I've chosen Rasterize[] for a question:) $\endgroup$
    – DimG
    Commented Nov 19, 2012 at 7:39
  • $\begingroup$ Understand. Yes, you have to choose ImageResolution as Export argument too. $\endgroup$
    – Murta
    Commented Nov 19, 2012 at 18:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.