Simply display a line graph on Jupyter Notebook

How to display histograms and scatter plots on Jupyter Notebook using Matplotlib.

The content of this article is tested in the environment of Jupyter Notebook prepared according to the following article. Easy installation and startup of Jupyter Notebook using Docker (also supports nbextensions and Scala) --Qiita

In this environment, you can access port 8888 with a browser and use Jupyter Notebook. You can open a new note by following New> Python 3 on the upper right button.

See the following article for histograms and scatter plots. Display histogram / scatter plot on Jupyter Notebook --Qiita

Data preparation

We have prepared two sample data assuming that the first column is the x and the second column is the y-axis.

test1.csv


0,100
1,110
2,108
4,120
6,124

test2.csv


0,90
1,95
2,99
3,104
4,108
5,111
6,115

Open Jupyter Notebook and import various things.

%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Read the data.

df1 = pd.read_csv("test1.csv", names=["x", "y"])
df2 = pd.read_csv("test2.csv", names=["x", "y"])

df will be an object of Pandas DataFrame.

See previous article for reading from CSV and handling DataFrame. Try basic operation for DataFrame --Qiita

Convert CSV file data to line graph

plt.plot(df1["x"], df1["y"])

image.png

df1 ["x "] and df1 ["y "] are Pandas Series objects that can be passed to plt.plot.

matplotlib.pyplot.plot — Matplotlib 3.1.1 documentation

You can also overlay two graphs.

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(df1["x"], df1["y"])
ax1.plot(df2["x"], df2["y"])

image.png

Graph any function

y = x^2 + 3x + 80

Let's draw a graph of the function.

x3 = np.linspace(0, 6, 13)
y3 = x3 * x3 + 3.0 * x3 + 80.0
plt.plot(x3, y3)

image.png

np.linspace (0, 6, 7) is an array of NumPy ndarrays with elements 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6 return. It is an array in which a total of 13 numbers are evenly arranged with 0 and 6 at both ends.

numpy.linspace — NumPy v1.17 Manual

If you perform four arithmetic operations on ndarray, it will be ndarray with the same number of elements, so x3 * x3 + 3.0 * x3 + 80.0 will also be ndarray containing 7 numbers.

If you make x and y into an array and pass it to plt.plot, you can make a graph like CSV data. I passed the Pandas Series to plt.plot earlier, but it seems that I can also pass the NumPy ndarray.

It can also be displayed superimposed on the CSV file data.

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(df1["x"], df1["y"])
ax1.plot(df2["x"], df2["y"])
ax1.plot(x3, y3)

image.png

option

You can specify the shape of a point in the data by passing the argument marker to plot.

x3 = np.linspace(0, 6, 13)
y3 = x3 * x3 + 3.0 * x3 + 80.0
plt.plot(x3, y3, marker=".")

image.png

x3 = np.linspace(0, 6, 13)
y3 = x3 * x3 + 3.0 * x3 + 80.0
plt.plot(x3, y3, marker="o")

image.png

See the reference below for the character strings that can be specified for marker. matplotlib.markers — Matplotlib 3.1.1 documentation

You can specify the destination width with the argument linewidth. If 0 is specified, there will be no line.

x3 = np.linspace(0, 6, 13)
y3 = x3 * x3 + 3.0 * x3 + 80.0
plt.plot(x3, y3, marker="o", linewidth=0)

image.png

Other options are also listed in the references below. matplotlib.pyplot.plot — Matplotlib 3.1.1 documentation

that's all.

Recommended Posts

Simply display a line graph on Jupyter Notebook
Install matplotlib and display graph on Jupyter Notebook
Make Jupyter Notebook a service on CentOS
Display the graph of tensorBoard on jupyter
Display histogram / scatter plot on Jupyter Notebook
Run Jupyter notebook on a remote server
Display HTML in Jupyter notebook
View PDF on Jupyter Notebook
Run Jupyter Notebook on windows
[Memo] Display Jupyter Notebook on PC in monospaced font (Mac)
Unable to display tensorboard in jupyter notebook on docker (solved)
A very convenient way to give a presentation on Jupyter Notebook
Try clustering with a mixed Gaussian model on a Jupyter Notebook
A note when I can't open Jupyter Notebook on Windows
Formatting with autopep8 on Jupyter notebook
Post a Jupyter Notebook as a blog post
Make a sound with Jupyter notebook
Run azure ML on jupyter notebook
Try running Jupyter Notebook on Mac
Real-time display of video acquired from webcam on Jupyter notebook (Python3)
Try SVM with scikit-learn on Jupyter Notebook
Start jupyter notebook on GPU server (remote server)
I want to display an image on Jupyter Notebook using OpenCV (mac)
How to set up a jupyter notebook on ssh destination (AWS EC2)
Clone the github repository on jupyter notebook
GPU check of PC on jupyter notebook
Build jupyter notebook on remote server (CentOS)
Use vim keybindings on Docker-launched Jupyter Notebook
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Simply draw a graph by specifying a file
[Jupyter Notebook memo] Display kanji with matplotlib
Build a PYNQ environment on Ultra96 V2 and log in to Jupyter Notebook
Try a state-space model (Jupyter Notebook + IR kernel)
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
Draw a graph with Japanese labels in Jupyter
Quickly display the QR code on the command line
Simply build a Python 3 execution environment on Windows
[Pythonocc] I tried using CAD on jupyter notebook
Graph drawing with jupyter (ipython notebook) + matplotlib + vagrant
Try Apache Spark on Jupyter Notebook (on local Docker
Remotely open Jupyter notebook launched on the server
jupyter notebook does not start on mac fish
Jupyter Notebook memo
Introducing Jupyter Notebook
Powerful Jupyter Notebook
[Don't refer to 04.02.17] Display the temperature sensor on a real-time graph with rasberry pi 3.
Golang on jupyter
Jupyter on AWS
Jupyter notebook password
Jupyter Notebook memo
A memo that uses an interactive display mode like Jupyter notebook with VSCode + Python
Read the csv file with jupyter notebook and write the graph on top of it
Run Tensorflow from Jupyter Notebook on Bash on Ubuntu on Windows
[Python] How to draw a line graph with Matplotlib
Monitor the training model with TensorBord on Jupyter Notebook
Until drawing a 3D graph in Python on windows10
Visualize railway line data as a graph with Cytoscape 2
Make a parrot return LINE Bot on AWS Cloud9
Try basic operations for Pandas DataFrame on Jupyter Notebook
Open Jupyter Lab (or Jupyter Notebook) by specifying a directory
Drawing a tree structure with D3.js in Jupyter Notebook