差分
このページの2つのバージョン間の差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| python:matplotlib [2020/10/14 13:03] – 作成 baba | python:matplotlib [2020/10/15 15:53] (現在) – baba | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== matplotlib ====== | ====== matplotlib ====== | ||
| - | Qiitaに良い記事を見つけたので、メモ。初めてmatplot使うときにすごくわかりやすかったです。 | + | Qiitaに良い記事を見つけたので、メモ。初めてmatplot使うときにすごくわかりやすかったです。ほとんど馬場の忘備録ですが、論文とかでグラフ作成の必要が出たときに見返して大事な部分だけをさらえる内容にしています。 |
| * https:// | * https:// | ||
| + | |||
| + | ===== bar plot ===== | ||
| + | 棒グラフのサンプルです。各barに数値をいれて、ラベルを90度回転させてる例。matplotlibはversion3.3以上を利用してください。 | ||
| + | {{ : | ||
| + | <file .py simple_barplotpy> | ||
| + | import matplotlib | ||
| + | import matplotlib.pyplot as plt | ||
| + | import numpy as np | ||
| + | |||
| + | |||
| + | def autolabel(rects, | ||
| + | """ | ||
| + | Attach a text label above each bar in *rects*, displaying its height. | ||
| + | |||
| + | *xpos* indicates which side to place the text w.r.t. the center of | ||
| + | the bar. It can be one of the following {' | ||
| + | """ | ||
| + | |||
| + | ha = {' | ||
| + | offset = {' | ||
| + | |||
| + | for rect in rects: | ||
| + | height = rect.get_height() | ||
| + | ax.annotate(' | ||
| + | xy=(rect.get_x() + rect.get_width() / 2, height), | ||
| + | xytext=(offset[xpos]*3, | ||
| + | textcoords=" | ||
| + | ha=ha[xpos], | ||
| + | |||
| + | |||
| + | # plotting data | ||
| + | plot_data = [20, 35, 30, 35, 27, 50, 35, 30, 35, 27] | ||
| + | x_labels = [' | ||
| + | index = np.arange(len(plot_data)) | ||
| + | width = 0.35 # the width of the bars | ||
| + | |||
| + | |||
| + | fig, ax = plt.subplots(figsize=(15, | ||
| + | |||
| + | rects = ax.bar(index, | ||
| + | ax.set_box_aspect(0.2) | ||
| + | ax.autoscale() | ||
| + | |||
| + | # Add some text for labels, title and custom x-axis tick labels, etc. | ||
| + | ax.set_ylabel(' | ||
| + | ax.set_title(' | ||
| + | ax.set_xticks(index) | ||
| + | ax.set_xticklabels(x_labels) | ||
| + | ax.legend() | ||
| + | |||
| + | autolabel(rects, | ||
| + | |||
| + | fig.tight_layout() | ||
| + | plt.margins(0.01, | ||
| + | plt.subplots_adjust(bottom=0.10) | ||
| + | # plt.grid(True) | ||
| + | plt.xticks(index, | ||
| + | plt.savefig(' | ||
| + | plt.show() | ||
| + | |||
| + | </ | ||
| + | ===== box plot ===== | ||
| + | 箱ひげ図を描く最小のサンプルは次の通り。 | ||
| + | <file .py simple_boxplot.py> | ||
| + | test | ||
| + | </ | ||
| + | |||