Python matplotlib subplot 複数のグラフを表示する

参考:

from pylab import *
x = arange(1, 10, 0.1)
y1 = sin(x)
y2 = cos(x)

# 上の行のグラフ
subplot(211)
plot(x, y1)
xlabel("1") # 上の行のxlabel

# 下の行のグラフ
subplot(212)
plot(x, y2)
xlabel("2") # 下の行のylabel

show()

matplotlibのグラフの体裁を整える - たこ焼き食べた.net

 

発展

参考リンク:

pylab_examples example code: subplots_demo.py — Matplotlib 1.4.3 documentation

 

例:

import MySQLdb
from pylab import *
import sys
import numpy as np
import matplotlib.pyplot as plt
import datetime
from matplotlib.dates import YearLocator, MonthLocator, DayLocator, DateFormatter

connection = MySQLdb.connect(host="localhost", db="agemono", user="root", passwd="password", charset="utf8")
cursor= connection.cursor()
d = "select avg_sales+abs(avg_destruction), date from centerinvicinityd"
cursor.execute(d)
datad = cursor.fetchall()


proda =
prodd =

datestring=[]

print 1212
years = YearLocator() # every year
months = MonthLocator() # every month
monthsFmt = DateFormatter('%m')
days = DayLocator()

for row in datad:
prodd.append(row[0])
dateeach = datetime.datetime.strptime(row[1], '%m/%d/%y')
datestring.append(dateeach)

print 2222


subplot(211)
axis([min(datestring), max(datestring), min(prodd), max(prodd)])

print 33333

fig, ax = plt.subplots()
ax.plot_date(datestring, prodd, '-')
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
ax.xaxis.set_minor_locator(days)


print 44444444
xlabel("center in vicinity d")

print 5555

 

subplot(212)
print 6666
a = "select avg_sales+abs(avg_destruction), date from minatoa"
cursor.execute(a)
dataa = cursor.fetchall()
for row in dataa:
proda.append(row[0])

print 777777

axis([min(datestring), max(datestring), min(proda), max(proda)])
fig, ax = plt.subplots()
ax.plot_date(datestring, proda, '-')
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
ax.xaxis.set_minor_locator(days)

print 88888


xlabel("minato a")


# title = "Prod over Date in Areas"
# title(title)
grid(True)
plt.show()

cursor.close()
connection.close()

 

f:id:haruokny:20150817104248p:plain

f:id:haruokny:20150817104248p:plain