7
$\begingroup$

I am using M12.0.0.0 on Ubuntu and I have these two plots:

plot1 = DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 4}, PlotPoints -> 80];
plot2 = Plot[Sin[x], {x, -10, 10}, Frame -> True];

when I export these plots as png the FramTicks disapear! here is plot1 as seen in the MMA notebook

enter image description here
and this how it looks when exported using Export["/.../plotq1.png", plot1,ImageResolution -> 500]
enter image description here

same thing also applies for plot2 in the notebook is like

enter image description here

and exported is like

enter image description here

$\endgroup$
4
  • 2
    $\begingroup$ Is this a bug or the nature of PNG exporting? The current set of answers give nice workarounds but it would be nice to see an answer to the question "why?" $\endgroup$ Commented Jan 5, 2021 at 12:58
  • 2
    $\begingroup$ @bobthechemist I think this has to do with the fact that the default ticks are defined at an absolute size, rather than as Scaled objects. When the size of the image is increased, either through ImageSize in a notebook, or by exporting at high resolution, the default ticks do not scale accordingly and end up too small to be seen. $\endgroup$
    – MarcoB
    Commented Jan 5, 2021 at 16:28
  • $\begingroup$ @MarcoB thanks for commenting - I've seen this issue a number of times in my own work and your analysis makes sense and is helpful. $\endgroup$ Commented Jan 5, 2021 at 18:21
  • 1
    $\begingroup$ I've run into similar issues exporting figures to .png format. My current solution is to export them to .pdf. If you really need .png, you could always use ImageMagick to convert pdf to png. $\endgroup$ Commented Sep 23, 2021 at 17:22

2 Answers 2

4
$\begingroup$

Same issue in

$Version
"11.3.0 for Microsoft Windows (64-bit) (March 7, 2018)"

plt1a = DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 4}, PlotPoints -> 80]

enter image description here

Export["plot1A.png", plt1a, ImageResolution -> 500]

enter image description here

If you specify a lower image resolution the issue does not arise:

Export["plot1A100.png", plt1a, ImageResolution -> 100]

enter image description here

To get the ticks visible at high image resolution, you can make the ticks longer and/or thicker:

plt1b = DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 4}, PlotPoints -> 80,
   FrameTicksStyle -> Thick, 
   FrameTicks -> Charting`ScaledTicks[{Identity, Identity}, 
    "TicksLength" -> {0.02, 0.01}]]

enter image description here

Export["plot1B.png", plt1b, ImageResolution -> 500]

enter image description here

To get tick labels on the bottom and left frames only, use

FrameTicks -> {{Charting`ScaledTicks[{Identity, Identity}, 
    "TicksLength" -> {0.02, 0.01}], Automatic},
  {Charting`ScaledTicks[{Identity, Identity}, 
   "TicksLength" -> {0.02, 0.01}], Automatic}}]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks a lot @kglr! is it possible to remove numbers from the top and right axes? $\endgroup$
    – MMA13
    Commented Jan 5, 2021 at 12:36
  • 1
    $\begingroup$ @HD2006, please see the update. $\endgroup$
    – kglr
    Commented Jan 5, 2021 at 12:43
  • $\begingroup$ @kglr I've seen you use ``ChartingScaledTicks ` often. In fact, I've learned quite a few tricks with it from your answers! There is information about this function scattered through MMA.SSE, but not collected in a single place. There is, however, an unanswered question from last year (How to use Charting`ScaledTicks?) about it that would make for a great repository of that information. I was wondering if you could be persuaded to summarize your knowledge of this function in an answer there? $\endgroup$
    – MarcoB
    Commented Jan 5, 2021 at 16:25
  • $\begingroup$ @MarcoB , what I know about Charting`ScaledTicks is limited to what I could decipher from ??Charting`ScaledTicks. I will see if I can extract something (perhaps a list of argument patterns) useful for the linked question. $\endgroup$
    – kglr
    Commented Jan 5, 2021 at 21:21
5
$\begingroup$

If you specify the FrameTicks as follows it works fine:

ft[low_, high_] :=
  Table[{i, i, {.01, 0}, Directive[Black, Thickness[.002]]}, {i, low,high}];

plot1 = DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 4}, PlotPoints -> 80
   , FrameTicks -> { {ft[0, 4], None}, {ft[-2, 2], None}}];

Export["/tmp/plotq1.png", plot1, ImageResolution -> 500];

SystemOpen@"/tmp/plotq1.png"
$\endgroup$
1
  • 1
    $\begingroup$ Nice solution, kinda frustrating that a program with a $1480/year license cost has so many quirky behaviors and poorly documented solutions. $\endgroup$ Commented Sep 23, 2021 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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