Created
June 1, 2020 09:26
-
-
Save gh640/9858dcf9d149a73cec452e9d656c799f to your computer and use it in GitHub Desktop.
サンプルコード: matplotlib.pyplot で出力するグラフのサイズを指定する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """サンプルコード: matplotlib.pyplot で出力するグラフのサイズを指定する | |
| 次の 2 ステップでできる: | |
| 1. `fig.set_size_inches()` で DPI で割ったサイズを指定しておく | |
| 2. `pyplot.savefig()` で DPI を指定して出力する | |
| """ | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| CSV_IN = './data.csv' | |
| FILE_OUT = './chart.png' | |
| DPI = 96 | |
| OUT_WIDTH = 1000 | |
| OUT_HEIGHT = 600 | |
| def main(): | |
| df = pd.read_csv(CSV_IN, index_col=0) | |
| xseries = df.columns | |
| fig, ax = plt.subplots() | |
| fig.set_size_inches(OUT_WIDTH / DPI, OUT_HEIGHT / DPI) | |
| for index, row in df.iterrows(): | |
| line, = ax.plot(xseries, row.values) | |
| line.set_label(index) | |
| ax.legend() | |
| # show_chart() | |
| save_file() | |
| def show_chart(): | |
| plt.show() | |
| def save_file(): | |
| plt.savefig(FILE_OUT, dpi=DPI) | |
| if __name__ == '__main__': | |
| main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
サンプル
data.csv: