Plotting data with C++

We plot with the plotcpp library, which is a thin wrapper around the gnuplot command-line utility. With this library, we can draw points on a scatter plot or draw lines. The initial step to start plotting with this library is creating an object of the Plot class. Then, we have to specify the output destination of the drawing. We can set the destination with the Plot::SetTerminal() method and this method takes a string with a destination point abbreviation. It can be the qt string value to show the operating system (OS) window with our drawing, or it can be a string with a picture file extension to save a drawing to a file, as in the code sample that follows. Also, we can configure a title of the drawing, the axis labels, and some other parameters with the Plot class methods. However, it does not cover all possible configurations available for gnuplot. In a case where we need some unique options, we can use the Plot::gnuplotCommand() method to make direct gnuplot configuration.

There are two drawing modes to draw a set of different graphics on one plot. We can use the Draw2D() method with objects of the Points or Lines classes, but in this case, we should specify all graphics configurations before compilation. The second option is to use the Plot::StartDraw2D() method to get an intermediate drawing state object. Then, we can use the Plot::AddDrawing() method to add different drawings to the one plot. The Plot::EndDraw2D() method should be called after we drew the last graphics.

We can use the Points type for drawing points. An object of this type should be initialized with start and end forward iterators to the integral numeric data types, which represent coordinates. We should specify three iterators to points coordinates, two iterators for the x coordinates where they start and end, and one iterator to the y coordinates' start. The number of coordinates in the containers should be the same. The last parameter is the gnuplot visual style configuration. Objects of the Lines class can be configured in the same way.

When we have completed all drawing operations, we should call the Plot::Flush() method to render all commands to the window or the file, as shown in the following code block:

 ...
using Coords = std::vector<DataType>;
using PointCoords = std::pair<Coords, Coords>;
using Clusters = std::unordered_map<index_t, PointCoords>;

const std::vector<std::string> colors{"black", "red", "blue",
"green", "cyan", "yellow",
"brown", "magenta"};
...
void PlotClusters(const Clusters& clusters,
const std::string& name,
const std::string& file_name) {
plotcpp::Plot plt;
plt.SetTerminal("png");
plt.SetOutput(file_name);
plt.SetTitle(name);
plt.SetXLabel("x");
plt.SetYLabel("y");
plt.SetAutoscale();
plt.gnuplotCommand("set grid");

auto draw_state = plt.StartDraw2D<Coords::const_iterator>();
for (auto& cluster : clusters) {
std::stringstream params;
params << "lc rgb '" << colors[cluster.first] << "' pt 7";
plt.AddDrawing(draw_state,
plotcpp::Points(
cluster.second.first.begin(), cluster.second.first.end(),
cluster.second.second.begin(),
std::to_string(cluster.first) + " cls", params.str()));
}

plt.EndDraw2D(draw_state);
plt.Flush();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.30.19