VOOZH about

URL: https://qiita.com/maskot1977/items/7e728b7bf375772a0d02

⇱ 散布図 #Python - Qiita


👁 Image
1

Go to list of users who liked

0

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

@maskot1977(Ikemen Mas Kot)Team Coffee

散布図

1
Posted at

散布図

「合州国の州別暴力犯罪率」を例に説明します。

# 「#」(シャープ)以降の文字はプログラムに影響しません。
# 図やグラフを図示するためのライブラリをインポートする。
import matplotlib.pyplot as plt
%matplotlib inline
# データフレーム操作に関するライブラリをインポートする
import pandas as pd
from pandas.tools import plotting # 高度なプロットを行うツールのインポート
# URL によるリソースへのアクセスを提供するライブラリをインポートする。
# import urllib # Python 2 の場合
import urllib.request # Python 3 の場合
df2 = pd.read_csv('USArrests.txt', sep='\t', index_col=0) # データの読み込み
df2
# 散布図を描く
df2.plot(kind='scatter', x=df2.columns[2], y=df2.columns[0], grid=True)
# 丸のサイズと色に意味をもたせた散布図を描く
df2.plot(kind='scatter', x=df2.columns[2], y=df2.columns[0], grid=True, c=df2.iloc[:, 3], cmap='coolwarm', 
 s=df2.iloc[:, 1], alpha=0.5)
# 丸のサイズと色に意味をもたせた散布図を描く(別法)

names, name_label = df2.dropna().index, "States"
x_axis, x_label = df2.dropna()["UrbanPop"], "UrbanPop"
y_axis, y_label = df2.dropna()["Murder"], "Murder"
sizes, size_label = df2.dropna()["Assault"], "Assault" 
colors, color_label = df2.dropna()["Rape"], "Rape"

plt.figure(figsize=(15, 10))
for x, y, name in zip(x_axis, y_axis, names):
 plt.text(x, y, name, alpha=0.8, size=12)
plt.scatter(x_axis, y_axis, s=sizes, c=colors, alpha=0.5, cmap='coolwarm')
plt.colorbar(alpha=0.8)
plt.title("x=%s, y=%s, size=%s, color=%s" % (x_label, y_label, size_label, color_label))
plt.xlabel(x_label, size=12)
plt.ylabel(y_label, size=12)
plt.grid()
plt.show()
1

Go to list of users who liked

0
0

Go to list of comments

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
1

Go to list of users who liked

0