mirror of
https://github.com/jkriege2/JKQtPlotter.git
synced 2025-01-12 00:40:32 +08:00
added example for log axes
This commit is contained in:
parent
4949176229
commit
2724b007aa
@ -18,7 +18,8 @@ All test-projects are Qt-projects that use qmake to build. You can load them int
|
||||
| Screenshot | Description | Notes |
|
||||
|:-------------:| ------------- | ------------- |
|
||||
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest1_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest) | [Very Basic Example (Line Graph)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest) | `JKQTPxyLineGraph`<br/>C-style arrays of data |
|
||||
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_symbols_and_styles_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_symbols_and_styles) | [Line Graph with Different Symbols and Line Styles](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_symbols_and_styles) | `JKQTPxyLineGraph`<br/>C++ vector of data |
|
||||
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_symbols_and_styles_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_symbols_and_styles) | [Line Graph with Different Symbols and Line Styles](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_symbols_and_styles) | `JKQTPxyLineGraph`<br/>C++ vector of data<br/>setting line styles and symbol styles<br/>automatic graph coloring |
|
||||
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_logaxes_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_logaxes) | [logarithmic axes](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_logaxes) | `JKQTPxyLineGraph` and `JKQTPgeoText`<br/>C++ vector of data<br/>logarithmic axes<br/>plot line styles<br/>internal LaTeX parser<br/>add commenting text to a graph |
|
||||
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_symbols_and_errors_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_symbols_and_errors) | [Simple Line/Symbol Graph With Errorbars](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_symbols_and_errors) | `JKQTPxyLineErrorGraph`<br/>C-style arrays of data |
|
||||
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_barchart_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_barchart) | [Simple Bar Charts](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_barchart) | `JKQTPbarVerticalGraph`<br/>C-style arrays of data |
|
||||
| [![](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/JKQTPbarVerticalGraphStacked_small.png)](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_stackedbars) | [Stacked Bar Charts](https://github.com/jkriege2/JKQtPlotter/tree/master/test/jkqtplotter_simpletest_stackedbars) | `JKQTPbarVerticalStackableGraph`, `JKQTPbarHorizontalStackableGraph`<br/>C++-style vectors of data |
|
||||
|
BIN
screenshots/jkqtplotter_simpletest_logaxes.png
Normal file
BIN
screenshots/jkqtplotter_simpletest_logaxes.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 31 KiB |
BIN
screenshots/jkqtplotter_simpletest_logaxes_nolog.png
Normal file
BIN
screenshots/jkqtplotter_simpletest_logaxes_nolog.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
BIN
screenshots/jkqtplotter_simpletest_logaxes_small.png
Normal file
BIN
screenshots/jkqtplotter_simpletest_logaxes_small.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
69
test/jkqtplotter_simpletest_logaxes/README.md
Normal file
69
test/jkqtplotter_simpletest_logaxes/README.md
Normal file
@ -0,0 +1,69 @@
|
||||
[Back to JKQTPlotter main page](https://github.com/jkriege2/JKQtPlotter/)
|
||||
|
||||
# JKQtPlotter
|
||||
|
||||
## Line Graph with Logarithmic y-axis
|
||||
This project (see `./test/jkqtplotter_simpletest_logaxes/`) simply creates a JKQtPlotter widget (as a new window) and several line-graphs of different resonance curves.
|
||||
|
||||
The source code of the main application can be found in [`jkqtplotter_simpletest_logaxes.cpp`](https://github.com/jkriege2/JKQtPlotter/blob/master/test/jkqtplotter_simpletest_logaxes/jkqtplotter_simpletest_logaxes.cpp). Mainly several graphs are generated in a loop and then different line styles are applied to the graphs (set by ``graph->set_style()`). The colors are set automtically from an internal default palette. The main loop looks like this:
|
||||
|
||||
```c++
|
||||
QVector<Qt::PenStyle> pens {Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine, Qt::DashDotDotLine };
|
||||
for (int id=0; id<D.size(); id++) {
|
||||
// generate some plot data
|
||||
QVector<double> Y;
|
||||
for (auto& xx: X) {
|
||||
Y<<1.0/sqrt(sqr(1-sqr(xx))+sqr(2*xx*D[id]));
|
||||
}
|
||||
|
||||
JKQTPxyLineGraph* graph=new JKQTPxyLineGraph(&plot);
|
||||
|
||||
// copy data into datastore and immediately set the yColumn
|
||||
graph->set_xColumn(columnX);
|
||||
graph->set_yColumn(ds->addCopiedColumn(Y, "y"+QString::number(id)));
|
||||
|
||||
// don't use symbols
|
||||
graph->set_symbol(JKQTPnoSymbol);
|
||||
// use one of different pens
|
||||
graph->set_style(pens[id%pens.size()]);
|
||||
// set width of graph line
|
||||
graph->set_lineWidth(1.5);
|
||||
|
||||
// graph title is made from symbol+penstyle
|
||||
graph->set_title(QString("D=\\delta/\\omega_0=%1").arg(D[id]));
|
||||
|
||||
// add the graph to the plot, so it is actually displayed
|
||||
plot.addGraph(graph);
|
||||
}
|
||||
```
|
||||
|
||||
Then a `JKQTPgeoText` is added to the graph, which shows the function plotted in the plot:
|
||||
```c++
|
||||
// 4. Also we add a text-element in the plot to show the plotted function
|
||||
// This element (JKQTPgeoText) is taken from the set of geometric elements
|
||||
// and is simply parametrized by a position (1.8/10) and the text to display.
|
||||
// In addition you can also set the font size (here to 15)
|
||||
plot.addGraph(new JKQTPgeoText(&plot, 1.8, 10, "\\frac{A}{A_{stat}}=\\frac{1}{\\sqrt{\\left(1-\\eta^2\\right)^2+\\left(2{\\eta}D\\right)^2}}", 15));
|
||||
// for nicer rendering we set the fonts used by the internal LaTeX parser instance to XITS
|
||||
plot.get_plotter()->get_mathText()->useXITS();
|
||||
```
|
||||
|
||||
Finally the y-axis is switched to logarithmic scaling and the axis labels are set:
|
||||
```c++
|
||||
// 4. set y-axis to logarithmic (x-axis would be analogous)
|
||||
// and set axis labels (using LaTeX notation)
|
||||
plot.get_yAxis()->set_logAxis(true);
|
||||
plot.get_yAxis()->set_axisLabel("Amplitude A/A_{stat}");
|
||||
plot.get_xAxis()->set_axisLabel("relative driving frequency \\eta=\\omega/\\omega_0");
|
||||
```
|
||||
|
||||
The result looks like this:
|
||||
|
||||
![jkqtplotter_simpletest_symbols_and_styles](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_logaxes.png)
|
||||
|
||||
Without the logarithmic scaling we would have:
|
||||
|
||||
![jkqtplotter_simpletest_symbols_and_styles](https://raw.githubusercontent.com/jkriege2/JKQtPlotter/master/screenshots/jkqtplotter_simpletest_logaxes_nolog.png)
|
||||
|
||||
|
||||
[Back to JKQTPlotter main page](https://github.com/jkriege2/JKQtPlotter/)
|
@ -0,0 +1,81 @@
|
||||
#include <QApplication>
|
||||
#include "jkqtplotter/jkqtplotter.h"
|
||||
#include "jkqtplotter/jkqtpgeoelements.h"
|
||||
#include "jkqtplottertools/jkqtptools.h"
|
||||
|
||||
#define sqr(x) ((x)*(x))
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
||||
JKQtPlotter plot;
|
||||
JKQTPdatastore* ds=plot.getDatastore();
|
||||
|
||||
// 2. now we create data a vector of x-values for a simple plot (resonance curve with different damping value D)
|
||||
// see https://en.wikipedia.org/wiki/Resonance
|
||||
QVector<double> X;
|
||||
QVector<double> D { 0.001, 0.1, 1, 10};
|
||||
const int Ndata=500; // number of plot points in each curve
|
||||
for (int i=0; i<Ndata; i++) {
|
||||
const double x=double(i)/double(Ndata)*3;
|
||||
X<<x;
|
||||
//Y1<<1.0/sqrt(sqr(1-sqr(x))+sqr(2*x*D1));
|
||||
}
|
||||
// and copy it to the datastore
|
||||
size_t columnX=ds->addCopiedColumn(X, "x");
|
||||
|
||||
// 3. now we make several plots for different values of D
|
||||
QVector<Qt::PenStyle> pens {Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine, Qt::DashDotDotLine };
|
||||
for (int id=0; id<D.size(); id++) {
|
||||
// generate some plot data
|
||||
QVector<double> Y;
|
||||
for (auto& xx: X) {
|
||||
Y<<1.0/sqrt(sqr(1-sqr(xx))+sqr(2*xx*D[id]));
|
||||
}
|
||||
|
||||
JKQTPxyLineGraph* graph=new JKQTPxyLineGraph(&plot);
|
||||
|
||||
// copy data into datastore and immediately set the yColumn
|
||||
graph->set_xColumn(columnX);
|
||||
graph->set_yColumn(ds->addCopiedColumn(Y, "y"+QString::number(id)));
|
||||
|
||||
// don't use symbols
|
||||
graph->set_symbol(JKQTPnoSymbol);
|
||||
// use one of different pens
|
||||
graph->set_style(pens[id%pens.size()]);
|
||||
// set width of graph line
|
||||
graph->set_lineWidth(1.5);
|
||||
|
||||
// graph title is made from symbol+penstyle
|
||||
graph->set_title(QString("D=\\delta/\\omega_0=%1").arg(D[id]));
|
||||
|
||||
// add the graph to the plot, so it is actually displayed
|
||||
plot.addGraph(graph);
|
||||
}
|
||||
|
||||
// 4. Also we add a text-element in the plot to show the plotted function
|
||||
// This element (JKQTPgeoText) is taken from the set of geometric elements
|
||||
// and is simply parametrized by a position (1.25/10) and the text to display.
|
||||
// In addition you can also set the font size (here to 15)
|
||||
plot.addGraph(new JKQTPgeoText(&plot, 1.25, 10, "\\frac{A}{A_{stat}}=\\frac{1}{\\sqrt{\\left(1-\\eta^2\\right)^2+\\left(2{\\eta}D\\right)^2}}", 15));
|
||||
// for nicer rendering we set the fonts used by the internal LaTeX parser instance to XITS
|
||||
//plot.get_plotter()->get_mathText()->useXITS();
|
||||
|
||||
// 4. set y-axis to logarithmic (x-axis would be analogous)
|
||||
// and set axis labels (using LaTeX notation)
|
||||
plot.get_yAxis()->set_logAxis(true);
|
||||
plot.get_yAxis()->set_axisLabel("Amplitude A/A_{stat}");
|
||||
plot.get_xAxis()->set_axisLabel("relative driving frequency \\eta=\\omega/\\omega_0");
|
||||
|
||||
|
||||
// 5. autoscale the plot so the graph is contained
|
||||
plot.zoomToFit();
|
||||
|
||||
// 6. show plotter and make it a decent size
|
||||
plot.show();
|
||||
plot.resize(600,400);
|
||||
|
||||
return app.exec();
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
# source code for this simple demo
|
||||
SOURCES = jkqtplotter_simpletest_logaxes.cpp
|
||||
|
||||
# configure Qt
|
||||
CONFIG += qt
|
||||
QT += core gui svg
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
|
||||
|
||||
# output executable name
|
||||
TARGET = jkqtplotter_simpletest_logaxes
|
||||
|
||||
# include JKQtPlotter source code
|
||||
include(../../lib/jkqtplotter.pri)
|
||||
include(../../lib/jkqtplotterressources/math_fonts/xits.pri)
|
||||
DEFINES += AUTOLOAD_XITS_FONTS
|
||||
DEFINES += USE_XITS_FONTS
|
||||
|
||||
# here you can activate some debug options
|
||||
#DEFINES += SHOW_JKQTPLOTTER_DEBUG
|
||||
#DEFINES += JKQTBP_AUTOTIMER
|
Loading…
Reference in New Issue
Block a user