1

I have a time series of financial data in a Pandas DataFrame:

Date  Close Volume                              
2003-09-01 8.24890  54344
2003-09-02 8.23245  76655
2003-09-03 8.22710  87655

I would like to plot the price (Close) on the left axis and the volume on the right axis. The latter should be a histogram, while the former should be a normal line-graph.

I have the following code:

    fig, ax = plt.subplots(figsize=(18, 10))
    ax2 = ax.twinx()
    df[instrument]['Close'].plot(ax=ax, style='b-')
    plt.ylabel('Rate')
    df[instrument]['Volume'].hist(ax=ax2)
    plt.ylabel('Volume')
    plt.xlabel('Date')
    plt.plot()

The price comes up fine. The volume does not show up on the right axis. However, I do get some numbers on the right axis, but no histogram. How can I fix this?

Here is an example of what I would like to achieve: enter image description here

Thanks in advance

Oeyvind
  • 357
  • 6
  • 19
  • I don't see how a histogram of some numeric data plotted on the same axis as a line plot with dates as x values would even make sense. Could you explain what you expect to see on the plot? – ImportanceOfBeingErnest Feb 12 '18 at 15:49
  • What I want to achieve is to have the left axis show the price, and the right axis show the volume. As in day one, the price is 100 and the volume is 1000. Day two, price has increased to 110, and the volume shot up as well, to 2000, and so forth... – Oeyvind Feb 12 '18 at 15:57
  • Sure, I'm talking about the horizontal axis here. The line plot would plot values between say january 2003 and december 2004, but the histogram would plot values between 50000 and 90000 or so. It's not even clear if december 2004 is smaller or bigger than 90000, right? Just as you cannot add 4 apples to 5 books. So again, what you expect to see on the plot? – ImportanceOfBeingErnest Feb 12 '18 at 16:02
  • Maybe I am bad at explaining? I have added a picture of what I want to achieve in the question to clarify. – Oeyvind Feb 12 '18 at 16:30
  • Ok, you do not want a [histogram](https://en.wikipedia.org/wiki/Histogram) at all. But a usual bar plot. – ImportanceOfBeingErnest Feb 12 '18 at 16:47

0 Answers0