-
-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 992 plot arrays #1008
Merged
Merged
Issue 992 plot arrays #1008
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4b41c91
#992 add simple 1D and 2D plots
rtimms a410ceb
#992 add tests
rtimms 422a101
#992 fix docs
rtimms 698da0c
#992 reorganise plots
rtimms 7f7273e
#992 update docs
rtimms 449e6f5
Merge branch 'develop' into issue-992-plot-arrays
rtimms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Array | ||
===== | ||
|
||
.. autoclass:: pybamm.Array | ||
:members: | ||
|
||
.. autofunction:: pybamm.linspace | ||
|
||
.. autofunction:: pybamm.meshgrid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ Expression Tree | |
variable | ||
independent_variable | ||
scalar | ||
array | ||
matrix | ||
vector | ||
state_vector | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Dynamic Plot | ||
============ | ||
|
||
.. autofunction:: pybamm.dynamic_plot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Plotting | ||
======== | ||
|
||
.. toctree:: | ||
|
||
quick_plot | ||
dynamic_plot | ||
plot | ||
plot_2D |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Plot | ||
==== | ||
|
||
.. autofunction:: pybamm.plot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Plot 2D | ||
======= | ||
|
||
.. autofunction:: pybamm.plot2D |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# | ||
# Method for creating a dynamic plot | ||
# | ||
import pybamm | ||
|
||
|
||
def dynamic_plot(*args, **kwargs): | ||
""" | ||
Creates a :class:`pybamm.QuickPlot` object (with arguments 'args' and keyword | ||
arguments 'kwargs') and then calls :meth:`pybamm.QuickPlot.dynamic_plot`. | ||
The key-word argument 'testing' is passed to the 'dynamic_plot' method, not the | ||
`QuickPlot` class. | ||
|
||
Returns | ||
------- | ||
plot : :class:`pybamm.QuickPlot` | ||
The 'QuickPlot' object that was created | ||
""" | ||
kwargs_for_class = {k: v for k, v in kwargs.items() if k != "testing"} | ||
plot = pybamm.QuickPlot(*args, **kwargs_for_class) | ||
plot.dynamic_plot(kwargs.get("testing", False)) | ||
return plot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# | ||
# Method for creating a 1D plot of pybamm arrays | ||
# | ||
import pybamm | ||
from .quick_plot import ax_min, ax_max | ||
|
||
|
||
def plot(x, y, xlabel=None, ylabel=None, title=None, testing=False, **kwargs): | ||
""" | ||
Generate a simple 1D plot. Calls `matplotlib.pyplot.plot` with keyword | ||
arguments 'kwargs'. For a list of 'kwargs' see the | ||
`matplotlib plot documentation <https://tinyurl.com/ycblw9bx>`_ | ||
|
||
Parameters | ||
---------- | ||
x : :class:`pybamm.Array` | ||
The array to plot on the x axis | ||
y : :class:`pybamm.Array` | ||
The array to plot on the y axis | ||
xlabel : str, optional | ||
The label for the x axis | ||
ylabel : str, optional | ||
The label for the y axis | ||
testing : bool, optional | ||
Whether to actually make the plot (turned off for unit tests) | ||
|
||
""" | ||
import matplotlib.pyplot as plt | ||
|
||
if not isinstance(x, pybamm.Array): | ||
raise TypeError("x must be 'pybamm.Array'") | ||
if not isinstance(y, pybamm.Array): | ||
raise TypeError("y must be 'pybamm.Array'") | ||
|
||
plt.plot(x.entries, y.entries, **kwargs) | ||
plt.ylim([ax_min(y.entries), ax_max(y.entries)]) | ||
plt.xlabel(xlabel) | ||
plt.ylabel(ylabel) | ||
plt.title(title) | ||
|
||
if not testing: # pragma: no cover | ||
plt.show() | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# | ||
# Method for creating a filled contour plot of pybamm arrays | ||
# | ||
import pybamm | ||
from .quick_plot import ax_min, ax_max | ||
|
||
|
||
def plot2D(x, y, z, xlabel=None, ylabel=None, title=None, testing=False, **kwargs): | ||
""" | ||
Generate a simple 2D plot. Calls `matplotlib.pyplot.contourf` with keyword | ||
arguments 'kwargs'. For a list of 'kwargs' see the | ||
`matplotlib contourf documentation <https://tinyurl.com/y8mnadtn>`_ | ||
|
||
Parameters | ||
---------- | ||
x : :class:`pybamm.Array` | ||
The array to plot on the x axis. Can be of shape (M, N) or (N, 1) | ||
y : :class:`pybamm.Array` | ||
The array to plot on the y axis. Can be of shape (M, N) or (M, 1) | ||
z : :class:`pybamm.Array` | ||
The array to plot on the z axis. Is of shape (M, N) | ||
xlabel : str, optional | ||
The label for the x axis | ||
ylabel : str, optional | ||
The label for the y axis | ||
title : str, optional | ||
The title for the plot | ||
testing : bool, optional | ||
Whether to actually make the plot (turned off for unit tests) | ||
|
||
""" | ||
import matplotlib.pyplot as plt | ||
|
||
if not isinstance(x, pybamm.Array): | ||
raise TypeError("x must be 'pybamm.Array'") | ||
if not isinstance(y, pybamm.Array): | ||
raise TypeError("y must be 'pybamm.Array'") | ||
if not isinstance(z, pybamm.Array): | ||
raise TypeError("z must be 'pybamm.Array'") | ||
|
||
# Get correct entries of x and y depending on shape | ||
if x.shape == y.shape == z.shape: | ||
x_entries = x.entries | ||
y_entries = y.entries | ||
else: | ||
x_entries = x.entries[:, 0] | ||
y_entries = y.entries[:, 0] | ||
|
||
plt.contourf( | ||
x_entries, | ||
y_entries, | ||
z.entries, | ||
vmin=ax_min(z.entries), | ||
vmax=ax_max(z.entries), | ||
cmap="coolwarm", | ||
**kwargs | ||
) | ||
plt.xlabel(xlabel) | ||
plt.ylabel(ylabel) | ||
plt.title(title) | ||
plt.colorbar() | ||
|
||
if not testing: # pragma: no cover | ||
plt.show() | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# Tests for the Array class | ||
# | ||
import pybamm | ||
import numpy as np | ||
|
||
import unittest | ||
|
||
|
||
class TestArray(unittest.TestCase): | ||
def test_name(self): | ||
arr = pybamm.Array(np.array([1, 2, 3])) | ||
self.assertEqual(arr.name, "Array of shape (3, 1)") | ||
|
||
def test_linspace(self): | ||
x = np.linspace(0, 1, 100)[:, np.newaxis] | ||
y = pybamm.linspace(0, 1, 100) | ||
np.testing.assert_array_equal(x, y.entries) | ||
|
||
def test_meshgrid(self): | ||
a = np.linspace(0, 5) | ||
b = np.linspace(0, 3) | ||
A, B = np.meshgrid(a, b) | ||
c = pybamm.linspace(0, 5) | ||
d = pybamm.linspace(0, 3) | ||
C, D = pybamm.meshgrid(c, d) | ||
np.testing.assert_array_equal(A, C.entries) | ||
np.testing.assert_array_equal(B, D.entries) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Add -v for more debug output") | ||
import sys | ||
|
||
if "-v" in sys.argv: | ||
debug = True | ||
pybamm.settings.debug_mode = True | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pybamm | ||
import unittest | ||
import numpy as np | ||
|
||
|
||
class TestPlot(unittest.TestCase): | ||
def test_plot(self): | ||
x = pybamm.Array(np.array([0, 3, 10])) | ||
y = pybamm.Array(np.array([6, 16, 78])) | ||
pybamm.plot(x, y, xlabel="x", ylabel="y", title="title", testing=True) | ||
|
||
def test_plot_fail(self): | ||
x = pybamm.Array(np.array([0])) | ||
with self.assertRaisesRegex(TypeError, "x must be 'pybamm.Array'"): | ||
pybamm.plot("bad", x) | ||
with self.assertRaisesRegex(TypeError, "y must be 'pybamm.Array'"): | ||
pybamm.plot(x, "bad") | ||
|
||
def test_plot2D(self): | ||
x = pybamm.Array(np.array([0, 3, 10])) | ||
y = pybamm.Array(np.array([6, 16, 78])) | ||
X, Y = pybamm.meshgrid(x, y) | ||
|
||
# plot with array directly | ||
pybamm.plot2D(x, y, Y, xlabel="x", ylabel="y", title="title", testing=True) | ||
|
||
# plot with meshgrid | ||
pybamm.plot2D(X, Y, Y, xlabel="x", ylabel="y", title="title", testing=True) | ||
|
||
def test_plot2D_fail(self): | ||
x = pybamm.Array(np.array([0])) | ||
with self.assertRaisesRegex(TypeError, "x must be 'pybamm.Array'"): | ||
pybamm.plot2D("bad", x, x) | ||
with self.assertRaisesRegex(TypeError, "y must be 'pybamm.Array'"): | ||
pybamm.plot2D(x, "bad", x) | ||
with self.assertRaisesRegex(TypeError, "z must be 'pybamm.Array'"): | ||
pybamm.plot2D(x, x, "bad") | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Add -v for more debug output") | ||
import sys | ||
|
||
if "-v" in sys.argv: | ||
debug = True | ||
pybamm.settings.debug_mode = True | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could
plot2D
automatically do meshgrid(x,y) if they aren't given in meshgrid form?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
turns out contourf handles this provided they are the correct shape. have added some lines to account for this