カラフルな散布図を保存するプログラムを作りましたが、処理時間がかかりすぎて困っています。私の環境で下記プログラムをデータ数1000で実行した場合25s程度、データ数2000で実行すると75s程度かかります。最終的にデータ数6000で実行しようと考えています。処理時間を短くする方法をご教授お願い致します。
python
1# カラフルな散布図を保存する時間 2 3# モジュール・クラス群のインポート 4from matplotlib import pyplot as plt 5import time 6import random 7 8# データ数設定 9positions = 1000 10# 1000の時、scatter経過時間が10s, 画像保存時間が12s。 11# 2000の時、scatter経過時間が38s, 画像保存時間が48s。 12 13# x1,y1の設定 14x1 = [] 15y1 = [] 16for i in range(0, positions): 17 x1.append(round(random.random(), 2)) 18 y1.append(round(random.random(), 2)) 19 20t_start = time.time() # プログラムの実行時間測定用 21 22# figureを生成する 23fig = plt.figure() 24# axをfigureに設定する 25ax = fig.add_subplot(1, 1, 1) 26 27# x1,y1の色の設定 28cm1 = plt.get_cmap('jet') # jetはカラフルなカラーマップ 29cm1_2d = [] 30for i in range(0, positions): 31 cm1_2d.append(cm1(i / positions)) 32 33# 経過時間確認 34time_scatter_before = time.time() 35elapsed_time = time_scatter_before - t_start 36print(f"scatter前経過時間:{elapsed_time}") 37 38# x1, y1をjetのカラーマップでプロット 39for i in range(0, positions): 40 ax.scatter(x1, y1, s=5, alpha=1, c=cm1_2d) 41 42# 経過時間確認 43time_scatter_after = time.time() 44elapsed_time = time_scatter_after - time_scatter_before 45print(f"scatter経過時間:{elapsed_time}") 46 47# グラフの設定 48ax.grid(True) # grid表示ON 49ax.set_axisbelow(True) 50ax.set_facecolor('0.8') # グラフ背景 51ax.grid(color='1') # 目盛り線の色 52ax.set_xlim(left=0, right=1) # x範囲 53ax.set_ylim(bottom=0, top=1) # y範囲 54ax.set_xlabel('X1', fontname="Meiryo") # x軸ラベル 55ax.set_ylabel('Y1', fontname="Meiryo") # y軸ラベル 56ax.set_title('グラフタイトル', fontname="Meiryo") # グラフタイトル 57 58# 経過時間確認 59time_graph_setting = time.time() 60elapsed_time = time_graph_setting - time_scatter_after 61print(f"グラフ設定経過時間:{elapsed_time}") 62 63plt.savefig("x1y1.png") 64 65# 経過時間確認 66time_save = time.time() 67elapsed_time = time_save - time_graph_setting 68print(f"画像保存時間:{elapsed_time}")
for i in range(0, positions):
ax.scatter(x1, y1, s=5, alpha=1, c=cm1_2d)
とされていますが、同じ位置に同じ色でデータをプロットしています(1000回上書き)。何か意味があるのでしょうか?

回答1件
あなたの回答
tips
プレビュー