import csv import matplotlib.pyplot as plt import matplotlib.ticker as ticker import matplotlib.font_manager as mfontm import matplotlib.dates as mdates import datetime
DATA_DIR = "./BME280log/" LOG_NAME = " BME280.log" PIC_DIR = "./BME280log/" PIC_NAME = " BME280.png" PIC_SIZE = (4.8, 3.2) LINE_WIDTH = 1 DAYS_AGO = 0
timestamps = [] tempertures = [] humidities = [] pressures = [] date = "{0:%Y.%m.%d}".format(datetime.date.today() - datetime.timedelta(days=DAYS_AGO)) log_filename = DATA_DIR + date + LOG_NAME with open(log_filename, "r") as f: reader = csv.reader(f) for row in reader: timestamps.append(datetime.datetime.strptime(row[0] ,"%Y/%m/%d %H:%M:%S")) tempertures.append(float(row[1])) humidities.append(float(row[2])) pressures.append(float(row[3]))
fp = mfontm.FontProperties(fname="./ipag.ttf")
fig = plt.figure(figsize=PIC_SIZE) plt.subplots_adjust(left=0.07, bottom=0.15, top=0.9, right=1) ax1 = fig.add_subplot(111) ax2 = ax1.twinx() ax3 = ax1.twinx() ax1.plot(timestamps, tempertures, color='orange', alpha=1.0, lw=LINE_WIDTH, antialiased=True) ax2.plot(timestamps, humidities, color='deepskyblue', alpha=0.6, lw=LINE_WIDTH, antialiased=True) ax3.plot(timestamps, pressures, color='green', alpha=0.8, lw=LINE_WIDTH, antialiased=True)
ax1.set_ylim(0, 40) ax2.set_ylim(0, 100) ax3.set_ylim(960, 1020) ax1.yaxis.set_major_locator(ticker.MultipleLocator(5)) ax2.yaxis.set_major_locator(ticker.MultipleLocator(10)) ax3.yaxis.set_major_locator(ticker.MultipleLocator(10))
fig.subplots_adjust(right=0.80) ax3.spines["right"].set_position(("axes", 1.13))
hoursfmt = mdates.DateFormatter("%H:%M") ax1.xaxis.set_major_formatter(hoursfmt) xlabels = ax1.get_xticklabels() plt.setp(xlabels, rotation=45)
ax1.yaxis.set_label_coords(-0.045, 1.03) ax2.yaxis.set_label_coords(1.07, 1.09) ax3.yaxis.set_label_coords(1.21, 1.09) ax1.set_ylabel("(℃)", fontproperties=fp, rotation=0) ax2.set_ylabel("(%)", fontproperties=fp, rotation=0) ax3.set_ylabel("(hPa)", fontproperties=fp, rotation=0)
fig.legend(["温度", "湿度", "気圧"], bbox_to_anchor=(0, 0), bbox_transform=ax1.transAxes, loc="lower left", prop=fp)
pic_filename = PIC_DIR + date + PIC_NAME plt.savefig(pic_filename)
|