Skip to content

Commit 9eb18b0

Browse files
author
Hashim Abdul
committed
Add string x-axis named_plot example and docs note
1 parent 30f1698 commit 9eb18b0

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ int main()
6565

6666
![Basic example](./examples/basic.png)
6767

68+
String x-axis labels with legend:
69+
```cpp
70+
#include "../matplotlibcpp.h"
71+
#include <string>
72+
#include <vector>
73+
74+
namespace plt = matplotlibcpp;
75+
76+
int main()
77+
{
78+
std::vector<std::string> x = {"Jan 2021", "Feb 2021", "Mar 2021"};
79+
std::vector<double> y = {58.4, 61.2, 63.9};
80+
81+
plt::named_plot("Vehicle speed (max)", x, y, "o-");
82+
plt::legend();
83+
plt::save("./string_xaxis_named_plot.png");
84+
}
85+
```
86+
g++ examples/string_xaxis_named_plot.cpp -std=c++11 -I. -I/usr/include/python2.7 -lpython2.7
87+
88+
Expected behavior and constraints:
89+
- `x` is a `std::vector<std::string>` and `y` is numeric with matching length.
90+
- String labels are mapped as categorical x values by matplotlib.
91+
- Use `plt::save(...)` for headless runs or `plt::show()` for interactive runs.
92+
6893
Alternatively, matplotlib-cpp also supports some C++11-powered syntactic sugar:
6994
```cpp
7095
#include <cmath>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "../matplotlibcpp.h"
2+
#include <string>
3+
#include <vector>
4+
5+
namespace plt = matplotlibcpp;
6+
7+
int main()
8+
{
9+
const std::vector<std::string> months = {
10+
"Jan 2021", "Feb 2021", "Mar 2021", "Apr 2021", "May 2021"
11+
};
12+
const std::vector<double> speed_kmh = {58.4, 61.2, 63.9, 62.1, 65.0};
13+
14+
plt::figure_size(900, 500);
15+
plt::named_plot("Vehicle speed (max)", months, speed_kmh, "o-");
16+
plt::title("Monthly peak speed");
17+
plt::xlabel("Month");
18+
plt::ylabel("Speed (km/h)");
19+
plt::legend();
20+
plt::tight_layout();
21+
22+
// Use save() for CI/headless usage; replace with show() for interactive runs.
23+
plt::save("./string_xaxis_named_plot.png");
24+
return 0;
25+
}

0 commit comments

Comments
 (0)