4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonでggplot2のプロットを作成する(plotnine)

Last updated at Posted at 2023-12-12

Pythonには、plotnineというデータ可視化ライブラリがあります。
plotnineはggplot2をもとにしており、plotnineを用いることで、Pythonでggplot2のプロットを作成することができます。
今回は、irisデータセットの可視化を行います。
実行環境:MacBook Air M1, 2020

Pythonのインストール

以下のサイトにアクセスし、Latest Python 3 Release - Python 3.12.1をクリックします。
https://www.python.org/downloads/macos/
スクリーンショット 2023-12-09 1.58.58.png

開いたページを下にスクロールし、macOS 64-bit universal2 installerをクリックします。

スクリーンショット 2023-12-09 1.59.48.png

インストーラをダウンロードしたら、インストールを進めます。

スクリーンショット 2023-12-09 2.00.38.png

ターミナルでPythonのバージョンを確認します。

% python3 -V
Python 3.12.1

Python 3.12.1をインストールすることができました。

環境構築

今回は、仮想環境を構築し、データ可視化を行います。
ターミナルで以下のコマンドを実行します。

% mkdir plotnine
% cd plotnine
% python3 -m venv .venv
% source .venv/bin/activate

plotnineディレクトリを作成し、仮想環境の構築と起動を行いました。
次回からは、以下のコマンドのみを実行します。

% cd plotnine
% source .venv/bin/activate

ライブラリのインストール

以下のコマンドを実行し、データ可視化に用いるライブラリをインストールします。

% pip install ipykernel
% pip install pandas
% pip install plotnine
% pip install scikit-learn

VSCodeのインストール

以下のURLからVSCodeをインストールしました。
https://code.visualstudio.com

データ可視化

VSCodeを起動し、plotnineディレクトリを開き、iris.ipynbを作成しました。

ライブラリの読み込み

import pandas as pd
from plotnine import *

データの読み込み

sklearn.datasetsからirisデータセットを読み込みます。

from sklearn.datasets import load_iris
iris = load_iris()
data = pd.DataFrame(data = iris.data, columns = iris.feature_names)
data.head()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

データフレームにアヤメの種類(target)を追加します。

data['target'] = iris.target
data.loc[data['target'] == 0, 'target'] = "setosa"
data.loc[data['target'] == 1, 'target'] = "versicolor"
data.loc[data['target'] == 2, 'target'] = "virginica"
data.head()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

データ可視化

棒グラフ

アヤメの種類(target)のカウントを示す棒グラフを作成します。

(ggplot(data, aes(x = 'target', fill = 'target'))
 + geom_bar())

image.png

散布図

がく片の長さとがく片の幅の関係を示す散布図を作成します。

(ggplot(data, aes(x = 'sepal length (cm)', y = 'sepal width (cm)', color = 'target')) 
 + geom_point())

image.png

ヒストグラム

がく片の長さの分布を示すヒストグラムを作成します。

(ggplot(data, aes(x = 'sepal length (cm)', fill = 'target')) 
 + geom_histogram(position = "identity", alpha = 0.5, bins = 10))

image.png

密度曲線

がく片の長さの分布を示す密度曲線を作成します。

(ggplot(data, aes(x = 'sepal length (cm)', fill = 'target')) 
 + geom_density(alpha = 0.5))

image.png

箱ひげ図

がく片の長さの分布を示す箱ひげ図を作成します。

(ggplot(data, aes(x = 'target', y = 'sepal length (cm)', fill = 'target')) 
 + geom_boxplot())

image.png

バイオリンプロット

がく片の長さの分布を示すバイオリンプロットを作成します。

(ggplot(data, aes(x = 'target', y = 'sepal length (cm)', fill = 'target')) 
 + geom_violin())

image.png

密度プロット

がく片の長さとがく片の幅の関係を示す密度プロットを作成します。

(ggplot(data, aes(x = 'sepal length (cm)', y = 'sepal width (cm)', color = 'target')) 
 + geom_point()
 + stat_density_2d())

image.png

plotnineを用いてデータ可視化を行うことができました。
Pythonでggplot2と同様のプロットを作成できることがすごいと思いました。

参考文献

https://biomedicalhacks.com/2020-08-07/python-ggplot2/
https://qiita.com/ao_log/items/fe9bd42fd249c2a7ee7a
https://plotnine.readthedocs.io/en/v0.12.4/
https://plotnine.readthedocs.io/en/v0.12.4/api.html

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?