Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Apr 23, 2015
1 parent 95416aa commit bd34f33
Show file tree
Hide file tree
Showing 6 changed files with 700 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
examples/readdoubles2

examples/readdoubles1

examples/matplotlib/.dub/build/application-debug-posix.osx-x86_64-dmd_2067-B22D9B4DC44F841A0861C6CE3D44FC8C/atmosphere_gm_charts

examples/matplotlib/dub.selections.json

examples/matplotlib/atmosphere_gm_charts
33 changes: 31 additions & 2 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,37 @@ Phobos is the standard runtime library that comes with the D language compiler.
.. _C Standard library: http://www.cplusplus.com/reference/clibrary/
.. _Phobos: http://dlang.org/phobos/

Plotting
~~~~~~~~
Plotting with matplotlib (python)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There are two projects can be used with the D programming language:

+ Plotcli_ is a command line application written in D that can create plots from text/csv files and from piped data, making it useful during data analysis.
+ PLplot_ is a cross-platform software package written in С for creating scientific plots. It includes low-level D bindings.

But these two are not so convenient to use in comparison with matplotlib.

matplotlib_ is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell, web application servers, and different graphical user interface toolkits. To integrate with python the PyD package can be used.

PyD_ is a library that provides seamless interoperability between the D programming language and Python.
The minimal configuration file for this example is

.. literalinclude:: ../../examples/matplotlib/dub.json
:language: json
:tab-width: 4

.. note:: The python should be `installed <https://www.python.org/downloads/>`_. PyD searches the version of the python that noted in the PyD's sub-configuration (``"pyd": "python34"`` in this example). For more information, see `the PyD's dub configuration file <https://github.com/ariovistus/pyd/blob/master/dub.json>`_.

The following `example <>`_

.. literalinclude:: ../../examples/matplotlib/source/app.d
:language: d
:tab-width: 4

.. _matplotlib: http://matplotlib.org
.. _Plotcli: https://github.com/BlackEdder/plotd
.. _PyD: http://pyd.readthedocs.org
.. _PLplot: http://plplot.sourceforge.net

Web Application
~~~~~~~~~~~~~~~
Expand Down
8 changes: 8 additions & 0 deletions examples/matplotlib/.dub/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dub": {
"cachedUpgrades": {
"pyd": "0.9.7"
},
"lastUpgrade": "2015-04-23T15:06:38.966706"
}
}
2 changes: 1 addition & 1 deletion examples/matplotlib/dub.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "atmosphere_gm_charts",
"name": "plotting_example",
"dependencies": {
"pyd": "~>0.9.4",
},
Expand Down
65 changes: 62 additions & 3 deletions examples/matplotlib/source/app.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
import std.stdio;
import pyd.pyd;
import pyd.embedded;
import pyd.extra;

void main() {
/++
`d_to_python_numpy_ndarray` converts a D array to numpy.ndarray.
`toNumpyArray` is only an alias.
+/
alias toNumpyArray = d_to_python_numpy_ndarray;

/++
A static constructor is a function that performs initializations of
thread local data before the `main()` function gets control for the
main thread.
Shared static constructors are executed before any static
constructors, and are intended for initializing any shared global
data.
+/
shared static this() {
//initializes PyD package.
py_init();
}

void main()
{
auto pythonContext = new InterpContext();
/+
Uniform Function Call Syntax (UFCS)
is used in the following line if code.
}
Equivalent code would be just:
--------
pythonContext.sample = toNumpyArray(readData("view/data.txt"));
--------
+/
pythonContext.sample = "view/data.txt".readData.toNumpyArray;
pythonContext.py_stmts(script);
}

double[] readData(string file)
{
import std.algorithm.iteration : map, splitter;
import std.array : array;
import std.conv : to;
import std.file : readText;

return file
.readText //Reads the contents of a text file into a string.
.splitter //Lazily splits words.
.map!(to!double) //Lazily converts words to doubles.
.array; //Creates an array.
}

immutable script = `
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
num_bins = 50
ax = plt.subplot()
n, bins, patches = ax.hist(sample, num_bins, normed=1, label='sample')
plt.show()
`;

0 comments on commit bd34f33

Please sign in to comment.