Skip to content

Title is drawn into scatter3 plot #457

@zandivx

Description

@zandivx

Bug category

  • bug - compilation error
  • bug - compilation warning
  • bug - runtime error
  • bug - runtime warning
  • bug - logic error

Describe the bug
Upon adding a title("...") call to one of the examples provided under https://alandefreitas.github.io/matplotplusplus/plot-types/data-distribution/scatter-plot-3d/ the title is drawn directly into the data.

Steps to Reproduce

1. Copy an example from https://alandefreitas.github.io/matplotplusplus/plot-types/data-distribution/scatter-plot-3d/
#include <matplot/matplot.h>
#include <tuple>

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data();

int main() {
  using namespace matplot;

  auto [x, y, z] = generate_data();

  std::vector<double> sizes(x.size() / 3, 16);
  std::fill_n(std::back_inserter(sizes), x.size() / 3, 8);
  std::fill_n(std::back_inserter(sizes), x.size() / 3, 2);

  std::vector<double> colors(x.size() / 3, 1);
  std::fill_n(std::back_inserter(colors), x.size() / 3, 2);
  std::fill_n(std::back_inserter(colors), x.size() / 3, 3);

  scatter3(x, y, z, sizes, colors);
  title("Title"); // <- HERE
  view(40, 35);

  save("plot.png");
  return 0;
}

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data() {
  using namespace matplot;
  int n = 16;
  auto r = iota(-n, 2., n);
  auto theta = transform(r, [n](double x) { return x / n * pi; });
  auto phi = transform(r, [n](double x) { return x / n * pi / 2.; });
  auto sinphi = transform(phi, [](double x) { return sin(x); });
  auto cosphi = transform(phi, [](double x) { return cos(x); });
  cosphi.front() = 0;
  cosphi.back() = 0;
  auto sintheta = transform(theta, [](double x) { return sin(x); });
  sintheta.front() = 0;
  sintheta.back() = 0;
  auto costheta = transform(theta, [](double x) { return cos(x); });
  std::vector<std::vector<double>> X(17, std::vector<double>(17, 0.));
  std::vector<std::vector<double>> Y(17, std::vector<double>(17, 0.));
  std::vector<std::vector<double>> Z(17, std::vector<double>(17, 0.));
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < n + 1; ++j) {
      X[i][j] = cosphi[i] * costheta[j];
      Y[i][j] = cosphi[i] * sintheta[j];
      Z[i][j] = sinphi[i];
    }
  }
  auto X1d = reshape(X);
  auto Y1d = reshape(Y);
  auto Z1d = reshape(Z);
  std::vector<double> x =
      concat(concat(transform(X1d, [](double x) { return x * 0.5; }),
                    transform(X1d, [](double x) { return x * 0.75; })),
             X1d);
  std::vector<double> y =
      concat(concat(transform(Y1d, [](double y) { return y * 0.5; }),
                    transform(Y1d, [](double y) { return y * 0.75; })),
             Y1d);
  std::vector<double> z =
      concat(concat(transform(Z1d, [](double z) { return z * 0.5; }),
                    transform(Z1d, [](double z) { return z * 0.75; })),
             Z1d);

  return std::make_tuple(x, y, z);
}
  1. Create a minimal working CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)

project(Scatter3DTitleIssueMWE)

enable_language(CXX)
set(CMAKE_CXX_STANDARD 23)

find_package(Matplot++ REQUIRED)

add_executable(mwe main.cpp)
target_link_libraries(mwe PRIVATE Matplot++::matplot)
  1. Build it
/usr/bin/cmake -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ --no-warn-unused-cli -S/tmp/matplotplusplus-scatter3d-title-issue-mwe -B/tmp/matplotplusplus-scatter3d-title-issue-mwe/build -G "Ninja Multi-Config"

Output

Image

Platform

  • cross-platform issue - linux
  • cross-platform issue - windows
  • cross-platform issue - macos

Environment Details:

  • OS: Ubuntu (WSL)
  • OS Version: 24.04.2
  • Compiler: gcc
  • Compiler version: 13.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions