Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bio-IT Workshops
Intermediate Python
Commits
fdea41c2
Verified
Commit
fdea41c2
authored
Jul 20, 2020
by
Renato Alves
🌱
Browse files
Add line plot and warning about too few/many points
parent
c2c0db02
Changes
2
Hide whitespace changes
Inline
Side-by-side
_episodes/03-plotting.md
View file @
fdea41c2
...
...
@@ -19,8 +19,8 @@ keypoints:
# Plotting in Python
The words
*plotting*
and
*plot*
have their origin in
[
plotters
](
https://en.wikipedia.org/wiki/Plotter
)
,
devices that use pens to replicate the human act of drawing
and are able to produce
high quality results.
devices that use pens to replicate the human act of drawing
.
Plotters became popular thanks to their ability to produce
high quality results.
The Python community has developed several frameworks to generate plots,
also known as charts or graphs.
...
...
@@ -415,7 +415,7 @@ fig.savefig("fig/subplot.png")
And we got a great looking result.
If instead of independent panels, you are plotting facets or dependent variables,
you should additionally
,
specify that the subplots should have
you should additionally specify that the subplots should have
the same minimum and maximum limits for both X and Y axis.
This can either be done manually by iterating over each
`Axes`
...
...
@@ -445,23 +445,26 @@ Notice that sharing both axis automatically hid the axis on the inner subplots.
{: .callout }
### Line plots
Line plots are one of the most common kinds of plot you can create
with
`matplotlib`
.
As we saw above, you can create a line plot by providing a set of X and Y coordinates.
Consecutive points will be The order of the points will dictate how lines
in a specific order.
The order of the points will dictate how lines will be drawn.
Consecutive points will be connected with a line.
Picking on our example from before:
~~~
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 100) # Generate an array of 100 values between 0 and 2
start = 0
stop = 2
samples = 100
x = np.linspace(start, stop, samples) # Generate an array of 100 values between 0 and 2
plt.plot(x, x, label='linear') # A Figure and Axes are implicitly created here
plt.plot(x, x**2, label='quadratic') # subsequent calls are added to the same Axes
...
...
@@ -474,9 +477,59 @@ plt.savefig("my-simple-plot.png") # This is the same as before
~~~
{: .language-python }
which produces the exact same output as before.

The
`numpy`
function
`linspace()`
creates an array with values
`[0, 0.02, 0.04, ...]`
. Due to the small increment the plot looks like a smooth curve.
The
`plot()`
function takes
`X`
values as its first attribute and
`Y`
values for the second.
Given
`plot(X, Y)`
it then takes a pair of coordinates from both variables as with:
`(X[0], Y[0]), (X[1], Y[1]), ...`
.
> ## Beware of too many or too few data points
>
> Keep in mind that `matplotlib` will try to plot all the data you pass as arguments.
> If your provide thousands of data points, you may not see a significant visual change
> but your plotting code will take considerably longer to produce a result.
> Similarly, if you don't provide enough points, the linear interpolation produced by
> `matplotlib` may introduce misleading visual effects.
{: .callout }
> ## Nice and smooth
>
> Modify the values in `start`, `stop` and `samples`,
> to produce alternative versions of the above plot with different degrees of *smoothness*.
> Play also with different mathematical expressions other than `x ** 2` or `x ** 3`.
>
> `numpy`'s [documentation][numpy-docs] has a [nice list of mathematical functions][numpy-math-functions].
> For example, the `sin()` function is available as `np.sin()`.
>
> > ## Solution
> >
> > Using only `4` samples the curve doesn't look as smooth as before.
x = np.linspace(0, 2, 4)
> > 
> >
> > And a sine plot would look like:
> >
> > ~~~
> > start = 0
> > stop = 5
> > samples = 100
> >
> > x = np.linspace(start, stop, samples)
> >
> > plt.plot(x, np.sin(x), label='sin')
> > plt.plot(x, np.sin(x**3), label='sin-cubic')
> > ~~~
> > {: .language-python }
> >
> > 
> >
> > Notice also that as the values of `X` become larger,
> > the number of samples is not enough
> > and visual artifacts start to appear in. the `sine-cubic` plot.
> {: .solution }
{: .challenge }
### Histograms
~~~
...
...
_includes/links.md
View file @
fdea41c2
...
...
@@ -92,7 +92,8 @@
[
mypy
]:
http://mypy-lang.org/
[
np-home
]:
https://numpy.org/
[
numfocus
]:
https://numfocus.org/
[
numpy-docs
]:
https://numpy.org/doc/stable/
[
numpy-docs
]:
https://numpy.org/doc/stable/
[
numpy-math-functions
]:
https://numpy.org/doc/stable/reference/routines.math.html
[
old-formatting
]:
https://docs.python.org/2/library/stdtypes.html#string-formatting
[
os-file-dir
]:
https://docs.python.org/3.8/library/os.html#os-file-dir
[
os-module
]:
https://docs.python.org/3/library/os.html
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment