0
1

More than 3 years have passed since last update.

普通のグラフを散布図っぽくアレンジ|Pandas

Last updated at Posted at 2020-06-17

散布図の描画を利用してグラフ作成

散布図と言いますとだいたいこんなイメージですよね。
スクリーンショット 2020-06-17 23.32.49.jpg
こういったデータ分析に強いのがPandasライブラリです。

これを使ってフツーのグラフを散布図風に描画してみました。

まずはソースコードです。

scatterSample.py
# Pandas ライブラリ
# 2020.06.18.ProOJI
# Matplotlib.pyplot、 そしてNumpyも使用
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# 等差数列 linspace(初項、最終項、要素数)
#  例:np.linspace(0, 10, 4)
#    Ans: [ 0. , 3.33333333, 6.66666667, 10. ]
x = np.linspace(0,6.3, 50)
# 正弦(sinθ)
a = (np.sin(x)+1)*3
# 余弦(cosθ)
b = (np.cos(x)+1)*3
# 配列全てを[1]に初期化してしまう強いヤツ( x軸向の直線)
c = np.ones_like(x)*3
# 指数関数
d = np.exp(x)/100.
# 辞書型(dict)でタプルの中に設定
df = pd.DataFrame({"x": x, "a": a, "b": b, "c": c, "d": d})

#
# sin + scatter(意味:散らす)散布図っぽく
ax1 = df.plot(kind="scatter", x="x", y="a", color="blue", label="a - x")
# cos + scatter
ax2 = df.plot(kind="scatter", x="x", y="b", color="red", label="b - x", ax=ax1)
# 一直線 + 実線
df.plot(x="x", y="c", color="green", label="c - x", ax=ax1)
# 指数関数 + scatter ( xをy軸方面へ使用 )
df.plot(kind="scatter", x="d", y="x", color="orange", label="b - d", ax=ax1)
# sin + 実線 ( xをy軸方面へ使用 )
df.plot(x="a", y="x", color="purple", label="x - a", ax=ax1)

# ラベルを設置
ax2.set_xlabel("Horizontal Label")
ax2.set_ylabel("Vertical Label")
# 表示
plt.show()

コーディング後、出来上がったグラフがこちら
(ちょっとにぎやかすぎた感は否めない...)
スクリーンショット 2020-06-18 0.05.07.jpg

まとめ

本来ならPandasはもっと意味のあることに使用するのでしょうが
お近づき、という感じでフツーのグラフを2Dで描画してみました。
いろいろと利用価値がありますね。

0
1
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
0
1