0

I am testing the capabilities of pandas to plot financial data (price and volume) on the same chart. If I try to render both data as lines, it works fine:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

a = pd.date_range('2019-01-01', '2019-06-01',freq = 'D')
b = np.random.normal(size = len(a), loc = 50)
v = np.random.normal(size = len(a), loc = 1000)
c = pd.DataFrame(index = a, data = zip(b,v), columns = ['price', 'volume'])

fig, ax = plt.subplots(figsize = (15,8))
bx = ax.twinx()
c.price.plot.line(ax = ax, color = 'r')
c.volume.plot.line(ax = bx,  color = 'g', alpha = .2)
plt.show()

This gives: enter image description here

However if I try to render one as a line and the other as a bar chart, by replacing the 3 last lines by:

c.price.plot.line(ax = ax, color = 'r')
c.volume.plot.bar(ax = bx,  color = 'g', alpha = .2)
plt.show()

This gives the wrong result: enter image description here

Would anybody know how to make the above code work with line + bar ??

jim jarnac
  • 4,804
  • 11
  • 51
  • 88
  • 1
    This is a pretty common question, the last version of it appeared only [yesterday](https://stackoverflow.com/questions/59577455/pandas-squished-plot-on-one-axis). In short: Use matplotlib, not pandas. – ImportanceOfBeingErnest Jan 04 '20 at 18:56

1 Answers1

3

Use Matplotlib plotting library.

Matplotlib's function pyplot has functions bar and plot. You can use them to display data on the same chart.

Example