Changes
Page history
yfournier created page: create your first figure
authored
Feb 16, 2017
by
Yori Fournier
Show whitespace changes
Inline
Side-by-side
create-your-first-figure.md
View page @
15f7ebc5
...
@@ -18,32 +18,33 @@ alike in matplotlib a plot is composed of four type of objects:
...
@@ -18,32 +18,33 @@ alike in matplotlib a plot is composed of four type of objects:
> **NOTE**: If you have not read yet the page about axes you should start [there](Create your first axes)
> **NOTE**: If you have not read yet the page about axes you should start [there](Create your first axes)
It is always a good idea to design figures, the sub-module mytools of myplotlib offers a few standard figure classes
It is always a good idea to design figures, the sub-module mytools of myplotlib offers a few standard figure classes (FigOneAxes2D, ...)
that are very practical for testing and fast development purposes but at the end you may want to access some more
that are very practical for testing and fast development purposes, but at the end of the day you may want to access some more specific features.
specific features.
As just mentioned a figure is a container for axes. Indeed a figure may contain a few axes. This is practical
A figure is a container for axes, and often it is useful to have several axes on the same figure,
for comparison, completeness, or user specific legends.
either for comparison, completeness, or user specific legends.
Indeed, alike in matplotlib, colorbars and legends are axes.
Indeed in matplotlib colorbars and legends are axes. In myplotlib too.
The difference is that myplotlib provides an additional feature to figures:
**user-specific keywords!**
The difference is that in myplotlib figures have a one additional feature:
**user-specific keywords!**
Keywords are very practical, you can access them from all axes functions, these are the global variables of the figure.
A further advantage they can be passed to the figure while creation and can be modified on the fly with the update
function.
keywords are very practical you can access them from any axes functions. These are the global variables of the figure.
Therefore
`MyFig`
(the figure
*base class*
in myplotlib) has two functions that can be overwritten:
one more advantage of keywords they can, passed to the figure while creating it and can be modified on the fly with
-
*declareKeywords*
: where you declare the keywords you need and there default values. (tech: create a fig.keywords dict)
the function update.
-
*addAxes*
: the function where you tell the figure which axes to use and where to put them. (tech: uses fig.add_axes())
# The class FigMyFirstFigure
# The class AxMyFirstAxes
Create
`mymodule/myFigures/<name of my class>.py`
Create
`mymodule/myAxes/<name of my class>.py`
> Here we will use mymodule/myFigures/figMyFirstFigure.py
> Here we will use mymodule/myAxes/axMyFirstAxes.py
```
python
```
python
# Import the mother class My
Axes
from the local myplotlib module
# Import the mother class My
Fig
from the local myplotlib module
from
..myplotlib
import
My
Axes
from
..myplotlib
import
My
Fig
# Here you can also import any needed module
# Here you can also import any needed module
# ex: from .. import np
# ex: from .. import np
...
@@ -51,54 +52,34 @@ from ..myplotlib import MyAxes
...
@@ -51,54 +52,34 @@ from ..myplotlib import MyAxes
# Begin of the class
# Begin of the class
class
axMyFirstAxes
(
MyAxes
):
class
figMyFirstFigure
(
MyFig
):
# This is the function that will format the rawdata so that they can be plotted
def
formatRawdata
(
self
,
rawdata
):
# Extract some part of the data
rawdata1
=
rawdata
.
data
[
<
some
key
>
]
rawdata2
=
rawdata
.
data
[
<
some
key
>
]
# Format the data
if
(
self
.
fig
.
keywords
[
<
some
key
>
]):
# format in a certain way
else
:
# format in another way
# Put everything you need into the data of the axes
self
.
data
=
{
'
xdata
'
:
xdata
,
'
ydata
'
:
ydata
,
'
xlabel
'
:
xlabel
,
'
ylabel
'
:
ylabel
}
# Returning True or False is not optional
return
(
True
)
# The function where you declare the user-defined keywords
def
declareKeywords
(
self
):
# Th
is function is the plotting procedure
# Th
e size of the figure can be also defined here (optional)
def
plotting
(
self
):
self
.
FIGSIZE
=
(
8.
,
6.
)
# the size are in inches. notes that a column in A&A is 8 inches.
# You can get the keywords you need from the figure
# the name of the dict is not optional.
xrange
=
self
.
fig
.
keywords
.
get
(
'
xrange
'
,
None
)
self
.
keywords
=
{
'
<name of the keyword>
'
:
<
default
value
>
,
yrange
=
self
.
fig
.
keywords
.
get
(
'
yrange
'
,
None
)
'
xunit
'
:
'
years
'
,
'
withAnotation
'
:
False
,
'
twoColumn
'
:
False
,
...}
# All matplotlib functions are available (see http://matplotlib.org/api/axes_api.html)
self
.
plot
(
self
.
data
[
'
xdata
'
],
self
.
data
[
'
ydata
'
])
# Any decoration you need
# The function where you tell the figure where tu plot what
self
.
set_xlabel
(
self
.
data
[
'
xlabel
'
])
def
addAxes
(
self
):
self
.
set_ylabel
(
self
.
data
[
'
ylabel
'
])
if
xrange
is
not
None
:
# the aspect ratio of the axes and its position in the figure
self
.
set_xlim
(
xrange
)
ratioAxes1
=
6.
/
8.
frameAxes1
=
[
0
,
0
,
0.5
,
1.0
]
# [x0, y0, width, height] in unit of figwidth and figheight
if
yrange
is
not
None
:
ratioAxes2
=
6.
/
8.
self
.
set_ylim
(
yrange
)
frameAxes2
=
[
0.5
,
0.0
,
0.5
,
1.0
]
# Returning True or False is not optional
self
.
add_axes
(
AxMyFirstAxes
(
self
,
rationAxes1
,
frameAxes1
))
return
(
True
)
self
.
add_axes
(
AxMyFirstAxes
(
self
,
rationAxes2
,
frameAxes2
)
)
```
```
# Linking the new class to the module
# Linking the new class to the module
...
...
...
...