4
$\begingroup$

Note: This question is about AceFEM package for finite element analysis in Mathematica.

I would like to improve styling of color legend produced by default use of SMTShowMesh. The main problem is that size of colorful rectangles cannot be changed and this looks bad when the main picture of the mesh is either very large or very small. Furthermore, text on the legend starts to overlap if FontSize is increased enough.

(* This is a minimal example to setup mesh of 1 hexaxedral element. *)
<< AceFEM`;
SMTInputData[];
SMTAddDomain["A", "OL:SED3H1DFHYH1NeoHooke", {}];
SMTAddElement["A", {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 0, 1},{1, 0,1}, {1, 1, 1}, {0, 1, 1}}];
SMTAnalysis["NodeReordering" -> False];

(* Field values in nodes are randomly chosen. *)
field = RandomReal[{-1, 1}, SMTNoNodes]

Display mesh with chosen FontSize. Text on legend is overlapped. How to fix this?

SMTShowMesh["Field" -> field, "Mesh" -> Gray, "Contour" -> True, 
 ImageSize -> 300, Axes -> True, LabelStyle -> 20, 
 "TextStyle" -> {FontSize -> 20, FontFamily -> "Arial"}]

bad legend

$\endgroup$
6
$\begingroup$

Simple solution is to suppress the default legend generated by AceFEM (SMTShowMesh[...,"Legend"->False,...]) and create your own legend manually with BarLegend command or similar. You just need to take care of appropriate value range and number of contour lines if they are displayed.

(* The range of values for plotted field. *)
MinMax[field]

(* Constants in With are used to avoid repeating the same values twice. *)
With[{
  lim = 0.8*Max@Abs[field],
  font = 16,
  contours = 10
  },
 Legended[
  SMTShowMesh[
   "ColorFunction" -> (ColorData["TemperatureMap", #] &),
   "Contour" -> {-lim, lim, contours - 2},
   "Field" -> field,
   "Legend" -> False,(* Suppress the default legend.  *)
   "Mesh" -> Gray,
   Axes -> True,
   ImageSize -> 250,
   LabelStyle -> font,
   Lighting -> {{"Ambient", White}}(* 
   This Lightning setting produces lively colors. *)
   ],
  BarLegend[
   {"TemperatureMap", {-lim, lim}}, contours - 1,
   LegendMarkerSize -> 200,
   LegendLabel -> "label\ntext",
   LabelStyle -> {FontSize -> font},
   LegendFunction -> "Frame"
   ]
  ]
 ]

new legend

The above described approach also enables different positioning of BarLegend and tons of other styling functions described in MMA documentation.

Legended[
 SMTShowMesh[
  "ColorFunction" -> (ColorData["TemperatureMap", #] &),
  "Contour" -> True,
  "Field" -> field,
  "Legend" -> False,
  "Mesh" -> Gray,
  ImageSize -> 250,
  Lighting -> {{"Ambient", White}}
  ],
 Placed[
  BarLegend[
   {"TemperatureMap", MinMax[field]}, 7,
   LegendMarkerSize -> 250,
   LegendLabel -> "label text",
   LabelStyle -> {FontSize -> 10},
   LegendLayout -> "Row"
   ],
  Above
  ]
 ]

horizontal legend

$\endgroup$
5
$\begingroup$

The SMSShowMesh legend size is constructed with Colored Rectangle primitives which have a fixed size, therefore its size cannot be changed, unless you would manually specify Rectangle geometry after plot has been generated. NZupan suggested some nice alternatives for legend, but if one prefers to use the default legend, the best option to increase its size is to specify lower ImageSize, so the mesh size is reduced while the legend and text remains the same size:

plot = SMTShowMesh["Field" -> field, "Mesh" -> Gray, "Contour" -> True, 
"ColorFunction" -> (ColorData["TemperatureMap", #] &), ImageSize -> 240,
Axes -> True, "TextStyle" -> {FontFamily -> "Arial"}]

enter image description here

You can then plot the magnified image inside Mathematica it will be shown 2 times as big:

Magnify[plot , 2]

enter image description here

If the plot is needed for export, then you can just specify the higher resolution when exporting:

Export["plot.png", plot,ImageResolution -> 360]

For vector graphics output should be fine:

Export["plot.pdf", plot]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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