0

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

2
  • 1
    I understand what you want is basically this with a couple changes (mainly the common X axis labels), isn't it? Commented Feb 12, 2012 at 17:15
  • @RicardoCárdenes: Yes, your understanding is correct. The 'volume' chart will basically be vertical bars (looks like the chart below the image in the link you provided) Commented Feb 12, 2012 at 19:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This almost works, but I notices two 'quirks': 1). It overwrites the bottom part of the existing chart. 2). There are two inexplicable gaps in the volume chart - one at the begining of the volume chart and the other at the end of the volume chart, which seems to suggest that the X axis scaling is different, since the volume chart data starts later (and finishes earlier) than the chart in the top (main) chart
Updated the snippet to use the same x data in both plots, which should remedy quirk 2. I can't reproduce quirk 1. as the fig.subplots_adjust(hspace=0.5) ensures that the vertical space between the two subplots is enough to avoid overwriting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.