Skip to content

Commit ff5211b

Browse files
authored
Merge pull request #165 from vinisalazar/ep13
Improving Episode 13. Thank you @vinisalazar for your great contributions as usual.
2 parents 43ddc1f + a528afd commit ff5211b

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

_episodes/13-matplotlib.md

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ it is fully controllable down to basic elements and includes a module `pylab` th
2424
(designed to feel like MATLAB plotting, if you happen to have done that before).
2525

2626
The Matplotlib library can be imported using any of the import techniques we have seen. As Pandas is generally imported
27-
with `import Pandas as pd`, you will find that `matplotlib` is most commonly imported with `import matplotlib as plt` where 'plt' is the alias.
27+
with `import pandas as pd`, you will find that Matplotlib is most commonly imported with `import matplotlib as plt` where 'plt' is the alias.
2828

2929
In addition to importing the library, in a Jupyter notebook environment we need to tell Jupyter that when we produce a
3030
graph, we want it to be display the graph in a cell in the notebook just like any other results. To do this we use the `%matplotlib inline` directive.
@@ -35,16 +35,10 @@ and advanced plot types. One of its most useful features is formatting.
3535

3636
## Plotting with Pandas
3737

38-
The `pandas` library contains very tight integration with `matplotlib`. There are functions in `pandas` that
39-
automatically call `matplotlib` functions to produce graphs.
38+
The Pandas library contains very tight integration with Matplotlib. There are functions in Pandas that
39+
automatically call Matplotlib functions to produce graphs.
4040

41-
Other graphical libraries available from within Python are for example `plotnine` (a ggplot2 realisation for python)
42-
and `seaborn`. [Seaborn](https://seaborn.pydata.org) has some very powerful features and advanced plot types.
43-
One of its most useful features is formatting.
44-
45-
## Plotting with Pandas
46-
47-
To plot with `pandas` we have to import it as we have done in past episodes.
41+
To plot with Pandas we have to import it as we have done in past episodes.
4842
To tell Jupyter that when we produce a graph we want it to be displayed in a cell in the notebook just like any other results,
4943
we use the `%matplotlib inline` directive. Without that we need to do a `show()` command.
5044

@@ -56,8 +50,12 @@ import pandas as pd
5650

5751
We also need data to work with loaded into a DataFrame and it's helpful to look at a few rows to remember what's there.
5852

53+
We are going to use the dataset from the setup page, `SAFI_full_shortname.csv`. For the data to load, __make sure to
54+
have that file in the same folder where your Jupyter notebook is running.__ If the file is not in that folder, you
55+
are going to have to type the full path.
56+
5957
~~~
60-
df = pd.read_csv("data/SAFI_full_shortname.csv")
58+
df = pd.read_csv("SAFI_full_shortname.csv")
6159
df.head()
6260
~~~
6361
{: .language-python}
@@ -166,7 +164,10 @@ sns.boxplot(data=df,x ='village',y='buildings_in_compound')
166164
167165
![png](../fig/boxplot1.png)
168166
169-
We can make it look prettier with `seaborn`, much more easily than fixing components manually with `matplotlib`. [`Seaborn`](https://seaborn.pydata.org) is a Python data visualization library based on `matplotlib`. It provides a high-level interface for drawing attractive and informative statistical graphics. `Seaborn` comes with Anaconda; to make it available in our python session we need to import it.
167+
We can make it look prettier with Seaborn, much more easily than fixing components manually with Matplotlib.
168+
[Seaborn](https://seaborn.pydata.org) is a Python data visualization library based on Matplotlib.
169+
It provides a high-level interface for drawing attractive and informative statistical graphics.
170+
Seaborn comes with Anaconda; to make it available in our Python session we need to import it.
170171
171172
~~~
172173
import seaborn as sns
@@ -205,10 +206,17 @@ not through Pandas. First we need to import it.
205206
206207
## Customising our plots with Matplotlib
207208
208-
We can further customise our plots with `matplotlib` directly. First we need to import it.
209-
The `matplotlib` library can be imported using any of the import techniques we have seen. As `pandas` is generally imported with `import pandas as pd`, you will find that `matplotlib` is most commonly imported with `import matplotlib.pylab as plt` where `plt` is the alias.
209+
We can further customise our plots with Matplotlib directly. First we need to import it.
210+
The Matplotlib library can be imported using any of the import techniques we have seen.
211+
As Pandas is generally imported with `import pandas as pd`, you will find that `matplotlib` is most commonly imported
212+
with `import matplotlib.pyplot as plt` where `plt` is the alias.
213+
For demonstration purposes, we are going to use randomly generated data, using the NumPy library (aliased here as `np`).
210214
211215
~~~
216+
import numpy as np
217+
import pandas as pd
218+
import matplotlib.pyplot as plt
219+
212220
# Generate some date for 2 sets of points.
213221
x1 = pd.Series(np.random.rand(20) - 0.5)
214222
y1 = pd.Series(np.random.rand(20) - 0.5)
@@ -346,22 +354,6 @@ demonstrate some of the available features.
346354
>
347355
{: .challenge}
348356
349-
## Saving a graph
350-
351-
If you wish to save your graph as an image you can do so using the `savefig()` function. The image can be saved as a pdf, jpg or png file by changing the file extension.
352-
353-
~~~
354-
df = pd.DataFrame(np.random.normal(size=(100,5)), columns=list('ABCDE'))
355-
df.plot(kind = 'box', return_type='axes')
356-
357-
plt.title('Box Plot')
358-
plt.xlabel('xlabel')
359-
plt.ylabel('ylabel')
360-
361-
plt.savefig('boxplot_from_df.pdf')
362-
~~~
363-
{: .language-python}
364-
365357
[matplotlib-web]: http://matplotlib.org/
366358
[pandas-web]: http://pandas.pydata.org/
367359
[ggplot2-web]: http://ggplot2.tidyverse.org/

0 commit comments

Comments
 (0)