Skip to content

Instantly share code, notes, and snippets.

@gh640
Created June 1, 2020 09:26
Show Gist options
  • Select an option

  • Save gh640/9858dcf9d149a73cec452e9d656c799f to your computer and use it in GitHub Desktop.

Select an option

Save gh640/9858dcf9d149a73cec452e9d656c799f to your computer and use it in GitHub Desktop.
サンプルコード: matplotlib.pyplot で出力するグラフのサイズを指定する
"""サンプルコード: 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()
@gh640
Copy link
Copy Markdown
Author

gh640 commented Jun 1, 2020

サンプル data.csv:

,2019,2020,2021
Dog,5,3,8
Cat,10,20,15
Panda,3,1,3

@gh640
Copy link
Copy Markdown
Author

gh640 commented Jun 1, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment