python - Matplotlib - Formatting two plots on the same figure -


i'm trying plot data i'm getting stuck on plotting 2 plots on same figure. looks this:

enter image description here

the code is:

import re import sqlite3 import matplotlib.pyplot plt matplotlib.dates import datetime dt matplotlib.dates import dateformatter  ...  company in companies:     cursor.execute("select distinct url t_surv_data company = ? order product_type", (company,))     urls = [r[0] r in cursor.fetchall()]      idx, url in enumerate(urls):                   cursor.execute("select price, timestamp t_surv_data url = ? order timestamp", (url,))     data = [[r[0], r[1]] r in cursor.fetchall()]     price, date = zip(*data)                     date = [dt.datetime.strptime(d, '%y-%m-%d %h:%m:%s') d in date]      f = plt.figure('''figsize=(3, 2)''')      ax = f.add_subplot(111)     ax.plot(date, price) # x, y     ax.xaxis.set_major_formatter(dateformatter('%d\n%h\n%y'))     #ax.set_ylim(ymin=0) # if use break plot      ax2 = f.add_subplot(211)     ax2.scatter(date, [1,1,-1])     ax2.xaxis.set_major_formatter(dateformatter('%d\n%h\n%y'))     #ax2.set_ylim(ymin=-1, ymax=1) # if use break plot      plt.savefig('plt/foo' + str(idx) + '.png')     plt.close() 

how can solve questions:

1 - plots looks 1 above other. how can format visual independent plots on same figure.

2 - i'm using line of code both plots "ax2.xaxis.set_major_formatter(dateformatter('%d\n%h\n%y'))" there no sync in dates. dates should equal in 2 plots.

some 1 can give me clue on questions?

best regards,

you not using add_subplot correctly:

ax = f.add_subplot(2,1,1) ax2 = f.add_subplot(2,1,2) 

the first number indicates number of rows, second number of columns , third index of plot.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -