Skip to content

Commit

Permalink
Makes syntax more consistent between single plots and figures with mu…
Browse files Browse the repository at this point in the history
…ltiple subplots. Starts general code cleanup.
  • Loading branch information
pazmivaniye committed Jun 14, 2024
1 parent ad62b54 commit 6350559
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 81 deletions.
64 changes: 41 additions & 23 deletions pgfplotter
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace pgfplotter
{
template<typename T>
using require_iterable = std::enable_if_t<std::is_same<decltype(std::begin(
std::declval<const T&>())), decltype(std::end(std::declval<const
T&>()))>::value>;

std::array<std::vector<double>, 3> mesh_grid(const std::function<double(
double, double)>& f, double xMin, double xMax, double yMin, double yMax,
std::size_t res);
Expand All @@ -17,12 +22,8 @@ namespace pgfplotter

class Plotter
{
friend void plot_multiple(const std::string&, const Plotter&, const
Plotter&, bool);
friend void plot_multiple(const std::string&, const Plotter&, const
Plotter&, const Plotter&, bool);
friend void plot_multiple(const std::string&, const std::vector<
Plotter>&, bool);
friend void plot(const std::string&, const std::vector<const
Plotter*>&);

static constexpr std::array<std::array<int, 3>, 5> _colorCycle = {{
{ 0, 57, 181},
Expand All @@ -32,12 +33,10 @@ namespace pgfplotter
{255, 227, 0}
}};

const std::string name;

std::string titleStr;
std::string xLabel;
std::string yLabel;
std::string zLabel;
std::string _title;
std::string _xLabel;
std::string _yLabel;
std::string _zLabel;

std::vector<std::array<std::vector<double>, 3>> data;
std::vector<std::vector<double>> surfaceX;
Expand Down Expand Up @@ -142,10 +141,10 @@ namespace pgfplotter
static constexpr unsigned int Northwest = 5;
static constexpr unsigned int Southwest = 6;

void title(const std::string& title);
void x_label(const std::string& xLabel);
void y_label(const std::string& yLabel);
void z_label(const std::string& zLabel);
void setTitle(const std::string& title);
void setXLabel(const std::string& xLabel);
void setYLabel(const std::string& yLabel);
void setZLabel(const std::string& zLabel);

void line(const std::vector<double>& x, const std::vector<double>& y,
const std::vector<double>& z, const std::string& name, const std::
Expand Down Expand Up @@ -451,7 +450,6 @@ namespace pgfplotter
void axis_equal_image();
void resize(double width, double height);
void resize(double size);
void plot(const std::string& path, bool deleteData = false) const;
void x_precision(int n);
void y_precision(int n);
void z_precision(int n);
Expand All @@ -475,12 +473,32 @@ namespace pgfplotter
void bidirColormap();
};

void plot_multiple(const std::string& path, const Plotter& p, const Plotter&
q, bool deleteData = false);
void plot_multiple(const std::string& path, const Plotter& p, const Plotter&
q, const Plotter& r, bool deleteData = false);
void plot_multiple(const std::string& path, const std::vector<Plotter>& p,
bool deleteData = false);
// Note: ".png" is automatically appended to plot path.
template<typename... Ts>
void plot(const std::string& path, const Plotter& p, Ts&&... q)
{
std::vector<const Plotter*> ptrs = {&p};
plot(path, ptrs, q...);
}
template<typename... Ts>
void plot(const std::string& path, std::vector<const Plotter*> ptrs, const
Plotter& p, Ts&&... q)
{
ptrs.push_back(&p);
plot(path, ptrs, q...);
}
template<typename T, require_iterable<T>* = nullptr>
void plot(const std::string& path, const T& p)
{
std::vector<const Plotter*> ptrs;
ptrs.reserve(p.size());
for(auto& n : p)
{
ptrs.push_back(&n);
}
plot(path, ptrs);
}
void plot(const std::string& path, const std::vector<const Plotter*>& ptrs);
}

#endif
75 changes: 23 additions & 52 deletions plotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,24 +423,24 @@ static const std::string src4 = "\\end{groupplot}" + endl +
static const std::string src5 = "\\end{document}";
static const std::string src7 = "" + endl;

void pgfplotter::Plotter::title(const std::string& title)
void pgfplotter::Plotter::setTitle(const std::string& title)
{
titleStr = title;
_title = title;
}

void pgfplotter::Plotter::x_label(const std::string& xLabel)
void pgfplotter::Plotter::setXLabel(const std::string& xLabel)
{
this->xLabel = xLabel;
_xLabel = xLabel;
}

void pgfplotter::Plotter::y_label(const std::string& yLabel)
void pgfplotter::Plotter::setYLabel(const std::string& yLabel)
{
this->yLabel = yLabel;
_yLabel = yLabel;
}

void pgfplotter::Plotter::z_label(const std::string& zLabel)
void pgfplotter::Plotter::setZLabel(const std::string& zLabel)
{
this->zLabel = zLabel;
_zLabel = zLabel;
}

void pgfplotter::Plotter::line(const std::vector<double>& x, const std::vector<double>& y,
Expand Down Expand Up @@ -877,9 +877,9 @@ std::string pgfplotter::Plotter::plot_src(const std::string& path, int subplot)
src += ", /pgf/number format/sci";
}
src += "}, label style = {font = \\" + FontSize + "}, ylabel near ticks";
if(!zLabel.empty())
if(!_zLabel.empty())
{
src += ", ylabel = {" + zLabel + "}";
src += ", ylabel = {" + _zLabel + "}";
}
src += "}";

Expand Down Expand Up @@ -1007,21 +1007,21 @@ std::string pgfplotter::Plotter::plot_src(const std::string& path, int subplot)
zMax);//TEMP
}*/

if(!xLabel.empty())
if(!_xLabel.empty())
{
src += ", xlabel = {" + xLabel + "}";
src += ", xlabel = {" + _xLabel + "}";
}
if(!yLabel.empty())
if(!_yLabel.empty())
{
src += ", ylabel = {" + yLabel + "}";
src += ", ylabel = {" + _yLabel + "}";
}
if(!zLabel.empty())
if(!_zLabel.empty())
{
src += ", zlabel = {" + zLabel + "}";
src += ", zlabel = {" + _zLabel + "}";
}
if(!titleStr.empty())
if(!_title.empty())
{
src += ", title = {\\" + TitleSize + " " + titleStr + "}";
src += ", title = {\\" + TitleSize + " " + _title + "}";
}
if(legendPos)
{
Expand Down Expand Up @@ -1316,37 +1316,8 @@ std::string pgfplotter::Plotter::plot_src(const std::string& path, int subplot)
return src;
}

void pgfplotter::Plotter::plot(const std::string& path, bool deleteData) const
{
const std::string src = src0 + src2a + "1" + src2bNoSep + plot_src(path, 0)
+ src4 + src5;

compile(path, src, deleteData);
}

void pgfplotter::plot_multiple(const std::string& path, const pgfplotter::Plotter& p, const pgfplotter::Plotter& q,
bool deleteData)
{
const bool b = p._noSep || q._noSep;
const std::string src = src0 + src2a + "2" + (b ? src2bNoSep : src2b) + p.
plot_src(path, 0) + q.plot_src(path, 1) + src4 + src5;

compile(path, src, deleteData);
}

void pgfplotter::plot_multiple(const std::string& path, const pgfplotter::Plotter& p, const pgfplotter::Plotter& q,
const pgfplotter::Plotter& r, bool deleteData)
{
const bool b = p._noSep || q._noSep || r._noSep;
const std::string src = src0 + src2a + "3" + (b ? src2bNoSep : src2b) + p.
plot_src(path, 0) + q.plot_src(path, 1) + r.plot_src(path, 2) + src4 +
src5;

compile(path, src, deleteData);
}

void pgfplotter::plot_multiple(const std::string& path, const std::vector<pgfplotter::Plotter>& p, bool
deleteData)
void pgfplotter::plot(const std::string& path, const std::vector<const
pgfplotter::Plotter*>& p)
{
if(p.empty())
{
Expand All @@ -1358,16 +1329,16 @@ void pgfplotter::plot_multiple(const std::string& path, const std::vector<pgfplo
bool b = false;
for(const auto& n : p)
{
b = b || n._noSep;
b = b || n->_noSep;
}

std::string src = src0 + src2a + std::to_string(p.size()) + (b ? src2bNoSep
: src2b);
for(std::size_t i = 0, n = p.size(); i < n; ++i)
{
src += p[i].plot_src(path, i);
src += p[i]->plot_src(path, i);
}
src += src4 + src5;

compile(path, src, deleteData);
compile(path, src, false);
}
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ build:
mkdir -p $@

clean:
$(RM) test build/* output/*
$(RM) -r test build output
26 changes: 21 additions & 5 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
std::cout << "Passed " << test << std::endl; \
++test;

namespace pgf = pgfplotter;

static const std::string PlotName = "plot";

static std::string get_dir(const std::string& path)
Expand All @@ -38,11 +40,11 @@ int main(int, char** argv)
}
CATCH

pgf::Plotter p;
try
{
const auto data = pgfplotter::mesh_grid([](double x, double y){ return
std::sin(x)*std::sin(y); }, 0., 1., 0., 1., 50);
pgfplotter::Plotter p;
p.surf(data[0], data[1], data[2]);
p.x_min(0.);
p.x_max(1.);
Expand All @@ -51,10 +53,11 @@ int main(int, char** argv)
p.z_min(0.);
p.z_max(1.);
p.view(45., 45.);
p.x_label("$x$ (\\si{\\lu})");
p.y_label("$y$ (\\si{\\arcsec})");
p.z_label("$\\dv{^2x}{y^2}$ (\\si{\\lu^2\\per\\arcsec^2})");
p.plot(outputDir + "/" + PlotName);
p.setXLabel("$x$ (\\si{\\lu})");
p.setYLabel("$y$ (\\si{\\arcsec})");
p.setZLabel("$\\dv{^2x}{y^2}$ (\\si{\\lu^2\\per\\arcsec^2})");
p.setTitle("Test Plot");
pgf::plot(outputDir + "/" + PlotName, p);
}
CATCH

Expand Down Expand Up @@ -102,4 +105,17 @@ int main(int, char** argv)
}
}
CATCH

try
{
pgf::plot(outputDir + "/" + PlotName + "-1", p, p);
}
CATCH

try
{
std::vector<pgf::Plotter> v(2, p);
pgf::plot(outputDir + "/" + PlotName + "-2", v);
}
CATCH
}

0 comments on commit 6350559

Please sign in to comment.