pythonデータ可視化練習
13598 ワード
データ可視化のために
例:『Pythonプログラミング入門から実戦へ』【美】Eric Matthes
インストール
anacondaを使用すると、
テストし、エラーが報告されていない場合は、コンポーネントパッケージが正常に使用できることを証明します.
使用
plot()
単純な使用
出力画像
修飾グラフ
出力画像
対応X Y座標
plot()関数は、図面を描くために使用される
scatter()散点図の描画
一連の点を描く方法
典型的な関数
軸範囲の設定
座標軸の範囲を設定します.例えばx軸を0~1100 y軸に0~1100000とする
色の設定
2つの色の設定方法
カラーグラデーション
パラメータcをy値リストに設定し,パラメータcmapを用いてpyplotにどの色マッピングを使用するかを教える.これらのコードは、y値の小さい点を水色に表示し、y値の大きい点を水色に表示します.集団スタイルは次のとおりです.
グラフの自動保存
2番目のパラメータは、グラフの余分な空白領域を切り取ることを指定します.
ランダムウォーク
クラスの作成
オブジェクトをインスタンス化して使用
結果
始点と終点をハイライトします.
サイコロのシミュレーション結果の可視化
サイコロ
1000回の結果を生成し、棒グラフを描画
結果
サイコロを2つ投げる
結果
matplotlib
,pygal
の2つのパケットを簡単に使用し,pythonを用いて折れ線図,散点図,ランダムウォークデータの生成,サイコロのシミュレーション,結果の統計を試みた.例:『Pythonプログラミング入門から実戦へ』【美】Eric Matthes
インストール
anacondaを使用すると、
matplotlib
がインストールされます.pipコマンドを使用してインストールできない場合.ここでは余計なことは言わない.import matplotlib
テストし、エラーが報告されていない場合は、コンポーネントパッケージが正常に使用できることを証明します.
使用
plot()
単純な使用
import matplotlib.pyplot as plt
squares = [1,4,8,16,25]
plt.plot(squares)
plt.show()
出力画像
修飾グラフ
import matplotlib.pyplot as plt
squares = [1,4,8,16,25]
plt.plot(squares,linewidth=5) #
#
plt.title("Square Numbers", fontsize=24) #
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
#
plt.tick_params(axis='both', labelsize=14) #
plt.show()
出力画像
対応X Y座標
input_values = [1, 2, 3, 4, 5]
squares = [1,4,8,16,25]
plt.plot(input_values,squares,linewidth=5)
plot()関数は、図面を描くために使用される
xy
の値を受け入れる.scatter()散点図の描画
import matplotlib.pyplot as plt
plt.scatter(2, 4, s=200)
#
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
#
plt.tick_params(axis='both', which='major', labelsize=14)
plt.show()
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.scatter(x_values, y_values, s=100)
一連の点を描く方法
典型的な関数
軸範囲の設定
plt.axis([0, 1100, 0, 1100000])
座標軸の範囲を設定します.例えばx軸を0~1100 y軸に0~1100000とする
色の設定
plt.scatter(x_values, y_values, c='red', edgecolor='none', s=40)
plt.scatter(x_values, y_values, c=(0, 0, 0.8), edgecolor='none', s=40)
2つの色の設定方法
カラーグラデーション
import matplotlib.pyplot as plt
x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues,
edgecolor='none', s=40)
plt.axis([0, 1100,-200, 1100000])
plt.show()
パラメータcをy値リストに設定し,パラメータcmapを用いてpyplotにどの色マッピングを使用するかを教える.これらのコードは、y値の小さい点を水色に表示し、y値の大きい点を水色に表示します.集団スタイルは次のとおりです.
グラフの自動保存
plt.savefig('squares_plot.png', bbox_inches='tight')
2番目のパラメータは、グラフの余分な空白領域を切り取ることを指定します.
ランダムウォーク
クラスの作成
from random import choice
class RandomWalk():
""" """
def __init__(self, num_points = 5000):
""" """
self.num_points = num_points
# (0,0)
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
""" """
while len (self.x_values) < self.num_points:
x_direction = choice([1,-1])
x_distance = choice([0,1,2,3,4])
x_step = x_direction * x_distance
y_direction = choice([1, -1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
# x y
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
self.x_values.append(next_x)
self.y_values.append(next_y)
オブジェクトをインスタンス化して使用
import matplotlib.pyplot as plt
from random_walk import RandomWalk
rw = RandomWalk()
rw.fill_walk()
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues,edgecolor='none', s=15)
plt.show()
結果
始点と終点をハイライトします.
#
plt.scatter(0,0,c ='green',edgecolors = 'none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor = 'none',s =100)
サイコロのシミュレーション結果の可視化
サイコロ
from random import randint
class Die():
""" """
def __init__(self,num_sides=6):
""" 6 """
self.num_sides = num_sides
def roll(self):
""" 1 """
return randint(1,self.num_sides)
1000回の結果を生成し、棒グラフを描画
from die import Die
import pygal
# D6
die = Die()
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)
#
frequencies = []
for value in range(1,die.num_sides+1):
frequency = results.count(value)
frequencies.append(frequency)
# print(frequencies)
#
hist = pygal.Bar()
hist.title = "Result of rolling one D6 1000 times."
hist.x_labels = ['1','2','3','4','5','6']
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6',frequencies)
hist.render_to_file('die_visual.svg')
結果
サイコロを2つ投げる
from die import Die
import pygal
die_1 = Die()
die_2 = Die()
results = []
for roll_num in range(1000):
result = die_1.roll() + die_2.roll()
results.append(result)
#
frequencies = []
max_result = die_1.num_sides+die_2.num_sides
for value in range(2,max_result+1):
frequency = results.count(value)
frequencies.append(frequency)
# print(frequencies)
#
hist = pygal.Bar()
hist.title = "Result of rolling two D6 1000 times."
hist.x_labels = ['2','3','4','5','6','7','8','9','10','11','12']
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6 + D6', frequencies)
hist.render_to_file('dice_visual.svg')
結果