[PYTHON] Display histogram / scatter plot 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 top right button.

Also, in this article, a CSV file created at random https://github.com/suzuki-navi/sample-data/blob/master/sample-data-1.csv I am using.

Data preparation

Load various imports and data.

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("sample-data-1.csv", names=["id", "target", "data1", "data2", "data3"])

df will be a DataFrame object.

image.png

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

histogram

Matplotlib documentation https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html#matplotlib.pyplot.hist

plt.hist(df["data1"])

image.png

You can specify the number of divisions on the horizontal axis with the option bins.

plt.hist(df["data1"], bins=30)

image.png

You can specify the range on the horizontal axis with range.

plt.hist(df["data1"], range=(-100, 100))

image.png

If you specify density = True, it is normalized so that the sum of the vertical axes is 1.0.

image.png

Scatter plot

Matplotlib documentation https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter

plt.scatter(df["data1"], df["data2"])

image.png

If you specify a sequence of integers in c, it seems that it will color-code each value.

plt.scatter(df["data1"], df["data2"], c = df["target"])

image.png

If you have a lot of points, you should specify ʻalpha. 0.0 is transparent, 1.0` is opaque, and you can see the shade by specifying the middle.

plt.scatter(df["data1"], df["data2"], c = df["target"], alpha=0.5)

image.png

It seems that you can specify the range of the horizontal axis with the methods xlim and ylim.

plt.xlim(-50, 50)
plt.ylim(-5, 5)

image.png

that's all.

Recommended Posts

Display histogram / scatter plot on Jupyter Notebook
Simply display a line graph on Jupyter Notebook
Display HTML in Jupyter notebook
Display PIL images on Jupyter
High charts on 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)
Formatting with autopep8 on 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)
Scatter plot
Make Jupyter Notebook a service on CentOS
Try SVM with scikit-learn on Jupyter Notebook
Start jupyter notebook on GPU server (remote server)
Display the graph of tensorBoard on jupyter
Clone the github repository on jupyter notebook
Build jupyter notebook on remote server (CentOS)
Use vim keybindings on Docker-launched Jupyter Notebook
[Jupyter Notebook memo] Display kanji with matplotlib
Run Jupyter notebook on a remote server
I want to display an image on Jupyter Notebook using OpenCV (mac)
Label each point on the seaborn scatter plot
Jupyter Notebook memo
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
Introducing Jupyter Notebook
Powerful Jupyter Notebook
Enable Jupyter Notebook with conda on remote server
Golang on jupyter
[Pythonocc] I tried using CAD on jupyter notebook
Jupyter on AWS
Jupyter notebook password
Jupyter Notebook memo
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
Easily launch jupyter notebook on AWS and access locally
Run Tensorflow from Jupyter Notebook on Bash on Ubuntu on Windows
Monitor the training model with TensorBord on Jupyter Notebook
Try basic operations for Pandas DataFrame on Jupyter Notebook
Drawing on Jupyter using the plot function of pandas
EC2 provisioning with Vagrant + Jupyter (IPython Notebook) on Docker
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
How to view progress bar on Jupyter Notebook to see progress
Seaborn basics for beginners ③ Scatter plot (jointplot) * With histogram
Hello X3DOM on Jupyter
Get started Jupyter Notebook
3 Jupyter notebook (Python) tricks
Enlarge the plot display.
[Cloud103] # 3 Jupyter Notebook again
Write charts in real time with Matplotlib on Jupyter notebook
View dynamic graphs in Jupyter notebook. (Inline display of D3.js)
A very convenient way to give a presentation on Jupyter Notebook
Notes on coloring by value in the matplotlib scatter plot
Try clustering with a mixed Gaussian model on a Jupyter Notebook
A note when I can't open Jupyter Notebook on Windows