以前、Matplotlibのロゴを自分で作るべく、極座標図の1番シンプルなソースコードを勉強しました。
今回は、その続きです。もう少しかっこいい図へと発展しようと思います。

Matplotlibの極座標図を点からバーに

前回は、1番シンプルなソースコードとして、下まで書けました。
import numpy as np
import matplotlib.pyplot as plt
x = 1.0 #プロットする値 [-] theta = 60 #プロット位置θ [度] # np.deg2radで[度]を[rad]に変換 theta_rad = np.deg2rad(theta) #プロット位置 [rad] #plt.plotではpolarは使えないので、plt.subplotで。 plt.subplot(polar="True") plt.scatter(theta_rad, x, color="red") plt.savefig("logo.png")
plt.show()
これを出力すると、以下の図になります。
logo
これだと極座標図の点だけになってしまったので、
Screen Shot 2020-01-05 at 10.36.30 PM

Matplotlibのロゴのように、点を"bar"にしましょう。


やり方を調べていたら、以下のサイトで乱数を使って、極座標図を綺麗に書く方法が紹介されていました。
参考サイト:極表示でのプロット 

1つずつ勉強しながらソースコードを書いてみました。コメントに解説を載せます。
import numpy as np
import matplotlib.pyplot as plt

#plt.plotではpolarは使えないので、plt.subplotで。
ax = plt.subplot(polar="True")
#Matplotlibのロゴと同じ18点
n = 18

#プロット点のx座標n個を2pi間に等間隔で用意
#np.deg2radで0[度]から360[度]を[rad]に変換
theta = np.arange(np.deg2rad(0),np.deg2rad(360),2*np.pi/n)

x = np.random.rand(n) #プロットする値(barの長さ)を乱数で作成 [-]
width = np.pi / 4 * np.random.rand(n) #幅も乱数で定義

#plt.barを使うとMatplotlibのロゴっぽく!
bars = plt.bar(theta, x, width=width, bottom=0.0)

#色付け・透過 for bar in bars: bar.set_facecolor(plt.cm.jet(np.random.rand())) #色も乱数で bar.set_alpha(0.5) #透明度50% ax.set_xticklabels([]) #x軸(角度)非表示 ax.set_yticklabels([]) #y軸(値)非表示 plt.savefig("logo2.png") plt.show()
乱数で値を指定すると、ソースコードを実行するたびに違う図が出てきて楽しいですね!
logo3
完成:)
この図をTwitterのアイコンにしようと思います。