Skip to content

Commit 284494b

Browse files
committed
core: make preset style control typography defaults across axes
1 parent f9cab5f commit 284494b

6 files changed

Lines changed: 90 additions & 73 deletions

File tree

examples/monte_carlo_alpha_example.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ int main(int argc, char** argv) {
3939
fs.cols = 1;
4040
fs.formats = {OutputFormat::Pdf, OutputFormat::Png, OutputFormat::Svg, OutputFormat::Eps};
4141
fs.size = FigureSizeInches{.w = 5.8, .h = 3.4};
42-
fs.style.font = "Helvetica";
43-
fs.style.font_pt = 12.5;
44-
fs.style.line_width_pt = 1.6;
45-
fs.style.grid = false;
4642
{
4743
std::ostringstream title;
4844
title << "Monte Carlo Ensemble (N=" << n_paths << ", alpha=" << alpha << ")";
@@ -53,6 +49,7 @@ int main(int argc, char** argv) {
5349
Figure fig(fs);
5450

5551
AxesSpec ax;
52+
ax.title = plot_title;
5653
ax.xlabel = "t [s]";
5754
ax.ylabel = "x(t)";
5855
ax.grid = false;
@@ -65,10 +62,7 @@ int main(int argc, char** argv) {
6562
ax.gnuplot_commands = {
6663
"set border 3 linewidth 0.8 linecolor rgb '#1c1c1c'",
6764
"set tics out nomirror",
68-
"set arrow 10 from graph 0, first 0 to graph 1, first 0 nohead lc rgb '#9a9a9a' dt 3 lw 0.8 back",
69-
"set xlabel 't [s]' font 'Helvetica,16'",
70-
"set ylabel 'x(t)' font 'Helvetica,16'",
71-
"set title '{/:Bold " + plot_title + "}' font 'Helvetica,18'"};
65+
"set arrow 10 from graph 0, first 0 to graph 1, first 0 nohead lc rgb '#9a9a9a' dt 3 lw 0.8 back"};
7266
fig.axes(0).set(ax);
7367

7468
std::vector<double> t;

examples/stats_plot_examples.cpp

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,6 @@
1515

1616
namespace {
1717

18-
std::string gp_escape(const std::string& text) {
19-
std::string out;
20-
out.reserve(text.size());
21-
for (char c : text) {
22-
if (c == '\'') {
23-
out += "''";
24-
} else {
25-
out += c;
26-
}
27-
}
28-
return out;
29-
}
30-
31-
void apply_large_typography(gnuplotpp::AxesSpec& ax,
32-
const double label_pt = 17.0,
33-
const double title_pt = 20.0) {
34-
std::vector<std::string> commands;
35-
if (!ax.xlabel.empty()) {
36-
commands.push_back("set xlabel '" + gp_escape(ax.xlabel) + "' font 'Helvetica," +
37-
std::to_string(label_pt) + "'");
38-
}
39-
if (!ax.ylabel.empty()) {
40-
commands.push_back("set ylabel '" + gp_escape(ax.ylabel) + "' font 'Helvetica," +
41-
std::to_string(label_pt) + "'");
42-
}
43-
if (!ax.y2label.empty()) {
44-
commands.push_back("set y2label '" + gp_escape(ax.y2label) + "' font 'Helvetica," +
45-
std::to_string(label_pt) + "'");
46-
}
47-
if (!ax.title.empty()) {
48-
commands.push_back("set title '{/:Bold " + gp_escape(ax.title) + "}' font 'Helvetica," +
49-
std::to_string(title_pt) + "'");
50-
}
51-
ax.gnuplot_commands.insert(ax.gnuplot_commands.end(), commands.begin(), commands.end());
52-
}
53-
5418
gnuplotpp::FigureSpec make_spec(const std::string& title) {
5519
using namespace gnuplotpp;
5620
FigureSpec fs;
@@ -61,12 +25,6 @@ gnuplotpp::FigureSpec make_spec(const std::string& title) {
6125
fs.title = title;
6226
fs.text_mode = TextMode::Enhanced;
6327
fs.formats = {OutputFormat::Pdf, OutputFormat::Png, OutputFormat::Svg};
64-
fs.palette = ColorPalette::Grayscale;
65-
fs.style.font = "Helvetica";
66-
fs.style.font_pt = 13.5;
67-
fs.style.line_width_pt = 1.8;
68-
fs.style.point_size = 0.85;
69-
fs.style.grid = false;
7028
fs.font_fallbacks = {"Arial", "Nimbus Sans", "DejaVu Sans", "Helvetica"};
7129
fs.panel_labels = false;
7230
fs.caption.clear();
@@ -127,7 +85,6 @@ int main(int argc, char** argv) {
12785
ax.xtick_step = 1.0;
12886
ax.has_ytick_step = true;
12987
ax.ytick_step = 1.0;
130-
apply_large_typography(ax);
13188
fig.axes(0).set(ax);
13289

13390
std::vector<double> q_theory, q_sample;
@@ -184,7 +141,6 @@ int main(int argc, char** argv) {
184141
ax.ymax = 3.2;
185142
ax.has_xtick_step = true;
186143
ax.xtick_step = 0.25;
187-
apply_large_typography(ax);
188144
fig.axes(0).set(ax);
189145

190146
std::vector<double> y_grid, half_w;
@@ -217,7 +173,6 @@ int main(int argc, char** argv) {
217173
std::to_string(summary.median) + " nohead lw 2.0 lc rgb '#1f1f1f' front",
218174
"set arrow 22 from -0.30," + std::to_string(summary.q3) + " to 0.30," +
219175
std::to_string(summary.q3) + " nohead lw 1.5 lc rgb '#1f1f1f' dt 2 front"};
220-
apply_large_typography(ax);
221176
fig.axes(0).set(ax);
222177

223178
fig.axes(0).add_series(SeriesSpec{.label = "left",
@@ -271,7 +226,6 @@ int main(int argc, char** argv) {
271226
ax.xmax = 1.3;
272227
ax.has_xtick_step = true;
273228
ax.xtick_step = 0.1;
274-
apply_large_typography(ax);
275229
fig.axes(0).set(ax);
276230

277231
const auto box = box_summary(samples);
@@ -281,7 +235,6 @@ int main(int argc, char** argv) {
281235
ax.ymin = *smin_it - ypad;
282236
ax.ymax = *smax_it + ypad;
283237
ax.gnuplot_commands = {"set xtics ('Sample A' 1.0) font 'Helvetica,13'"};
284-
apply_large_typography(ax);
285238
fig.axes(0).set(ax);
286239

287240
std::vector<double> x_pts;
@@ -317,7 +270,6 @@ int main(int argc, char** argv) {
317270
std::to_string(box.whisker_low) + " nohead lw 1.5 lc rgb '#1f1f1f'",
318271
"set arrow 5 from 0.95," + std::to_string(box.whisker_high) + " to 1.05," +
319272
std::to_string(box.whisker_high) + " nohead lw 1.5 lc rgb '#1f1f1f'"};
320-
apply_large_typography(ax);
321273
fig.axes(0).set(ax);
322274

323275
if (render(fig, out_root / "box_summary" / "figures") != 0) return 1;
@@ -328,7 +280,7 @@ int main(int argc, char** argv) {
328280
auto fs = make_spec("Confidence Ellipse");
329281
Figure fig(fs);
330282
AxesSpec ax;
331-
ax.title = "2-Sigma Confidence Ellipse";
283+
ax.title = "1/2/3-Sigma Confidence Ellipses";
332284
ax.xlabel = "x";
333285
ax.ylabel = "y";
334286
ax.grid = true;
@@ -338,7 +290,6 @@ int main(int argc, char** argv) {
338290
ax.legend_spec.opaque = true;
339291
ax.legend_spec.has_font_pt = true;
340292
ax.legend_spec.font_pt = 13.0;
341-
apply_large_typography(ax);
342293
fig.axes(0).set(ax);
343294

344295
std::normal_distribution<double> nx(0.0, 1.0);
@@ -359,16 +310,36 @@ int main(int argc, char** argv) {
359310
x,
360311
y);
361312

362-
std::vector<double> ex, ey;
363-
confidence_ellipse(x, y, 2.0, ex, ey, 240);
313+
std::vector<double> ex1, ey1;
314+
std::vector<double> ex2, ey2;
315+
std::vector<double> ex3, ey3;
316+
confidence_ellipse(x, y, 1.0, ex1, ey1, 240);
317+
confidence_ellipse(x, y, 2.0, ex2, ey2, 240);
318+
confidence_ellipse(x, y, 3.0, ex3, ey3, 240);
319+
fig.axes(0).add_series(
320+
SeriesSpec{.label = "1{/Symbol s} ellipse",
321+
.has_line_width = true,
322+
.line_width_pt = 2.0,
323+
.has_color = true,
324+
.color = "#1f77b4"},
325+
ex1,
326+
ey1);
364327
fig.axes(0).add_series(
365328
SeriesSpec{.label = "2{/Symbol s} ellipse",
366329
.has_line_width = true,
367330
.line_width_pt = 2.8,
368331
.has_color = true,
369332
.color = "#e45756"},
370-
ex,
371-
ey);
333+
ex2,
334+
ey2);
335+
fig.axes(0).add_series(
336+
SeriesSpec{.label = "3{/Symbol s} ellipse",
337+
.has_line_width = true,
338+
.line_width_pt = 2.0,
339+
.has_color = true,
340+
.color = "#2ca02c"},
341+
ex3,
342+
ey3);
372343

373344
if (render(fig, out_root / "confidence_ellipse" / "figures") != 0) return 1;
374345
}
@@ -398,7 +369,6 @@ int main(int argc, char** argv) {
398369
ax.xtick_step = 10.0;
399370
ax.has_ytick_step = true;
400371
ax.ytick_step = 0.2;
401-
apply_large_typography(ax);
402372
fig.axes(0).set(ax);
403373

404374
std::vector<double> sig(800);

include/gnuplotpp/plot.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ struct Style {
5454
double line_width_pt = 1.0;
5555
double point_size = 0.6;
5656
bool grid = false;
57+
double tick_font_scale = 1.0;
58+
double label_font_scale = 1.0;
59+
double title_font_scale = 1.0;
60+
bool title_bold = false;
5761
};
5862

5963
/** @brief High-level figure configuration. */
@@ -174,6 +178,14 @@ struct AxesSpec {
174178
int yminor_count = 2;
175179
std::string xformat;
176180
std::string yformat;
181+
bool has_tick_font_pt = false;
182+
double tick_font_pt = 8.0;
183+
bool has_label_font_pt = false;
184+
double label_font_pt = 9.0;
185+
bool has_title_font_pt = false;
186+
double title_font_pt = 9.0;
187+
bool has_title_bold = false;
188+
bool title_bold = false;
177189

178190
std::vector<LabelAnnotation> labels;
179191
std::vector<ArrowAnnotation> arrows;

src/gnuplot_backend.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,12 @@ void emit_plot_body(std::ostream& os,
439439
const bool ieee = is_ieee_preset(spec.preset);
440440
const bool custom_color_or_opacity = has_custom_color_or_opacity(fig);
441441
const bool heatmap = has_heatmap(fig);
442-
const double tick_font_pt = ieee ? 8.0 : spec.style.font_pt;
443-
const double label_font_pt = ieee ? 8.5 : spec.style.font_pt;
444-
const double title_font_pt = ieee ? 8.5 : spec.style.font_pt;
442+
const double tick_font_pt =
443+
ieee ? 8.0 : spec.style.font_pt * std::max(0.1, spec.style.tick_font_scale);
444+
const double label_font_pt =
445+
ieee ? 8.5 : spec.style.font_pt * std::max(0.1, spec.style.label_font_scale);
446+
const double title_font_pt =
447+
ieee ? 8.5 : spec.style.font_pt * std::max(0.1, spec.style.title_font_scale);
445448

446449
os << "set border linewidth 0.9 linecolor rgb '#222222'\n";
447450
os << "set tics in nomirror scale 0.5,0.25\n";
@@ -478,6 +481,12 @@ void emit_plot_body(std::ostream& os,
478481
for (std::size_t axis_idx = 0; axis_idx < all_axes.size(); ++axis_idx) {
479482
const auto& axis = all_axes[axis_idx];
480483
const auto& axis_spec = axis.spec();
484+
const double axis_tick_font_pt =
485+
axis_spec.has_tick_font_pt ? axis_spec.tick_font_pt : tick_font_pt;
486+
const double axis_label_font_pt =
487+
axis_spec.has_label_font_pt ? axis_spec.label_font_pt : label_font_pt;
488+
const double axis_title_font_pt =
489+
axis_spec.has_title_font_pt ? axis_spec.title_font_pt : title_font_pt;
481490
emit_auto_layout(os, spec, axis_spec);
482491

483492
os << "unset title\n";
@@ -494,6 +503,8 @@ void emit_plot_body(std::ostream& os,
494503
os << "unset y2tics\n";
495504
os << "set format x '%.2g'\n";
496505
os << "set format y '%.2g'\n";
506+
os << "set xtics font '" << esc(spec.style.font) << "," << axis_tick_font_pt << "'\n";
507+
os << "set ytics font '" << esc(spec.style.font) << "," << axis_tick_font_pt << "'\n";
497508

498509
const bool legend_enabled = axis_spec.legend && axis_spec.legend_spec.enabled;
499510
if (legend_enabled) {
@@ -517,28 +528,31 @@ void emit_plot_body(std::ostream& os,
517528
}
518529

519530
if (!axis_spec.title.empty()) {
520-
os << "set title '" << esc(axis_spec.title) << "' font '" << esc(spec.style.font) << ","
521-
<< title_font_pt << "'\n";
531+
const bool title_bold = axis_spec.has_title_bold ? axis_spec.title_bold : spec.style.title_bold;
532+
const std::string title_text = title_bold ? "{/:Bold " + esc(axis_spec.title) + "}"
533+
: esc(axis_spec.title);
534+
os << "set title '" << title_text << "' font '" << esc(spec.style.font) << ","
535+
<< axis_title_font_pt << "'\n";
522536
}
523537
if (!axis_spec.xlabel.empty()) {
524538
os << "set xlabel '" << esc(axis_spec.xlabel) << "' font '" << esc(spec.style.font) << ","
525-
<< label_font_pt << "'\n";
539+
<< axis_label_font_pt << "'\n";
526540
}
527541
if (!axis_spec.ylabel.empty()) {
528542
os << "set ylabel '" << esc(axis_spec.ylabel) << "' font '" << esc(spec.style.font) << ","
529-
<< label_font_pt << "'\n";
543+
<< axis_label_font_pt << "'\n";
530544
}
531545
if (!axis_spec.y2label.empty()) {
532546
os << "set y2label '" << esc(axis_spec.y2label) << "' font '" << esc(spec.style.font) << ","
533-
<< label_font_pt << "'\n";
547+
<< axis_label_font_pt << "'\n";
534548
}
535549

536550
if (axis_spec.grid || spec.style.grid) {
537551
os << "set grid xtics ytics linewidth 0.35 linecolor rgb '#e2e2e2'\n";
538552
}
539553
if (!axis_spec.colorbar_label.empty()) {
540554
os << "set cblabel '" << esc(axis_spec.colorbar_label) << "' font '" << esc(spec.style.font)
541-
<< "," << label_font_pt << "'\n";
555+
<< "," << axis_label_font_pt << "'\n";
542556
}
543557
if (axis_spec.has_cbrange) {
544558
os << "set cbrange [" << axis_spec.cbmin << ":" << axis_spec.cbmax << "]\n";

src/presets.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,66 @@ void apply_style_profile(FigureSpec& spec, StyleProfile profile) {
9696
spec.style.font_pt = 9.0;
9797
spec.style.line_width_pt = 1.5;
9898
spec.style.grid = true;
99+
spec.style.tick_font_scale = 0.95;
100+
spec.style.label_font_scale = 1.05;
101+
spec.style.title_font_scale = 1.12;
102+
spec.style.title_bold = false;
99103
spec.palette = ColorPalette::Tab10;
100104
break;
101105
case StyleProfile::IEEE_Strict:
102106
spec.style.font = "Times";
103107
spec.style.font_pt = 8.5;
104108
spec.style.line_width_pt = 1.0;
105109
spec.style.grid = false;
110+
spec.style.tick_font_scale = 1.0;
111+
spec.style.label_font_scale = 1.0;
112+
spec.style.title_font_scale = 1.0;
113+
spec.style.title_bold = false;
106114
spec.palette = ColorPalette::Grayscale;
107115
break;
108116
case StyleProfile::AIAA_Strict:
109117
spec.style.font = "Times";
110118
spec.style.font_pt = 8.0;
111119
spec.style.line_width_pt = 1.0;
112120
spec.style.grid = false;
121+
spec.style.tick_font_scale = 1.0;
122+
spec.style.label_font_scale = 1.0;
123+
spec.style.title_font_scale = 1.0;
124+
spec.style.title_bold = false;
113125
spec.palette = ColorPalette::Default;
114126
break;
115127
case StyleProfile::Presentation:
116128
spec.style.font = "Helvetica";
117129
spec.style.font_pt = 12.0;
118130
spec.style.line_width_pt = 2.0;
119131
spec.style.grid = true;
132+
spec.style.tick_font_scale = 1.0;
133+
spec.style.label_font_scale = 1.15;
134+
spec.style.title_font_scale = 1.25;
135+
spec.style.title_bold = true;
120136
spec.palette = ColorPalette::Viridis;
121137
break;
122138
case StyleProfile::DarkPrintSafe:
123139
spec.style.font = "Times";
124140
spec.style.font_pt = 9.0;
125141
spec.style.line_width_pt = 1.6;
126142
spec.style.grid = true;
143+
spec.style.tick_font_scale = 1.0;
144+
spec.style.label_font_scale = 1.08;
145+
spec.style.title_font_scale = 1.15;
146+
spec.style.title_bold = true;
127147
spec.palette = ColorPalette::Grayscale;
128148
break;
129149
case StyleProfile::Tufte_Minimal:
130150
spec.style.font = "Helvetica";
131-
spec.style.font_pt = 10.0;
151+
spec.style.font_pt = 12.5;
132152
spec.style.line_width_pt = 1.8;
133153
spec.style.point_size = 0.6;
134154
spec.style.grid = false;
155+
spec.style.tick_font_scale = 1.0;
156+
spec.style.label_font_scale = 1.30;
157+
spec.style.title_font_scale = 1.55;
158+
spec.style.title_bold = true;
135159
spec.palette = ColorPalette::Grayscale;
136160
break;
137161
}

tests/api_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ int main() {
7575
assert(spec.palette == ColorPalette::Grayscale);
7676
assert(!spec.style.grid);
7777
assert(spec.style.line_width_pt >= 1.5);
78+
assert(spec.style.label_font_scale > 1.0);
79+
assert(spec.style.title_font_scale > spec.style.label_font_scale);
80+
assert(spec.style.title_bold);
7881

7982
FigureSpec built_spec = FigureBuilder(spec).layout(1, 1).manifest(true).spec();
8083
assert(built_spec.write_manifest);

0 commit comments

Comments
 (0)