Homunculus Reticulli
Homunculus Reticulli

Reputation: 68366

adding a subplot to a matplotlib graph

I am using the snippet given here as the starting point for a script. I want to display a volume chart at the bottom of the first (i.e. main) chart. The volume subchart is basically plotted vertical bars.

Ideally, the date (i.e. X axis) labels will be underneath the volume subplot - i.e. the same date (X) axis is used for both the top (main) chart and the subplot. However, if it makes life easier (for anyone submitting a snippet), I can live with a volume subchart (with or without its own X axis date labels).

I find the matplotlib documentation and scattered tutorials very confusing. A link to an example where this kind of graphing is done (or a snippet posted here) will be very useful

Upvotes: 0

Views: 5776

Answers (1)

Appleman1234
Appleman1234

Reputation: 16076

Modify the ax declaration of the subplot to

ax = fig.add_subplot(211)

and comment out the minor_formatter declaration of #ax.xaxis.set_minor_formatter(dayFormatter)

and append the following before the show() call

fig.subplots_adjust(hspace=0.5)
ay = fig.add_subplot(212)
ay.xaxis.set_major_locator(mondays)
ay.xaxis.set_minor_locator(alldays)
ay.xaxis.set_major_formatter(weekFormatter)
dates = [ x[0] for x in quotes]
volumes = [ x[-1] for x in quotes]
ay.bar(dates,volumes,0.35)

This gives a volume subchart with its own X axis date labels

Upvotes: 1

Related Questions