1
$\begingroup$

I am trying to save some graphics into a file, and it seems impossible.

x1 = Round[RandomVariate[NormalDistribution[325, 5], 25], 0.1];
x2 = Round[RandomVariate[NormalDistribution[325.5, 2], 25], 0.1];
x3 = Round[RandomVariate[NormalDistribution[327.5, 7], 25], 0.1];

h1 = Histogram[x1, 6, "Probability", 
   ChartLegends -> {"\!\(\*SubscriptBox[\(x\), \(1\)]\)"}, 
   ChartStyle -> {Directive[Red]}, 
   PlotRange -> {{308, 342}, {0, 0.5}}];
h2 = Histogram[x2, 6, "Probability", 
   ChartLegends -> {"\!\(\*SubscriptBox[\(x\), \(2\)]\)"}, 
   ChartStyle -> {Directive[Blue]}, 
   PlotRange -> {{308, 342}, {0, 0.5}}];
h3 = Histogram[x3, 6, "Probability", 
   ChartLegends -> {"\!\(\*SubscriptBox[\(x\), \(3\)]\)"}, 
   ChartStyle -> {Directive[Green]}, 
   PlotRange -> {{308, 342}, {0, 0.5}}];
   Export["C:\CompareHist.png", GraphicsColumn[{h1, h2, h3}]]

Of course I cannot use save graphics into file option as there is no such option. And by the way: why Export functions always fails? Even when trying to follow tutorial. Is it some known bug?

$\endgroup$
6
  • 5
    $\begingroup$ Have you tried with "\\" rather than "\" in your file name ? $\endgroup$ Apr 2, 2013 at 11:16
  • $\begingroup$ When no C:\ it works. But how to write paths then? $\endgroup$
    – Misery
    Apr 2, 2013 at 11:18
  • 6
    $\begingroup$ Have a look at FileNameJoin. $\endgroup$ Apr 2, 2013 at 11:33
  • $\begingroup$ I am, but you first gave me the right solution, and I wanted to vote your answer. $\endgroup$
    – Misery
    Apr 2, 2013 at 13:18
  • 1
    $\begingroup$ The reason why the single backslash fails as a path separator is that the backslah acts as escape character in strings as in many programming languages. "\t", for instance, codes for the tab character. This means that if you want to have the literal backslash you need to escape that with the escape character, which is the backslash itself. So "\\" is the `\` character. $\endgroup$ Apr 2, 2013 at 19:12

1 Answer 1

5
$\begingroup$

From my experience, outputting into the desktop is quite handy. Thus, in every notebook I have one initialization cell that selects the output folder

SetDirectory["C:\\Users\\R\\Desktop"]];

Then one that allows me to simply export every plot that I output:

Export["out.png", Out[-1]];

The problem in your exporting function is that you are not setting the directory for the figure correctly. Have a look into the exporting directory information here and, if you want to avoid doing what I suggested above, use FileNameJoin (example).

EDIT: I didn't see this, but @b.gatessucks had suggested FileNameJoin before I did.

$\endgroup$

Your Answer

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

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