スタジオおふとん

プログラミング系

Pythonでいろいろグラフを書いてみた

基本的にはcsvからとってきて、散布図とか書く。ってやつです。
Excelでもやろうとすればできるレベル。

いっぱいコメントアウトありますが、完全に自分用です。
Python初心者なんでいじめないでください。

環境はjupyter notebook

よく使いそうなのは、以下ですかね。

  • 基本統計量の算出
  • 散布図
  • 絞りのMatrix図

histogramは頭が悪いので軸で混乱してしまう...

#import numpy as np
import pandas as pd

import plotly.offline as po
import plotly.graph_objs as go
import plotly.figure_factory as ff
po.init_notebook_mode()

iris = pd.read_csv("iris.csv")

df = pd.DataFrame(iris)

#並び替え
#df.sort_index(axis=1, ascending=True)

#基本統計量の算出
#df.describe()

#わからんから最初にやったheaderで無理やり渡すやり方。
#あんまよくないが、C言語脳にはわかりやすい。
header = ['SepalLength','SepalWidth','PetalLength','PetalWidth','Name']

trace = go.Scatter(
#文字列で渡すのが基本っぽいが、ループとかやりにくいのがいまいち
    #x = np.array(df['SepalLength']),
    #y = np.array(df['SepalWidth']),
#header方式、無理やり。
    #x = np.array(df[header[0]]),
    #y = np.array(df[header[1]]),
#これが正解っぽいです
    x = df.ix[:,0].values,
    y = df.ix[:,1].values,
    mode = "markers")

layout = go.Layout(
    title='Iris sepal Length-width',
    xaxis=dict(title='sepal length(cm)'),
    yaxis=dict(title='sepal width(cm)'),
    showlegend=False)

data = [trace]
#散布図とか
fig = dict(data=data, layout=layout)
#Matrix図
fig2 = ff.create_scatterplotmatrix(df, height=1600, width=1600)
#シャレオツなヒートグラフ的なやつ
fig3 = ff.create_scatterplotmatrix(df, diag='histogram', index=header[0],
                                  colormap=['rgb(100, 150, 255)', '#F0963C', 'rgb(51, 255, 153)'],
                                  colormap_type='seq', height=800, width=800)
#量多いとカオスになるので絞るパターン
df_custom = pd.DataFrame(iris, columns=[header[0], header[1], header[2]])
fig4 = ff.create_scatterplotmatrix(df_custom, diag='histogram',index=header[0], colormap='Blues', height=800, width=800)

po.iplot(fig, filename="Iris-sample-scatter", image="png")
po.iplot(fig2, filename="Scatterplot Matrix", image="png")
po.iplot(fig3, filename="Sequential Colormap", image="png")
po.iplot(fig4, filename="Sequential Colormap2", image="png")

# 相関係数一覧
df.corr()

# 参考
# https://plot.ly/python/scatterplot-matrix/

こんな感じです
f:id:kasero:20170924215534p:plain

追記

よくよく考えたら本当にやりたかったのはこんな感じだった。
ひとつの軸に対して、どれが相関がありそうか調べる。
あと、日本語対応。

import numpy as np
import pandas as pd

import plotly.offline as po
import plotly.graph_objs as go
import plotly.figure_factory as ff
po.init_notebook_mode()

iris = pd.read_csv("iris_jp2.csv", encoding="shift-jis")

df = pd.DataFrame(iris)

#基本統計量の算出
#df.describe()

#わからんから最初にやったheaderで無理やり渡すやり方。
#あんまよくないが、C言語脳にはわかりやすい。
#header = ['SepalLength','SepalWidth','PetalLength','PetalWidth','Name']
header = ['アヤメの長さ','アヤメの額の幅','アヤメの花弁の長さ','アヤメの花弁の幅','Name']

for i in header:
    trace = go.Scatter(
        #header方式、無理やり。
        #x = np.array(df[header[0]]),
        #y = np.array(df[header[1]]),
        x = df.ix[:,0].values,
        y = df.ix[:,i].values,
        #y = df.loc[:,i],
        mode = "markers")

    layout = go.Layout(
        title=header[0] + " - " + i,
        xaxis=dict(title=header[0]),
        yaxis=dict(title=i),
        showlegend=False)

    data = [trace]
#散布図とか
    fig = dict(data=data, layout=layout)
  
    #po.iplot(fig, filename=i, image="png")
    po.iplot(fig)

# 相関係数一覧
df.corr()

# 参考
# https://plot.ly/python/scatterplot-matrix/