2.[The class AxMyFirstAxes](#the-class-axmyfirstaxes)
3.[Linking the class to the module](#linking-the-new-class-to-the-module)
4.[Using the axes](#using-the-axes)
# Concept
**myplotlib** is an interface between you and the great functionality of matplotlib.
As such it naturally inherit a lot of the structures from matplotlib:
alike in matplotlib a plot is composed of four type of objects:
1.**The window** (that contains the buttons, and handle events)
2.**The canvas** (the sheet of paper where you draw the figure)
3.**The figure** (an empty object that contains an array of axes)
4.**The axes** (This is the core of the plot)
We naturally start with the **axes**.
An axes is composed of two **axis**, two **labels**, one **title** and a bunch of **artists**.
- an **axis**: has ticks (minors and majors), ticklabels (the numbers) and limits
- the **labels** and **title** are just text objects
- the **artists** are everything you may draw on the axes: lines, polygones, images, text, surfaces... (see [doc](http://matplotlib.org/users/artists.html#axes-container))
In **myplotlib** we add two intrinsic functionalities to an axes.
The main idea behind **myplotlib** is the independence of the axes.
> *An axes should know how to extract the relevant information it needs and how to represent them on the screen.*
As a results a MyAxes instance (the axes in myplotlib) has two additional functions:
-**formatRawData**: the function that extract the information (tech: produce a self.data object).
-**plotting**: the function that represent these data on the screen (tech: produce artists in the axes container).
If you look at [myplotlib/myAxes.py](https://gitlab.aip.de/yfournier/myplotlib/blob/tools/myAxes.py)
you will remark that these two functions are empty. That is the power of object oriented programming.
`MyAxes` is a container class that fits into a framework, it is compatible with all the functionalities of matplotlib.
So now you have the freedom of specialising this **base class** to any purpose you wish,
ensuring that you can enjoy the full power of matplotlib.
Now we will see how to overwrite the **base class**`MyAxes` with a new class that we will call `AxMyFirstAxes`.
To overwrite a class you just need to overwrite these two empty functions and actually do something.
# The class AxMyFirstAxes
Create `mymodule/myAxes/<name of my class>.py`
> Here we will use mymodule/myAxes/axMyFirstAxes.py
```python
# Import the mother class MyAxes from the local myplotlib module
from..myplotlibimportMyAxes
# Here you can also import any needed module
# ex: from .. import np
# import numpy as np
# Begin of the class
classaxMyFirstAxes(MyAxes):
# This is the function that will format the rawdata so that they can be plotted
defformatRawdata(self,rawdata):
# Extract some part of the data
rawdata1=rawdata.data[<somekey>]
rawdata2=rawdata.data[<somekey>]
# Format the data
if(self.fig.keywords[<somekey>]):
# 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)
# This function is the plotting procedure
defplotting(self):
# You can get the keywords you need from the figure
xrange=self.fig.keywords.get('xrange',None)
yrange=self.fig.keywords.get('yrange',None)
# 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
self.set_xlabel(self.data['xlabel'])
self.set_ylabel(self.data['ylabel'])
ifxrangeisnotNone:
self.set_xlim(xrange)
ifyrangeisnotNone:
self.set_ylim(yrange)
# Returning True or False is not optional
return(True)
```
# Linking the new class to the module
Now that the axes class is created we just need to link it to the module.
To do so you need to add the following line in `mymodule/myAxes/__init__.py`
```python
fromaxMyFirstAxesimportAxMyFirstAxes
```
the module `mymodule` import by default all axes in the sub-module `myAxes`, so you just need
to tell `myAxes` to load the class `AxMyFirstAxes` from the file `axMyFirstAxes`.