From 2267c82f0fc69eaf94e62c8ab9cd86beee38b17b Mon Sep 17 00:00:00 2001 From: chn Date: Sat, 7 Oct 2023 17:55:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=94=BB=E5=9B=BE=EF=BC=8C?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E8=AE=BE=E7=BD=AE=20y=20=E8=BD=B4=E5=88=BB?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ufo/plot.hpp | 6 ++++-- src/plot.cpp | 23 ++++++++++++++++++----- test/14.2.6.4.plot.yaml | 1 + 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/ufo/plot.hpp b/include/ufo/plot.hpp index 3117322..922a4e7 100644 --- a/include/ufo/plot.hpp +++ b/include/ufo/plot.hpp @@ -16,6 +16,7 @@ namespace ufo std::pair Resolution; std::pair Range; std::string Filename; + std::optional> YTicks; }; std::vector Figures; @@ -43,7 +44,7 @@ namespace ufo double threshold, bool exclude_endpoint = false ); // 根据搜索到的 q 点, 计算每个点的数值 - static std::vector> calculate_values + static std::tuple>, std::vector> calculate_values ( const std::vector>& path, const std::vector>>& qpoints, @@ -54,7 +55,8 @@ namespace ufo static void plot ( const std::vector>& values, - const decltype(InputType::FigureConfigType::Filename)& filename + const decltype(InputType::FigureConfigType::Filename)& filename, + const std::vector& x_ticks, const std::vector& y_ticks ); }; } diff --git a/src/plot.cpp b/src/plot.cpp index c1bf9fd..7bab8a4 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -46,6 +46,8 @@ namespace ufo Figures.back().Resolution = figure["Resolution"].as>(); Figures.back().Range = figure["Range"].as>(); Figures.back().Filename = figure["Filename"].as(); + if (figure["YTicks"]) + Figures.back().YTicks = figure["YTicks"].as>(); } SourceFilename = input["SourceFilename"].as(); Source = SourceType(SourceFilename); @@ -70,8 +72,9 @@ namespace ufo 0.001, i != path.size() - 2 )); } - auto values = calculate_values(lines, qpoints, figure.Resolution, figure.Range); - plot(values, figure.Filename); + auto [values, x_ticks] = calculate_values + (lines, qpoints, figure.Resolution, figure.Range); + plot(values, figure.Filename, x_ticks, figure.YTicks.value_or(std::vector{})); } return *this; } @@ -123,7 +126,7 @@ namespace ufo return result; } - std::vector> PlotSolver::calculate_values + std::tuple>, std::vector> PlotSolver::calculate_values ( const std::vector>& path, const std::vector>>& qpoints, @@ -134,12 +137,17 @@ namespace ufo // 整理输入 std::map> qpoints_with_distance; double total_distance = 0; + std::vector x_ticks; for (unsigned i = 0; i < path.size(); i++) { for (auto& _ : qpoints[i]) qpoints_with_distance.emplace(total_distance + (_.get().Qpoint - path[i].first).norm(), _); total_distance += (path[i].second - path[i].first).norm(); + if (i != path.size() - 1) + x_ticks.push_back(total_distance); } + for (auto& _ : x_ticks) + _ = _ / total_distance * resolution.first; // 插值 std::vector> values; @@ -183,12 +191,13 @@ namespace ufo resolution.second, range) ); } - return values; + return {values, x_ticks}; } void PlotSolver::plot ( const std::vector>& values, - const decltype(InputType::FigureConfigType::Filename)& filename + const decltype(InputType::FigureConfigType::Filename)& filename, + const std::vector& x_ticks, const std::vector& y_ticks ) { std::vector> @@ -210,6 +219,10 @@ namespace ufo auto ax = f->current_axes(); ax->image(std::tie(r, g, b)); ax->y_axis().reverse(false); + ax->y_axis().ticklabels({}); + ax->y_axis().tick_values(y_ticks); + ax->x_axis().ticklabels({}); + ax->x_axis().tick_values(x_ticks); f->save(filename); } } diff --git a/test/14.2.6.4.plot.yaml b/test/14.2.6.4.plot.yaml index 80ff172..113d1c7 100644 --- a/test/14.2.6.4.plot.yaml +++ b/test/14.2.6.4.plot.yaml @@ -12,3 +12,4 @@ Figures: Resolution: [ 1024, 400 ] Range: [ -5, 35 ] Filename: ./test/14.2.6.4.png + YTicks: [ 0, 5, 10, 15, 20, 25, 30 ]