TBTK
Need a break? Support the development by playing Polarity Puzzles
Plotter.h
Go to the documentation of this file.
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_VISUALIZATION_MAT_PLOT_LIB_PLOTTER
24 #define COM_DAFER45_TBTK_VISUALIZATION_MAT_PLOT_LIB_PLOTTER
25 
26 #include "TBTK/AnnotatedArray.h"
27 #include "TBTK/Array.h"
28 #include "TBTK/Property/Density.h"
29 #include "TBTK/Property/DOS.h"
31 #include "TBTK/Property/LDOS.h"
35 #include "TBTK/Streams.h"
36 #include "TBTK/TBTKMacros.h"
39 #include "TBTK/Visualization/MatPlotLib/matplotlibcpp.h"
42 
43 #include <string>
44 #include <tuple>
45 #include <vector>
46 
47 
48 namespace TBTK{
49 namespace Visualization{
50 namespace MatPlotLib{
51 
197 class Plotter{
198 public:
200  Plotter();
201 
206  void setSize(unsigned int width, unsigned int height);
207 
212  void setBoundsX(double minX, double maxX);
213 
218  void setBoundsY(double minY, double maxY);
219 
226  void setBounds(double minX, double maxX, double minY, double maxY);
227 
229 // void setAutoScaleX(bool autoScaleX);
230 
232 // void setAutoScaleY(bool autoScaleY);
233 
235 // void setAutoScale(bool autoScale);
236 
260  void setAxes(
261  const std::vector<
262  std::pair<unsigned int, std::vector<double>>
263  > &axes
264  );
265 
271  void setTitle(const std::string &title, bool overwrite = true);
272 
278  void setLabelX(const std::string &labelX, bool overwrite = true);
279 
285  void setLabelY(const std::string &labelY, bool overwrite = true);
286 
292  void setLabelZ(const std::string &labelZ, bool overwrite = true);
293 
295 // void plot(double x, double y, const std::string &arguments);
296 
304  void plot(
305  const Property::Density &density,
306  const Argument &argument = ""
307  );
308 
321  void plot(
322  const Index &pattern,
323  const Property::Density &density,
324  const Argument &argument = ""
325  );
326 
333  void plot(
334  const Property::DOS &dos,
335  const Argument &argument = ""
336  );
337 
344  void plot(
345  const Property::EigenValues &eigenValues,
346  const Argument &argument = "black"
347  );
348 
356  void plot(const Property::LDOS &ldos, const Argument &argument = "");
357 
370  void plot(
371  const Index &pattern,
372  const Property::LDOS &ldos,
373  const Argument &argument = ""
374  );
375 
383  void plot(
384  const Vector3d &direction,
385  const Property::Magnetization &magnetization,
386  const Argument &argument = ""
387  );
388 
402  void plot(
403  const Index &pattern,
404  const Vector3d &direction,
405  const Property::Magnetization &magnetization,
406  const Argument &argument = ""
407  );
408 
417  void plot(
418  const Vector3d &direction,
419  const Property::SpinPolarizedLDOS &spinPolarizedLDOS,
420  const Argument &argument = ""
421  );
422 
437  void plot(
438  const Index &pattern,
439  const Vector3d &direction,
440  const Property::SpinPolarizedLDOS &spinPolarizedLDOS,
441  const Argument &argument = ""
442  );
443 
451  void plot(
452  unsigned int state,
453  const Property::WaveFunctions &waveFunctions,
454  const Argument &argument = ""
455  );
456 
470  void plot(
471  const Index &pattern,
472  unsigned int state,
473  const Property::WaveFunctions &waveFunctions,
474  const Argument &argument = ""
475  );
476 
483  void plot(
484  const AnnotatedArray<double, double> &data,
485  const Argument &argument = ""
486  );
487 
494  void plot(
496  const Argument &argument = ""
497  );
498 
505  void plot(const Array<double> &data, const Argument &argument = "");
506 
514  void plot(
515  Array<double> x,
516  const Array<double> &y,
517  const Argument &argument = ""
518  );
519 
528  void plot(
529  Array<double> x,
530  Array<double> y,
531  const Array<double> &z,
532  const Argument &argument = ""
533  );
534 
536 /* void plot(
537  const std::vector<std::vector<double>> &data,
538  const std::vector<std::vector<double>> &intensity,
539  const std::string &arguments
540  );*/
541 
543 /* void plot(
544  const Array<double> &data,
545  const Array<double> &intensity,
546  const std::string &arguments
547  );*/
548 
554  void setPlotMethod3D(const std::string &plotMethod3D);
555 
562  void setRotation(int elevation, int azimuthal, bool overwrite = true);
563 
568  void setNumContours(unsigned int numContours);
569 
571 // void setHold(bool hold);
572 
574  void clear();
575 
577  void show() const;
578 
582  void save(const std::string &filename) const;
583 
588  void plot(
589  const Array<double> &data,
590  const std::initializer_list<
591  std::pair<std::string, std::string>
592  > &argument
593  ){
594  plot(data, Argument(argument));
595  }
596 
597  void plot(
598  const std::initializer_list<double> &data,
599  const Argument &argument = ""
600  ){
601  plot(std::vector<double>(data), argument);
602  }
603 
604  void plot(
605  const std::initializer_list<double> &x,
606  const Array<double> &y,
607  const Argument &argument = ""
608  ){
609  plot(std::vector<double>(x), y, argument);
610  }
611 
612  void plot(
613  const Array<double> &x,
614  const std::initializer_list<double> &y,
615  const Argument &argument = ""
616  ){
617  plot(x, std::vector<double>(y), argument);
618  }
619 
620  void plot(
621  const std::initializer_list<double> &x,
622  const std::initializer_list<double> &y,
623  const Argument &argument = ""
624  ){
625  plot(
626  std::vector<double>(x),
627  std::vector<double>(y),
628  argument
629  );
630  }
632 private:
634  enum class CurrentPlotType{None, Plot1D, PlotSurface, Contourf};
635 
637  CurrentPlotType currentPlotType;
638 
641  enum class PlotMethod3D{PlotSurface, Contourf};
642 
644  PlotMethod3D plotMethod3D;
645 
647  PlotParameters plotParameters;
648 
650  PlotSurfaceParameters plotSurfaceParameters;
651 
653  ContourfParameters contourfParameters;
654 
656  std::vector<std::pair<unsigned int, std::vector<double>>> axes;
657 
659  unsigned int numLines;
660 
662  unsigned int numContours;
663 
665  void plot1D(
666  const std::vector<double> &y,
667  const Argument &argument = ""
668  );
669 
671  void plot1D(
672  const std::vector<double> &x,
673  const std::vector<double> &y,
674  const Argument &argument = ""
675  );
676 
678  void plot2D(
679  const std::vector<std::vector<double>> &z,
680  const Argument &argument = ""
681  );
682 
684  void plot2D(
685  const std::vector<std::vector<double>> &x,
686  const std::vector<std::vector<double>> &y,
687  const std::vector<std::vector<double>> &z,
688  const Argument &argument = ""
689  );
690 
698  AnnotatedArray<double, double> convertAxes(
699  const AnnotatedArray<double, Subindex> &annotatedArray,
700  const std::initializer_list<
701  std::pair<unsigned int, std::pair<double, double>>
702  > &axisReplacement = {}
703  );
704 
709  Array<double> getNonDefaultAxis(
710  const Array<double> &axis,
711  unsigned int axisID
712  ) const;
713 
717  AnnotatedArray<double, Subindex> convertSpinMatrixToDouble(
718  const AnnotatedArray<SpinMatrix, Subindex> &annotatedArray,
719  const Vector3d &direction
720  ) const;
721 
723  std::string colorToHex(const Array<double> &color) const;
724 
726  std::string doubleToHex(double value) const;
727 };
728 
730  currentPlotType = CurrentPlotType::None;
731  plotMethod3D = PlotMethod3D::Contourf;
732  numLines = 0;
733  numContours = 8;
734  clear();
735 }
736 
738  double minX,
739  double maxX
740 ){
741  TBTKAssert(
742  minX < maxX,
743  "Plotter::setBoundsX()",
744  "minX has to be smaller than maxX",
745  ""
746  );
747  plotParameters.setBoundsX(minX, maxX);
748  contourfParameters.setBoundsX(minX, maxX);
749  plotSurfaceParameters.setBoundsX(minX, maxX);
750 }
751 
753  double minY,
754  double maxY
755 ){
756  TBTKAssert(
757  minY < maxY,
758  "Plotter::setBoundsY()",
759  "minY has to be smaller than maxY",
760  ""
761  );
762  plotParameters.setBoundsY(minY, maxY);
763  contourfParameters.setBoundsY(minY, maxY);
764  plotSurfaceParameters.setBoundsY(minY, maxY);
765 }
766 
767 inline void Plotter::setBounds(
768  double minX,
769  double maxX,
770  double minY,
771  double maxY
772 ){
773  setBoundsX(minX, maxX);
774  setBoundsY(minY, maxY);
775 }
776 
777 /*inline void Plotter::setAutoScaleX(bool autoScaleX){
778  this->autoScaleX = autoScaleX;
779 }
780 
781 inline void Plotter::setAutoScaleY(bool autoScaleY){
782  this->autoScaleY = autoScaleY;
783 }
784 
785 inline void Plotter::setAutoScale(bool autoScale){
786  setAutoScaleX(autoScale);
787  setAutoScaleY(autoScale);
788 }*/
789 
790 inline void Plotter::setAxes(
791  const std::vector<
792  std::pair<unsigned int, std::vector<double>>
793  > &axes
794 ){
795  this->axes = axes;
796 }
797 
798 inline void Plotter::setTitle(const std::string &title, bool overwrite){
799  plotParameters.setTitle(title, overwrite);
800  plotSurfaceParameters.setTitle(title, overwrite);
801  contourfParameters.setTitle(title, overwrite);
802  matplotlibcpp::title(title);
803 }
804 
805 inline void Plotter::setLabelX(const std::string &labelX, bool overwrite){
806  plotParameters.setLabelX(labelX, overwrite);
807  plotSurfaceParameters.setLabelX(labelX, overwrite);
808  contourfParameters.setLabelX(labelX, overwrite);
809  matplotlibcpp::xlabel(labelX);
810 }
811 
812 inline void Plotter::setLabelY(const std::string &labelY, bool overwrite){
813  plotParameters.setLabelY(labelY, overwrite);
814  plotSurfaceParameters.setLabelY(labelY, overwrite);
815  contourfParameters.setLabelY(labelY, overwrite);
816  matplotlibcpp::ylabel(labelY);
817 }
818 
819 inline void Plotter::setLabelZ(const std::string &labelZ, bool overwrite){
820  plotSurfaceParameters.setLabelZ(labelZ, overwrite);
821 }
822 
823 inline void Plotter::setPlotMethod3D(const std::string &plotMethod3D){
824  if(plotMethod3D.compare("plot_surface") == 0){
825  this->plotMethod3D = PlotMethod3D::PlotSurface;
826  }
827  else if(plotMethod3D.compare("contourf") == 0){
828  this->plotMethod3D = PlotMethod3D::Contourf;
829  }
830  else{
831  TBTKExit(
832  "Plotter::setPlotMethod3D()",
833  "Unknown plot method.",
834  "Must be 'plot_surface' or 'contourf'."
835  );
836  }
837 }
838 
839 inline void Plotter::setRotation(int elevation, int azimuthal, bool overwrite){
840  plotSurfaceParameters.setRotation(elevation, azimuthal, overwrite);
841  switch(currentPlotType){
842  case CurrentPlotType::PlotSurface:
843  plotSurfaceParameters.flush();
844  break;
845  default:
846  break;
847  }
848 }
849 
850 inline void Plotter::setNumContours(unsigned int numContours){
851  this->numContours = numContours;
852 }
853 
854 /*inline void Plotter::setHold(bool hold){
855  this->hold = hold;
856 }*/
857 
858 inline void Plotter::clear(){
859  plotParameters.clear();
860  plotSurfaceParameters.clear();
861  contourfParameters.clear();
862  axes.clear();
863  numLines = 0;
864  numContours = 8;
865  matplotlibcpp::clf();
866 }
867 
868 inline void Plotter::show() const{
869  matplotlibcpp::show();
870 }
871 
872 inline void Plotter::save(const std::string &filename) const{
873  matplotlibcpp::save(filename);
874 }
875 
876 }; //End namespace MatPlotLib
877 }; //End namespace Visualization
878 }; //End namespace TBTK
879 
880 #endif
TBTK::Array< double >
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::setLabelY
void setLabelY(const std::string &labelY, bool overwrite=true)
Definition: PlotSurfaceParameters.h:111
TBTK::AnnotatedArray
Array with additional information about its axes.
Definition: AnnotatedArray.h:41
TBTK::Property::DOS
Property container for density of states (DOS).
Definition: DOS.h:48
TBTK::Visualization::MatPlotLib::ContourfParameters::setTitle
void setTitle(const std::string &title, bool overwrite=true)
Definition: ContourfParameters.h:83
TBTK::Visualization::MatPlotLib::PlotParameters
Definition: PlotParameters.h:35
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::setLabelZ
void setLabelZ(const std::string &labelZ, bool overwrite=true)
Definition: PlotSurfaceParameters.h:119
TBTK::Visualization::MatPlotLib::Argument
Argument to matplotlib.
Definition: Argument.h:59
TBTK::Visualization::MatPlotLib::PlotParameters::setLabelY
void setLabelY(const std::string &labelY, bool overwrite=true)
Definition: PlotParameters.h:95
EigenValues.h
Property container for eigen values.
TBTK::Property::SpinPolarizedLDOS
Property container for spin-polarized local density of states (spin-polarized LDOS).
Definition: SpinPolarizedLDOS.h:46
TBTK::Visualization::MatPlotLib::PlotParameters::setBoundsX
void setBoundsX(double minX, double maxX, bool overwrite=true)
Definition: PlotParameters.h:103
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::setTitle
void setTitle(const std::string &title, bool overwrite=true)
Definition: PlotSurfaceParameters.h:95
TBTK::Visualization::MatPlotLib::PlotParameters::clear
void clear()
Definition: PlotParameters.h:138
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::setLabelX
void setLabelX(const std::string &labelX, bool overwrite=true)
Definition: PlotSurfaceParameters.h:103
Streams.h
Streams for TBTK output.
TBTK::Property::LDOS
Property container for the local density of states (LDOS).
Definition: LDOS.h:48
TBTK::Visualization::MatPlotLib::Plotter::save
void save(const std::string &filename) const
Definition: Plotter.h:872
TBTK::Visualization::MatPlotLib::Plotter::setBoundsY
void setBoundsY(double minY, double maxY)
Definition: Plotter.h:752
Array.h
Multi-dimensional array.
WaveFunctions.h
Property container for wave functions.
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::clear
void clear()
Definition: PlotSurfaceParameters.h:179
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::setBoundsX
void setBoundsX(double minX, double maxX, bool overwrite=true)
Definition: PlotSurfaceParameters.h:138
AnnotatedArray.h
Array with additional information about its axes.
ContourfParameters.h
Parameter container for plotting using the matplotlib function contourf.
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::setBoundsY
void setBoundsY(double minY, double maxY, bool overwrite=true)
Definition: PlotSurfaceParameters.h:149
TBTK::Visualization::MatPlotLib::ContourfParameters::setBoundsY
void setBoundsY(double minY, double maxY, bool overwrite=true)
Definition: ContourfParameters.h:118
TBTK::Visualization::MatPlotLib::Plotter::setPlotMethod3D
void setPlotMethod3D(const std::string &plotMethod3D)
Definition: Plotter.h:823
TBTK::Visualization::MatPlotLib::Plotter::setTitle
void setTitle(const std::string &title, bool overwrite=true)
Definition: Plotter.h:798
TBTK::Vector3d
Definition: Vector3d.h:33
TBTK::Visualization::MatPlotLib::ContourfParameters::setBoundsX
void setBoundsX(double minX, double maxX, bool overwrite=true)
Definition: ContourfParameters.h:107
TBTK::Visualization::MatPlotLib::Plotter::show
void show() const
Definition: Plotter.h:868
TBTK::Visualization::MatPlotLib::PlotParameters::setLabelX
void setLabelX(const std::string &labelX, bool overwrite=true)
Definition: PlotParameters.h:87
TBTK::Visualization::MatPlotLib::ContourfParameters
Definition: ContourfParameters.h:36
LDOS.h
Property container for local density of states (LDOS).
PlotSurfaceParameters.h
Parameter container for plotting using the matplotlib function plot_surface.
TBTK::Visualization::MatPlotLib::Plotter::setSize
void setSize(unsigned int width, unsigned int height)
SpinPolarizedLDOS.h
Property container for spin-polarized local density of states (spin-polarized LDOS).
TBTK::Visualization::MatPlotLib::Plotter::plot
void plot(const Property::Density &density, const Argument &argument="")
DOS.h
Property container for density of states (DOS).
TBTK::Visualization::MatPlotLib::ContourfParameters::setLabelX
void setLabelX(const std::string &labelX, bool overwrite=true)
Definition: ContourfParameters.h:91
TBTK::Property::Density
Property container for density.
Definition: Density.h:43
TBTK::Visualization::MatPlotLib::Plotter::setLabelY
void setLabelY(const std::string &labelY, bool overwrite=true)
Definition: Plotter.h:812
TBTK::Visualization::MatPlotLib::Plotter::setLabelZ
void setLabelZ(const std::string &labelZ, bool overwrite=true)
Definition: Plotter.h:819
TBTK::Visualization::MatPlotLib::Plotter::setBoundsX
void setBoundsX(double minX, double maxX)
Definition: Plotter.h:737
Magnetization.h
Property container for magnetization.
TBTK::Visualization::MatPlotLib::Plotter::setRotation
void setRotation(int elevation, int azimuthal, bool overwrite=true)
Definition: Plotter.h:839
Argument.h
Argument to matplotlib.
TBTK::Visualization::MatPlotLib::Plotter::setLabelX
void setLabelX(const std::string &labelX, bool overwrite=true)
Definition: Plotter.h:805
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::flush
void flush() const
Definition: PlotSurfaceParameters.h:160
TBTK::Visualization::MatPlotLib::PlotParameters::setTitle
void setTitle(const std::string &title, bool overwrite=true)
Definition: PlotParameters.h:82
TBTK::Visualization::MatPlotLib::Plotter::clear
void clear()
Definition: Plotter.h:858
TBTK::Visualization::MatPlotLib::ContourfParameters::clear
void clear()
Definition: ContourfParameters.h:142
TBTK::Visualization::MatPlotLib::ContourfParameters::setLabelY
void setLabelY(const std::string &labelY, bool overwrite=true)
Definition: ContourfParameters.h:99
Density.h
Property container for density.
TBTKMacros.h
Precompiler macros.
TBTK::Visualization::MatPlotLib::Plotter::Plotter
Plotter()
Definition: Plotter.h:729
TBTK::Visualization::MatPlotLib::Plotter::setNumContours
void setNumContours(unsigned int numContours)
Definition: Plotter.h:850
TBTK::Visualization::MatPlotLib::Plotter
Plots data.
Definition: Plotter.h:197
TBTK::Visualization::MatPlotLib::Plotter::setBounds
void setBounds(double minX, double maxX, double minY, double maxY)
Definition: Plotter.h:767
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters
Definition: PlotSurfaceParameters.h:36
TBTK::Visualization::MatPlotLib::PlotParameters::setBoundsY
void setBoundsY(double minY, double maxY, bool overwrite=true)
Definition: PlotParameters.h:114
TBTK::Visualization::MatPlotLib::PlotSurfaceParameters::setRotation
void setRotation(int elevation, int azimuthal, bool overwrite=true)
Definition: PlotSurfaceParameters.h:127
TBTK::Index
Physical index.
Definition: Index.h:44
TBTK::Visualization::MatPlotLib::Plotter::setAxes
void setAxes(const std::vector< std::pair< unsigned int, std::vector< double >> > &axes)
Definition: Plotter.h:790
PlotParameters.h
Parameter container for plotting using the matplotlib function plot.
TBTK::Property::EigenValues
Property container for eigen values.
Definition: EigenValues.h:46
TBTK::Property::WaveFunctions
Property container for wave function.
Definition: WaveFunctions.h:45
TBTK::Property::Magnetization
Property container for magnetization.
Definition: Magnetization.h:44