python3.doc_65
最終投稿日:2022年6月18日
● hoge.html ※gp_time アプリの name=view_plot を逆引き <img src="{% url 'gp_time:view_plot' %}"> ● urls.py app_name = 'gp_time' urlpatterns = [ ~ 省略 ~ url(r'^view_plot', view_plot, name='view_plot'), ] ● views.py from django.http import HttpResponse from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas import matplotlib.pyplot as plt def view_plot(request): data1 = [95.238, 100.505, 130.679, 116.55, 95.721, 95.369, 95.521, 95.695, 95.815, 95.721, 95.969, 96.179, 96.097, 96.296, 96.494, 96.432, 96.593, 99.31, 114.708, 94.696, 94.453, 94.661, 94.613, 95.033, 96.302, 94.589, 94.727, 94.944, 94.953, 95.502, 95.315, 94.887, 94.88, 95.089, 94.729, 94.812, 94.591, 94.597, 94.748, 94.8, 95.077, 94.985, 95.078, 95.141, 95.482, 95.437, 95.019, 95.266, 95.302, 95.334, 95.406, 95.412, 95.805, 95.778, 95.947, 96.625, 97.033] data2 = [103.446, 109.218, 128.522, 113.636, 96.932, 95.913, 96.551, 95.436, 95.861, 96.253, 95.766, 95.866, 96.254, 96.35, 96.089, 96.054, 96.153, 96.127, 96.276, 95.978, 96.174, 96.046, 96.113, 96.373, 96.927, 100.383, 114.547, 94.126, 94.274, 94.595, 94.421, 94.376, 94.373, 94.424, 94.309, 94.357, 95.084, 94.292, 94.377, 94.191, 94.678, 94.444, 94.54, 94.784, 94.94, 97.091, 95.073, 95.012, 95.385, 94.671, 93.953, 93.987, 94.829, 94.95, 94.558, 94.122, 94.992] data3 = [96.705, 101.541, 129.303, 116.756, 96.329, 95.692, 95.756, 95.939, 95.885, 95.925, 96.267, 96.126, 96.071, 96.274, 96.207, 96.296, 96.084, 96.364, 96.325, 99.344, 116.583, 93.74, 94.729, 94.586, 94.43, 95.012, 94.999, 94.633, 94.628, 95.011, 95.344, 95.028, 94.922, 94.986, 95.493, 94.985, 95.235, 95.216, 95.206, 95.385, 94.874, 95.186, 95.744, 94.546, 94.632, 94.491, 95.188, 94.733, 95.198, 94.648, 94.46, 94.599, 94.622, 95.428, 95.614, 96.364, 96.972] data4 = [100.323, 104.485, 129.434, 117.989, 97.616, 96.957, 96.862, 97.935, 98.113, 97.029, 97.028, 97.113, 97.152, 97.084, 99.656, 116.448, 95.725, 97.218, 96.263, 96.368, 95.585, 95.973, 96.007, 96.223, 96.245, 96.15, 96.089, 96.066, 95.978, 96.105, 96.421, 96.281, 96.446, 99.077, 116.049, 95.291, 95.04, 95.123, 95.417, 95.052, 94.943, 95.021, 95.056, 94.94, 94.938, 94.863, 95.081, 95.328, 95.131, 94.901, 95.185, 95.109, 95.142, 95.006, 95.251, 95.313, 95.55] # 領域のサイズ指定 fig = plt.figure(figsize=(12,8)) a5 = fig.add_subplot(111) # 1つ目のグラフ a5.plot(data1 ,marker='<', linestyle='-', markersize=5,label='y1') a44 = fig.add_subplot(111) # 2つ目のグラフ a44.plot(data2, marker='.', linestyle='--', markersize=5,label='y1') a77 = fig.add_subplot(111) # 3つ目のグラフ a77.plot(data3, marker='v', linestyle='-.', markersize=5,label='y1') a10 = fig.add_subplot(111) # 4つ目のグラフ a10.plot(data4, marker='^', linestyle=':', markersize=5,label='y1') # グラフタイトル plt.title('タイトル', fontsize=18,color='#ff0000') # 横軸のラベル plt.xlabel('LAP数', fontsize=14) # 縦軸のラベル plt.ylabel('LapTime', fontsize=14) # 横軸の表示する値の幅 plt.xlim(1, 58) # 縦軸の表示する値の幅 plt.ylim(93.5, 98) # グリッドを表示 plt.grid(True) # 凡例を表示(bbox_to_anchor は領域の外に凡例を表示する(1,1)で右上) plt.legend(loc='upper left',title='凡例',bbox_to_anchor=(1,0.5)) canvas=FigureCanvas(fig) response=HttpResponse(content_type='image/png') canvas.print_png(response) # 返却 return response【メモ】