Skip to content

Commit

Permalink
Fixed Plot4d.plot canvas updater
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Aug 16, 2017
1 parent da1b46f commit d093cd0
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file.
- duplicate primes matrix in maths module
- duplicate code in maths prime checking
- duplicate code in ml time series
### Fixed
- matplotlib dependencies
- Plot4d.plot cavas updater

## 4.6.1
### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[![Code Health](https://landscape.io/github/sirfoga/pyhal/master/landscape.svg?style=flat)](https://landscape.io/github/sirfoga/hal/master)
[![Code Climate](https://lima.codeclimate.com/github/sirfoga/pyhal/badges/gpa.svg)](https://codeclimate.com/github/sirfoga/pyhal)
![pylint Score](https://mperlet.de/pybadge/badges/8.78.svg)
![pylint Score](https://mperlet.de/pybadge/badges/8.74.svg)

[![PyPI version](https://badge.fury.io/py/PyHal.svg)](https://pypi.org/project/PyHal/) [![Documentation Status](https://readthedocs.org/projects/pyhal/badge/?version=latest)](http://pyhal.readthedocs.io/en/latest/?badge=latest)

Expand Down
56 changes: 56 additions & 0 deletions docs/examples/CHARTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Charts

## Plot3d.param()
```python
import numpy as np
from hal.charts.plotter import Plot3d

Plot3d().param(
np.sin, np.cos, np.exp, # x, y, z axis function
0, 10, 100 # from 0 to 10 using 100 points
)
```
![Plot3d](../images/charts/Plot3d_param.png)


## Plot3d.plot()
```python
import numpy as np
from hal.charts.plotter import Plot3d

def f(x, y): # sample function
return np.sqrt(np.power(x, 2) + np.power(y, 2))

Plot3d().plot(
f,
-1, 1, 20, # min, max and number of points of x axis
-1, 1, 20 # min, max and number of y axis
)
```
![Plot3d](../images/charts/Plot3d_plot.png)


## Plot4d.plot()
```python
import numpy as np
from hal.charts.plotter import Plot4d

def f(x, y, z):
return (np.sin(x) + np.cos(y)) / (np.power(z, 2))

Plot4d().plot(
f,
-10, 10, # min and max of x axis
-10, 10, # min and max of y axis
-10, 10, # min and max of z axis
precision=0.1, kind="slice"
)
```
![Plot3d](../images/charts/Plot4d_plot_0.png)
![Plot3d](../images/charts/Plot4d_plot_1.png)
![Plot3d](../images/charts/Plot4d_plot_2.png)
![Plot3d](../images/charts/Plot4d_plot_3.png)


## Questions and issues
The [github issue tracker](https://github.com/sirfoga/pyhal/issues) is **only** for bug reports and feature requests. Anything else, such as questions for help in using the library, should be mailed [here](mailto:sirfoga@protonmail.com).
Binary file added docs/images/charts/Plot3d_param.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/charts/Plot3d_plot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/charts/Plot4d_plot_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/charts/Plot4d_plot_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/charts/Plot4d_plot_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/charts/Plot4d_plot_3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 34 additions & 24 deletions hal/charts/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def scatter(vector_x, vector_y, vector_z):
if len(vector_x) == len(vector_y) == len(vector_z):
# general settings
fig = plt.figure()
chart = fig.add_subplot(111, projection="3d")
chart = fig.add_subplot(111, projection='3d')

# plot
chart.scatter(vector_x, vector_y, vector_z, c="r", marker="o")
Expand Down Expand Up @@ -201,7 +201,7 @@ def scatter(vector_x, vector_y, vector_z, vector_w):
raise ValueError("Cannot plot vectors of different length.")

def plot(self, function, min_x, max_x, min_y, max_y, min_z, max_z,
precision, kind):
precision=0.5, kind="contour"):
"""
:param function: function to plot
:param min_x: minimum of x-values
Expand Down Expand Up @@ -278,69 +278,79 @@ def get_precision_delta(min_val, max_val):
return float(max_val - min_val) / float(10 * precision)

if kind == "slice":
chart_axis = plt.axes(projection="3d") # general settings
chart = plt.axes(projection="3d") # general settings
points_x = get_precision(min_x, max_x)
points_y = get_precision(min_y, max_z)

x_ax = numpy.outer(linspace(min_x, max_x, points_x), points_x)
y_ax = numpy.outer(
x_axis = numpy.outer(linspace(min_x, max_x, points_x), points_x)
y_axis = numpy.outer(
linspace(min_y, max_y, points_y).flatten(), points_y
).T
# slider
axis_slider = plt.axes([0.12, 0.03, 0.78, 0.03], axisbg="white")
slider = Slider(axis_slider, "x", min_x, max_x, valinit=min_x)

def update():
def update(val):
"""
:return: re-plot
"""

chart_axis.clear()
chart.clear()
x_const = slider.val
z_axis = function(x_const, x_ax, y_ax)
chart_axis.plot_surface(
x_ax, y_ax, z_axis, alpha=0.3, linewidth=2.0
z_axis = function(x_const, x_axis, y_axis)
chart.plot_surface(
x_axis, y_axis, z_axis, alpha=0.3, linewidth=2.0
)
set_labels(chart_axis, "y", "z", "w")
set_labels(chart, "y", "z", "w")

slider.on_changed(update)
set_labels(chart_axis, "y", "z", "w")
set_labels(chart, "y", "z", "w")
# plot
plt.show()
else: # kind = contour
# general settings
fig = plt.figure()
chart_axis = fig.gca(projection="3d")
chart = fig.gca(projection="3d")

# create axes
x_ax = numpy.arange(min_x, max_x, get_precision_delta(
min_x, max_x, precision)).tolist()
y_ax = numpy.arange(min_y, max_y, get_precision_delta(
min_y, max_y, precision)).tolist()
x_ax, y_ax = numpy.meshgrid(x_ax, y_ax)
x_axis = numpy.arange(min_x, max_x, get_precision_delta(
min_x, max_x)).tolist()
y_axis = numpy.arange(min_y, max_y, get_precision_delta(
min_y, max_y)).tolist()
x_axis, y_axis = numpy.meshgrid(x_axis, y_axis)

# slider
axis_slider = plt.axes([0.12, 0.03, 0.78, 0.03], axisbg="white")
slider = Slider(axis_slider, "x", min_x, max_x, valinit=min_x)

# update

def update():
def update(val):
"""
:return: re-plot plot
"""

chart_axis.clear() # re-plot
chart.clear() # re-plot
x_const = slider.val
z_axis = []

# add new points
for i, _ in enumerate(x_ax):
z_axis.append(function(x_const, x_ax[i], y_ax[i]))
for i, _ in enumerate(x_axis):
z_axis.append(function(x_const, x_axis[i], y_axis[i]))

# show
set_labels(chart_axis, "y", "z", "w")
chart.contour(
x_axis, y_axis, z_axis, zdir="x", offset=min_x
)
chart.contour(
x_axis, y_axis, z_axis, zdir="y", offset=min_y
)
chart.contour(
x_axis, y_axis, z_axis, zdir="z", offset=min_z
)
chart.contour(x_axis, y_axis, z_axis, extend3d=True)
set_labels(chart, "y", "z", "w")

slider.on_changed(update)
set_limits(chart_axis)
set_limits(chart)
plt.show()

0 comments on commit d093cd0

Please sign in to comment.