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.
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.
See the previous article for reading from CSV and handling DataFrame. Try basic operation for DataFrame --Qiita
Matplotlib documentation https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html#matplotlib.pyplot.hist
plt.hist(df["data1"])
You can specify the number of divisions on the horizontal axis with the option bins
.
plt.hist(df["data1"], bins=30)
You can specify the range on the horizontal axis with range
.
plt.hist(df["data1"], range=(-100, 100))
If you specify density = True
, it is normalized so that the sum of the vertical axes is 1.0.
Matplotlib documentation https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter
plt.scatter(df["data1"], df["data2"])
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"])
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)
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)
that's all.
Recommended Posts