Message Boards Message Boards

0
|
343 Views
|
7 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Export Plots in an vector format

Posted 1 month ago

Hi, I have Mathematica 14.0. and when I am using export to vector format (like svg), then I am getting fully vectorized image (i.e. ticks, frame .. are letters, lines and so on). This is what i would like to have while, I am plotting 4 liines, but then I am having the plot fully pixelized (i.e. all the graph area+frame+ticks is one image which cannot be easily eddited). Please see the example bellow.:

plottable2 =
 ListLinePlot[{ArrayReshape[
    Flatten[Table[{{i - 0.05, tmp = RandomReal[{0., 0.5}]}, {i + 0.05,
         tmp}}, {i, -5.45, 1.95 + 0.01, 0.1}]], {75*2, 2}], 
   ArrayReshape[
    Flatten[Table[{{i - 0.05, tmp = RandomReal[{0., 0.5}]}, {i + 0.05,
         tmp}}, {i, -5.45, 1.95 + 0.01, 0.1}]], {75*2, 2}], 
   ArrayReshape[
    Flatten[Table[{{i - 0.05, tmp = RandomReal[{0., 0.5}]}, {i + 0.05,
         tmp}}, {i, -5.45, 1.95 + 0.01, 0.1}]], {75*2, 2}], 
   ArrayReshape[
    Flatten[Table[{{i - 0.05, tmp = RandomReal[{0., 0.5}]}, {i + 0.05,
         tmp}}, {i, -5.45, 1.95 + 0.01, 0.1}]], {75*2, 2}]},
  Filling -> Bottom, PlotStyle -> style, Frame -> True, 
  FrameStyle -> Thick, 
  FrameLabel -> {"adsorption energy (eV)", "Frequency"}, 
  LabelStyle -> Directive[Bold, Black, Thick, 18, "Times New Roman"], 
  PlotLabel -> Style["Random", Bold, 18, "Times New Roman"], 
  Axes -> False, PlotRange -> {0, 0.51}, 
  PlotLegends -> 
   Placed[{Style["*H", Black, 18, "Times New Roman"], 
     Style["*OH", Black, 18, "Times New Roman"], 
     Style["*\!\(\*SubscriptBox[\(OCH\), \(3\)]\)", Black, 18, 
      "Times New Roman"], 
     Style["*OCHO", Black, 18, "Times New Roman"]}, Right], 
  ImageSize -> 300]
Export[NotebookDirectory[] <> "tmp_plot.svg", plottable2]

Is there any reason for it? Is there an easy way, how to obtain vectorized output? Thank you very much for your answer!

POSTED BY: Ondrej Krejci
7 Replies

This seems the same problem and suggests it was fixed in 14.1:

https://mathematica.stackexchange.com/questions/302291/svg-export-is-rasterized-with-certain-colors

One of the comments suggests a fix:

There is some algorithm that checks this: RGBColor[{r, g, b, o}]; If[Min[{r, g, b}*o] < 6/1000, "raster", "vector"]. So use colors that the least product is at least 6/1000. Then if really needed you can replace the colors inside svg file.

I don't have Windows so I can't check. I'm not sure about the comment. It means pure Red would be rasterized. Maybe that's the case, but it seems odd. The comment has 3 upvotes. Either people are impressed by its specificity or it's correct, or both. Or maybe the condition is 0 < Min[{r, g, b}*o] < 6/1000, and it was mistyped.

POSTED BY: Michael Rogers

Dear Michael!

Thank you, that could explain the "randomnes" that I have experienced. i.e. exporting fully vector format with 2 lines, while getting a rasterized part with 3 and 4 lines. As it seems, the only thing, that I can do is to update the version (or use my Linux version.) Thank you very much!

POSTED BY: Ondrej Krejci

Dear Ondrej,

Sorry, I cannot explain anything about Windows. I've used it only when I had to for presentations. Usually, I've encountered rasterization in Export[] when there is difficulty representing color and alpha (gradients in particular) in the target file format. The use of VertexColors is a common cause of rasterization. But sometimes it has to do with other things, such the robustness of the converter or the way it is being used. If you haven't, try asking WRI directly (through the menu, Help > Give Feedback...).

POSTED BY: Michael Rogers

Dear Gareth, Thank you for your answer. The attached tmp_plot.svg file is an svg created with Mathematica 14.0 on Windows with that code above. (I do not care about the legends for now). If I try to open it with Inkscape, it really doesn't matter, how many times I click on the plot on the left - it is still one piece of block of an raster image, which cannot be ungrouped.

If I use the same code in Mathematica 12.3 on Linux, then I obtain tmp_plot_12_3.svg which is fully vectorized.

Even though I have a way how to do the stuff properly. This is really sub-optimal for creating the stuff while travelling. I would not mind if there would be a possibility to "enforce" the vectorization, rather than automated black-box that I cannot affect at all

Attachment

Attachment

POSTED BY: Ondrej Krejci

Interesting. I have v14.0 for Mac, and it works. It seems to be the Windows v14.0 version that is having trouble. SVG files are just XML, and can be opened in a text editor. Looking at your files, I can see that the Windows one has got a raster image embedded in it. But interestingly, it also seems to have e a lot of vector info as well!

Why don't you try a simple example, such as the one from the documentation? You can skip a step by using ExportString to see the actual XML produced.

ExportString[Graphics[{Red, Disk[]}], "SVG"]
POSTED BY: Gareth Russell

Update: If you delete the raster from the Windows version (in a text editor), it removes the graph, and what is left is a fully vectorized version of the legend.

POSTED BY: Gareth Russell

How do you know the SVG is not vector? When I run your code, and open the resultant SVG image in a browser, not seems to scale infinitely like a vector. It is true that if I Import it back into Mathematica 14, it does import as a raster, but that I think is a limitation of MMA.

POSTED BY: Gareth Russell
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract