Verified Commit 7140f821 authored by Toby Hodges's avatar Toby Hodges
Browse files

add note about using matplotlib as backend

parent 92e0f429
......@@ -518,12 +518,6 @@ x = np.linspace(0, 2, 4)
> >
> > plt.plot(x, np.sin(x), label='sin')
> > plt.plot(x, np.sin(x**3), label='sin-cubic')
> >
> > # And the other elements of the plot that we need to repeat
> > plt.xlabel('x label')
> > plt.ylabel('y label')
> > plt.title("Simple Plot")
> > plt.legend()
> > ~~~
> > {: .language-python }
> >
......@@ -535,79 +529,6 @@ x = np.linspace(0, 2, 4)
> {: .solution }
{: .challenge }
## Point plots and other variants
The `plot()` function is highly versatile by allowing us to modify the type of line drawn,
include point markers, use different colors, line thickness, and many other options covered in
the [`plot()` documentation][matplotlib-plot].
Modifying markers, lines and color is such a common task that this function
provides a convenient shorthand notation to specify all three options in one go.
You can supply a string with the `"[marker][line][color]"` notation as the last argument.
For example, `"o-b"` encodes a `circle (o)` marker, a continuous `line (-)` and both in blue `color (b)`.
Each component is optional so providing only a line style (`"-"`) or a marker and a color (`"ob"`)
is perfectly valid.
As markers and lines are aspects of a plot that are common to other plotting functions,
they also have dedicated pages in `matplotlib`'s documentation.
You can visit the [gallery of markers][matplotlib-markers], [the equivalent page for line styles][matplotlib-linestyle]
and the [gallery of colors][matplotlib-colors], to which you can refer by [name][matplotlib-color-names], [RGB or hexadecimal code][matplotlib-color-codes].
Lets now try to customize our polynomial plot from before:
~~~
x = np.linspace(0, 2, 10) # We reduce the number of samples for visual clarity
plt.plot(x, x, "o-b", label='linear') # full circles and continuous line in blue
plt.plot(x, x**2, "v--r", label='quadratic') # down pointing triangles a dashed line in red
plt.plot(x, x**3, "*-.g", label='cubic') # starts with dot dashed line in green
plt.plot(x, x**4, ".m", label='quadric') # points without a connecting line in magenta
~~~
{: .language-python }
![custom marker line color plot](../fig/my-custom-marker-line-color.png)
Although the aesthetic aspect could still benefit from additional changes,
we can see how we can conveniently modify the style of the plot.
Notice also that in the `quadric` case, omitting the `line` part of the formatting
style, disables drawing a connecting line.
> ## Point and scatter
>
> `matplotlib` includes also a [`scatter()` function][matplotlib-scatter]
> that provides additional features over simple point plots.
> Using the [`scatter()` documentation][matplotlib-scatter] and the scatter plot examples
> in the [`scatter()` gallery section][matplotlib-scatter-example],
> create a variant of the point plot above with points of increasing size.
>
> **Hint**: you will need to use the `s=` attribute of the `scatter()` function.
> See the [`scatter()` documentation][matplotlib-scatter] for additional options.
> You may also need to multiply all values by a constant if the difference in sizes is too small.
>
> > ## Solution
> >
> > Since `scatter()` doesn't draw lines, we cannot use the `[marker][line][color]` notation,
> > but we can still specify the `marker=` style.
> >
> > A possible solution is:
> > ~~~
> > x = np.linspace(0, 2, 20)
> > scale_factor = 10
> >
> > plt.scatter(x, x, s=scale_factor * x, marker="o", label='linear')
> > plt.scatter(x, x**2, s=scale_factor * x**2, marker="v", label='quadratic')
> > plt.scatter(x, x**3, s=scale_factor * x**3, marker="*", label='cubic')
> >
> > # And the other elements of the plot that we need to repeat
> > plt.xlabel('x label')
> > plt.ylabel('y label')
> > plt.title("Simple Plot")
> > plt.legend()
> > ~~~
> > {: .language-python }
> > ![custom scatter marker line color plot](../fig/my-scatter-marker-line-color.png)
> {: .solution }
{: .challenge }
### Histograms
......@@ -684,6 +605,7 @@ https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Basics.html
As we saw at the end of the [Working with Data](../02-data/) section,
it is possible to plot data in a pandas `DataFrame` or `Series`
directly from the object itself.
By default, pandas uses Matplotlib to create these plots.
To demonstrate this, we will borrow a dataset
prepared by [Software Carpentry][swc-gapminder-data]
......@@ -846,7 +768,7 @@ plot style, layout, etc.
~~~
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, figsize=(16,8))
fig, ax = plt.subplots(2, 2, figsize=(16,8)) # create a 2x2 grid of subplots
gapminder[gapminder['continent']=='Asia']['pop_1997'].plot(kind='bar', ax=ax[1,0])
gapminder[gapminder['continent']=='Asia']['gdpPercap_1997'].plot(kind='bar', ax=ax[0,0])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment