Horizontal Rule In Jupyter Notebook

If I entered multiple values in notebook, it returns only last one:Is there any way to see all values? Ik heb geprobeerd een csv-bestand te openen in jupyter notebook, maar het toont een foutmelding. En ik begreep de foutmelding niet. CSV-bestand en jupyter-notebookbestand bevinden zich in dezelfde.

Matplotlib is a Python package for 2D plotting and the matplotlib.pyplot sub-module contains many plotting functions to create various kinds of plots. Let's get started by importing matplotlib.pyplot and using %matplotlibJupyter magic to display plots in the notebook.

Basic Plotting

Procedure The general procedure to create a 2D line plot is:

1. Create a sequence of \$x\$ values.
2. Create a sequence of \$y\$ values.
3. Enter plt.plot(x,y,[fmt],**kwargs) where [fmt] is a (optional) format string and **kwargs are (optional) keyword arguments specifying line properties of the plot.
4. Use pyplot functions to add features to the figure such as a title, legend, grid lines, etc.
5. Enter plt.show() to display the resulting figure.

Let's begin with a basic example with a few random points:

The main things to notice are:

1. The sequences x and y define the coordinates of the points in the plot.
2. The line in the plot is constructed by connecting the points by straight lines.

The second observation implies that if we want to plot a smooth curve then we need to plot lots of points otherwise the plot will not be smooth. For example, we could try plotting the parabola \$y = x^2\$ for \$x in [-2,2]\$ using only 5 points:

This is too few points to plot a smooth curve such as \$y = x^2\$ and so we need more points! Let's try again using the NumPy function np.linspace to create 100 points!

That's a better representation of the parabola \$y = x^2\$. Note that the number of points we use in a line plot (100 in this case) is completely arbitrary but the goal is to show a smooth graph for a smooth curve and so we just need to pick a big enough number depending on the function. But be careful not to generate too many points since a very large number of points will take a long time to plot!

Now that we have the general idea, let's look at adding style and features to our plots!

Line Properties

A line appearing in a plot has several properties including color, transparency, style, width and markers. We can set these properties when we call plt.plot using the following keyword arguments:

PropertyDescription
alphatransparency (0.0 transparent through 1.0 opaque)
color (or c)any matplotlib color
labeltext appearing in legend
linestyle (or ls)solid, dashed, dashdot, dotted
linewidth (or lw)set width of the line
markerset marker style
markeredgecolor (or mec)any matplotlib color
markerfacecolor (or mfc)any matplotlib color
markersize (or ms)size of the marker

Note that we can specify a matplotlib color in several different ways including by name such as blue or red, or by a RGB tuple such as (1,0,1) for purple. For example, let's plot the function

\$\$y = e^{-x^2}cos(2 pi x) , x in [-2,2]\$\$

Notice that we used the pyplot function plt.legend to display the figure with a legend (showing the line label) and and plt.ylim to set the limits on the vertical axis to [-2,2].

Format Strings

A format string gives us a shortcut to add color, markers and line style to a line plot. For example, if we want to plot the function

\$\$y = frac{1}{1 + x^2} , x in [-5,5]\$\$

with a dashed black line and square markers, we could use keyword arguments:

Or we could use the corresponding format string 'ks--' where k denotes a black line, s a square marker and -- a dashed line:

Much easier! See below for a list of colors, markers and linestyles.

CharacterColor
bblue
ggreen
rred
ccyan
mmagenta
yyellow
kblack
wwhite

CharacterMarker
.point
ocircle
vtriangle down
^triangle up
ssquare
ppentagon
*star
+plus
xx
Ddiamond

Line Styles

CharacterLine Style
-solid line style
--dashed line style
-.dash-dot line style
:dotted line style See the matplotlib.pyplot.plot documentation for more options.

Pyplot Functions

There are many pyplot functions available for us to customize our figures. For example:

FucntionDescription
plt.xlimset \$x\$ limits
plt.ylimset \$y\$ limits
plt.xlabeladd label to the horizontal axis
plt.ylabeladd label to the vertical axis
plt.axisset axis properties (equal, off, scaled, etc.)
plt.xticksset tick locations on the horizontal axis
plt.yticksset tick locations on the vertical axis
plt.legenddisplay legend for several lines in the same figure
plt.savefigsave figure (as .png, .pdf, etc.) to working directory
plt.figurecreate a new figure and set its properties

See the pyplot documentation for a full list of functions.

Examples

Taylor Polynomials

Plot the function \$y = cos(x)\$ along with its Taylor polynomials of degrees 2 and 4.

Heart Curve

Plot the heart curve:

begin{align}x &= 16 sin^3(t) y &= 13 cos(t) - 5 cos(2t) - 2 cos(3t) - cos(4t)end{align}

for \$t in [0,2pi]\$.

Subplots

The plt.subplot function takes at least 3 inputs \$n\$, \$m\$ and \$i\$ and creates a figure with a \$n\$ by \$m\$ grid of subplots and then sets the \$i\$th subplot (counting across the rows) as the current plot (ie. current axes object).

For example, consider the sawtooth wave

\$\$f(t) = frac{1}{2} - frac{1}{pi} sum_{k=1}^{infty} (-1)^k frac{sin(2 pi k t)}{k}\$\$

and let \$f_N(t)\$ denote the \$N\$th partial sum of the sawtooth wave:

\$\$f_N(t) = frac{1}{2} - frac{1}{pi} sum_{k=1}^{N} (-1)^k frac{sin(2 pi k t)}{k}\$\$

Create a 2 by 2 grid of subplots to plot the first 4 partial sums:

begin{align}f_1(t) &= frac{1}{2} + frac{sin(2 pi t)}{pi} f_2(t) &= frac{1}{2} + frac{sin(2 pi t)}{pi} - frac{sin(4 pi t)}{2pi} f_3(t) &= frac{1}{2} + frac{sin(2 pi t)}{pi} - frac{sin(4 pi t)}{2pi} + frac{sin(6 pi t)}{3pi} f_4(t) &= frac{1}{2} + frac{sin(2 pi t)}{pi} - frac{sin(4 pi t)}{2pi} + frac{sin(6 pi t)}{3pi} - frac{sin(8 pi t)}{4pi}end{align}

R In Jupyter Notebook

See the documentation for more about subplots.

Beyond Line Plots

Scatter plots

A scatter plot has 4 dimensions: \$x\$ coordinate, \$y\$ coordinate, size and color. Let's make a random scatter plot:

Histograms

Generate an array of 10000 random numbers sampled from the normal distribution and create a histogram. Let's also superimpose the normal distribution:

\$\$y = frac{1}{sqrt{2pi}} e^{-x^2/2}\$\$

Bar plots

Plot the total precipitation in Vancouver by month as a bar plot:

Figure and Axes Objects

Under construction

Exercises

1. Plot the following functions:
1. \$f(x) = sqrt{ x }\$, \$x in [-9,9]\$
2. \$f(x) = sin(x) + sin(2x)\$, \$x in [0,4pi]\$
3. \$f(x) = arctan(x)\$, \$x in [-5,5]\$
4. \$f(x) = 2xln x \$, \$x not=0\$, \$f(0)=0\$, \$x in [-1,1]\$
5. \$f(x) = (x+2)(x+1)(x-1)(x-2)(x - 3)\$, \$x in [-2,3]\$
6. \$f(x) = e^{-x^2}\$, \$x in [-2,2]\$
2. Plot the figure eight curve:

begin{align}x &= sin(t) y &= sin(t) cos(t)end{align}

for \$t in [0,2pi]\$.

3. Plot the trefoil knot:

begin{align}x &= sin t + 2 sin 2t y &= cos t - 2 cos 2tend{align}

for \$t in [0,2pi]\$.

4. Plot the butterfly curve:

begin{align}x &= sin(t) left( e^{cos t } - 2 cos(4t) - sin^5(t/12) right) y &= cos(t) left( e^{cos t } - 2 cos(4t) - sin^5(t/12) right)end{align}

for \$t in [0,100]\$.

5. Write a function called parametric_plots which takes input parameters a and k and plots the parametric equation

begin{align}x(t) &= 2 k cos(t) - a cos(k t) y(t) &= 2 k sin(t) - a sin(k t)end{align}

for \$t in [0,2pi]\$. Include a title for each subplot to display the values for \$a\$ and \$k\$, and use plt.axis('equal') to display the curve properly.

Text can be added to Jupyter Notebooks using Markdown cells. You can change the cell type to Markdown by using the Cell menu, the toolbar, or the key shortcut m. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here:

You can make text italic or bold by surrounding a block of text with a single or double * respectively.

You can build nested itemized or enumerated lists:

• One
• Sublist
• This
• Sublist
• That
• The other thing
• Two
• Sublist
• Three
• Sublist

Now another list:

1. Here we go
1. Sublist
2. Sublist
2. There we go
3. Now this

Here is a blockquote:

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea — let’s do more of those!

Why Use Jupyter Notebook

You can use backslash to generate literal characters which would otherwise have special meaning in the Markdown syntax. You can add headings by starting a line with one (or multiple) # followed by a space, as in the following example: 