20
$\begingroup$

How can I change the base font style for all graphics objects, including the ones generated using plotting functions?

I would like to change this a single time and have it affect all graphics generated from there on (i.e. not specify the font on a plot by plot basis).

Say, I'd like to use 16 point Helvetica for everything by default.


My original thought was SetOptions[Graphics, BaseStyle -> {...}], but this will be overridden on graphics created using the various plotting functions (e.g. Plot).

My second try was changing the BaseStyle in the GraphicsBoxOptions of the notebook, but this didn't have an effect.

$\endgroup$
1

3 Answers 3

18
$\begingroup$

To begin with your observation

SetOptions[Graphics, BaseStyle->{FontFamily -> "Helvetica"}]

doesn't work with Plot -- which seems unintuitive.

enter image description here

The styling for graphics is controlled by the Graphics style in the core.nb stylesheet.

enter image description here

We see here that font type and size etc. is explicitly specified so this seems to be taking precedence. For example having a local Graphics style in a private stylesheet with

Cell[StyleData["Graphics"],
 GraphicsBoxOptions->{BaseStyle->Directive[FontFamily->"Helvetica",RGBColor[1,0,0],FontSize->16]}]

has no effect. This is analogous to evaluating SetOptions as above.

So if you want to change the base font for all graphics what you need to do is override those specified font styles in the core.nb stylesheet. For example add this to your private stylesheet or to the stylesheet you regularly use.

Cell[StyleData["Graphics"],
 FontFamily->"Helvetica",
 FontSize->16,
 FontWeight->"Plain",
 FontColor->RGBColor[1,0,0]]

enter image description here

and you will see it has immediate effect (the pic below is the same cells as the screen grab above. No addition evaluation has taken place. After adding the new Graphics style the fonts automatically change):

enter image description here

$\endgroup$
5
  • $\begingroup$ Are GraphicsBoxOptions part of any stylesheet or is it FrontEnd option, I can't find it. $\endgroup$
    – Kuba
    Feb 24, 2016 at 13:13
  • $\begingroup$ @Kuba they can be included as options to any style ...obviously the intended style would be expected to have graphics rendered in the cell $\endgroup$ Feb 24, 2016 at 22:19
  • $\begingroup$ Yes yes, I mean, where are generic GraphicsBoxOptions for e.g. Output inherited from? $\endgroup$
    – Kuba
    Feb 25, 2016 at 5:44
  • $\begingroup$ @kuba I do not know. there does not appear to be a parent anywhere $\endgroup$ Feb 25, 2016 at 6:36
  • $\begingroup$ I always find working with stylesheets confusing. Any chance you could add some code that creates the appropriate private stylesheet and sets it as the stylesheet for that notebook? $\endgroup$
    – Kvothe
    Apr 14, 2023 at 16:52
5
$\begingroup$

While Mike's answer is of course very nice and useful I'd like to add a very simple Kernel-only idea: Just put all the relevant SetOptions statements into your init.m startup file. This is easily done, once, e.g., like:

Export[
 FindFile[
  "init.m"], (StringJoin[
   "Map[SetOptions[#, BaseStyle \[Rule] {FontFamily->\"Helvetica\", \
FontColor->Red, FontSize->16}]&,\n{",
   ExportString[
    Riffle[#, ","] &@
     ToExpression@
      Quiet@Select[
        Select[Names[
          "System`*"], ! StringMatchQ[#, "*Box*" | "$*"] &], ! 
          FreeQ[Options[Symbol[#]][[All, 1]], BaseStyle] &], "List"], 
   "}];\n"]
  ), "Text", CharacterEncoding -> "ASCII"]

This produces (check by FilePrint@FindFile["init.m"] ) :

Map[SetOptions[#, BaseStyle -> {FontFamily->"Helvetica", FontColor->Red, FontSize->16}]&,
{ActionMenu
,
AdjacencyGraph
,
AngularGauge
,
Animate
,
Animator
,
ArrayPlot
,
BarChart
,
BarChart3D
,
BodePlot
,
BooleanGraph
,
BubbleChart
,
BubbleChart3D
,
BulletGauge
,
ButterflyGraph
,
Button
,
ButtonBar
,
CancelButton
,
CandlestickChart
,
CayleyGraph
,
Checkbox
,
CheckboxBar
,
CirculantGraph
,
ClockGauge
,
ColorSetter
,
ColorSlider
,
Column
,
CommunityGraphPlot
,
CompleteGraph
,
CompleteKaryTree
,
ContourPlot
,
ContourPlot3D
,
ControllerManipulate
,
CycleGraph
,
DateListLogPlot
,
DateListPlot
,
DeBruijnGraph
,
DefaultButton
,
DensityHistogram
,
DensityPlot
,
DirectedGraph
,
DiscretePlot
,
DiscretePlot3D
,
DistributionChart
,
Dynamic
,
DynamicModule
,
DynamicNamespace
,
DynamicWrapper
,
FileNameSetter
,
FlipView
,
Framed
,
Graph
,
GraphComplement
,
GraphDifference
,
GraphDisjointUnion
,
Graphics
,
Graphics3D
,
GraphicsArray
,
GraphicsColumn
,
GraphicsGrid
,
GraphicsRow
,
GraphIntersection
,
GraphPlot
,
GraphPlot3D
,
GraphPower
,
GraphUnion
,
Grid
,
GridGraph
,
HararyGraph
,
HighlightGraph
,
Histogram
,
Histogram3D
,
HorizontalGauge
,
HypercubeGraph
,
Hyperlink
,
ImageHistogram
,
IncidenceGraph
,
InputField
,
Inset
,
InteractiveTradingChart
,
Interpretation
,
Item
,
KagiChart
,
KaryTree
,
KirchhoffGraph
,
KnightTourGraph
,
Labeled
,
LabeledSlider
,
LayeredGraphPlot
,
LineBreakChart
,
LineGraph
,
LineIntegralConvolutionPlot
,
ListAnimate
,
ListContourPlot
,
ListContourPlot3D
,
ListCurvePathPlot
,
ListDensityPlot
,
ListLineIntegralConvolutionPlot
,
ListLinePlot
,
ListLogLinearPlot
,
ListLogLogPlot
,
ListLogPlot
,
ListPicker
,
ListPlot
,
ListPlot3D
,
ListPointPlot3D
,
ListPolarPlot
,
ListStreamDensityPlot
,
ListStreamPlot
,
ListSurfacePlot3D
,
ListVectorDensityPlot
,
ListVectorPlot
,
ListVectorPlot3D
,
Locator
,
LocatorPane
,
LogLinearPlot
,
LogLogPlot
,
LogPlot
,
Manipulate
,
MatrixPlot
,
MenuView
,
MorphologicalGraph
,
Mouseover
,
NeighborhoodGraph
,
NicholsPlot
,
NyquistPlot
,
Opener
,
OpenerView
,
Overlay
,
PairedBarChart
,
PairedHistogram
,
PairedSmoothHistogram
,
Pane
,
Panel
,
PaneSelector
,
ParametricPlot
,
ParametricPlot3D
,
PasteButton
,
PathGraph
,
Periodogram
,
PetersenGraph
,
PieChart
,
PieChart3D
,
Plot
,
Plot3D
,
PointFigureChart
,
PolarPlot
,
Polygon
,
PopupMenu
,
PopupView
,
ProbabilityPlot
,
ProbabilityScalePlot
,
ProgressIndicator
,
QuantilePlot
,
RadioButton
,
RadioButtonBar
,
RandomGraph
,
RectangleChart
,
RectangleChart3D
,
RegionPlot
,
RegionPlot3D
,
ReliefPlot
,
RenkoChart
,
ReverseGraph
,
RevolutionPlot3D
,
RootLocusPlot
,
Rotate
,
Row
,
SectorChart
,
SectorChart3D
,
Setter
,
SetterBar
,
SimpleGraph
,
SingularValuePlot
,
Slider
,
Slider2D
,
SlideView
,
SmoothDensityHistogram
,
SmoothHistogram
,
SmoothHistogram3D
,
Spectrogram
,
SphericalPlot3D
,
StarGraph
,
StreamDensityPlot
,
StreamPlot
,
Subgraph
,
TabView
,
Text
,
ThermometerGauge
,
Toggler
,
TogglerBar
,
Tooltip
,
TradingChart
,
TreeForm
,
TreeGraph
,
TreePlot
,
Trigger
,
TuranGraph
,
UndirectedGraph
,
VectorDensityPlot
,
VectorPlot
,
VectorPlot3D
,
VerticalGauge
,
VerticalSlider
,
WaveletImagePlot
,
WaveletListPlot
,
WaveletMatrixPlot
,
WaveletScalogram
,
WeightedAdjacencyGraph
,
WheelGraph}];
$\endgroup$
3
  • 1
    $\begingroup$ It seems to me that the FontColor part is not working. Nothing is turning red running the code above. The rest does seem to work (different font size for example.) I am testing with a Plot and ListPlot. Any fix? (12.0.0 for Linux) $\endgroup$
    – Kvothe
    Aug 14, 2020 at 18:16
  • $\begingroup$ Is this supposed to affect the style for AxesLabel's? It seems it does not. How would I change the default color for those? $\endgroup$
    – Kvothe
    Apr 14, 2023 at 16:54
  • $\begingroup$ Change BaseStyle to LabelStyle. $\endgroup$ Apr 15, 2023 at 14:32
0
$\begingroup$

Just an oversized comment on the answer by Rolf Mertig.

The solution proposed there works but produces some errors. I think this is related to changing the default BaseStyle value for functions that don't take one.

With the following code I change both the default BaseStyle and the LabelStyle without errors. I simply take out the functions that produce an error compared to the answer by Rolf.

functionsThatDoNotAllowDefaultLabelStyle = {"ArrayPlot3D", 
   "AstroGraphics", "AxisObject", "BioSequencePlot", "BuckyballGraph",
    "Canvas", "ComplexArrayPlot", "ConcaveHullMesh", 
   "ConvexHullRegion", "CSGRegion", "CSGRegionTree", 
   "CumulativeFeatureImpactPlot", "DominatorTreeGraph", 
   "DualPlanarGraph", "ExpressionTree", "FeatureImpactPlot", 
   "FeatureValueDependencyPlot", "FeatureValueImpactPlot", 
   "FileSystemTree", "FindIsomorphicSubgraph", "GeoGraphPlot", 
   "GeoGraphValuePlot", "GradientFittedMesh", "GraphJoin", 
   "GraphProduct", "GraphSum", "GraphTree", "ImageVectorscopePlot", 
   "ImageWaveformPlot", "LayeredGraphPlot3D", "ListLinePlot3D", 
   "ListStreamPlot3D", "ListVectorDisplacementPlot", 
   "ListVectorDisplacementPlot3D", "NestTree", "ParallelAxisPlot", 
   "PointValuePlot", "RadialAxisPlot", "RandomTree", 
   "ReconstructionMesh", "RegionDilation", "RegionErosion", 
   "RootTree", "RulesTree", "StreamPlot3D", "TernaryListPlot", 
   "TorusGraph", "Tree", "UnlabeledTree", "VectorDisplacementPlot", 
   "VectorDisplacementPlot3D", "VertexInComponentGraph", 
   "VertexOutComponentGraph", "ArrayPlot3D", "AstroGraphics", 
   "AxisObject", "BioSequencePlot", "BuckyballGraph", "Canvas", 
   "ComplexArrayPlot", "ConcaveHullMesh", "ConvexHullRegion", 
   "CSGRegion", "CSGRegionTree", "CumulativeFeatureImpactPlot", 
   "DominatorTreeGraph", "DualPlanarGraph", "ExpressionTree", 
   "FeatureImpactPlot", "FeatureValueDependencyPlot", 
   "FeatureValueImpactPlot", "FileSystemTree", 
   "FindIsomorphicSubgraph", "GeoGraphPlot", "GeoGraphValuePlot", 
   "GradientFittedMesh", "GraphJoin", "GraphProduct", "GraphSum", 
   "GraphTree", "ImageVectorscopePlot", "ImageWaveformPlot", 
   "LayeredGraphPlot3D", "ListLinePlot3D", "ListStreamPlot3D", 
   "ListVectorDisplacementPlot", "ListVectorDisplacementPlot3D", 
   "NestTree", "ParallelAxisPlot", "PointValuePlot", "RadialAxisPlot",
    "RandomTree", "ReconstructionMesh", "RegionDilation", 
   "RegionErosion", "RootTree", "RulesTree", "StreamPlot3D", 
   "TernaryListPlot", "TorusGraph", "Tree", "UnlabeledTree", 
   "VectorDisplacementPlot", "VectorDisplacementPlot3D", 
   "VertexInComponentGraph", "VertexOutComponentGraph"};
functionsThatDoNotAllowDefaultBaseStyle = {"ArrayPlot3D", 
   "AstroGraphics", "AxisObject", "BioSequencePlot", "BuckyballGraph",
    "Canvas", "ComplexArrayPlot", "ConcaveHullMesh", 
   "ConvexHullRegion", "CSGRegion", "CSGRegionTree", 
   "CumulativeFeatureImpactPlot", "DominatorTreeGraph", 
   "DualPlanarGraph", "ExpressionTree", "FeatureImpactPlot", 
   "FeatureValueDependencyPlot", "FeatureValueImpactPlot", 
   "FileSystemTree", "FindIsomorphicSubgraph", "GeodesicPolyhedron", 
   "GeoGraphPlot", "GeoGraphValuePlot", "GradientFittedMesh", 
   "GraphJoin", "GraphProduct", "GraphSum", "GraphTree", 
   "ImageVectorscopePlot", "ImageWaveformPlot", "LayeredGraphPlot3D", 
   "ListLinePlot3D", "ListStreamPlot3D", "ListVectorDisplacementPlot",
    "ListVectorDisplacementPlot3D", "NestTree", "ParallelAxisPlot", 
   "PointValuePlot", "RadialAxisPlot", "RandomTree", 
   "ReconstructionMesh", "RegionDilation", "RegionErosion", 
   "RootTree", "RulesTree", "StreamPlot3D", "TernaryListPlot", 
   "TorusGraph", "Tree", "UnlabeledTree", "VectorDisplacementPlot", 
   "VectorDisplacementPlot3D", "VertexInComponentGraph", 
   "VertexOutComponentGraph"};

functionsPossiblyTakingDefaultLabelStyle = Select[
   Select[Names["System`*"], ! StringMatchQ[#, "*Box*" | "$*"] &],
   ! FreeQ[Options[Symbol[#]][[All, 1]], LabelStyle] &];

functionsTakingDefaultBaseStyle = Complement[
   Select[
    Select[Names["System`*"], ! StringMatchQ[#, "*Box*" | "$*"] &], ! 
      FreeQ[Options[Symbol[#]][[All, 1]], BaseStyle] &]
   , functionsThatDoNotAllowDefaultBaseStyle];

fnt = "16";
functionsTakingDefaultLabelStyle = 
  Complement[functionsPossiblyTakingDefaultLabelStyle, 
   functionsThatDoNotAllowDefaultLabelStyle];
initializationCommandDefaultStyles = 
  StringJoin[(StringJoin[
     "Map[SetOptions[#, LabelStyle \[Rule] \
{FontColor->Black,FontSize->" <> ToString[fnt] <> "}]&,\n{", 
     ExportString[
      Riffle[#, ","] &@
       ToExpression@Quiet@functionsTakingDefaultLabelStyle, "List"], 
     "}];\n"])
   , "\n",
   StringJoin[
    "Map[SetOptions[#, BaseStyle \[Rule] \
{FontColor->Black,FontSize->" <> ToString[fnt] <> "}]&,\n{", 
    ExportString[
     Riffle[#, ","] &@ToExpression@functionsTakingDefaultBaseStyle, 
     "List"], "}];\n"]
   ];
Export[FindFile["init.m"], initializationCommandDefaultStyles, "Text",
  CharacterEncoding -> "ASCII"]

Get[$UserBaseDirectory <> "/Kernel/init.m"]
$\endgroup$

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.