0

This is sort of a 2 part question. I have a set of data indexed by date time index:

     DateTime,  ClosedPL,  MaxClosedDraw,   Max,  MaxDrawPerPeriod
1/6/2012 10:52,     -7350,         -20643,     0,                 0
1/6/2012 12:00,         0,         -20643,     0,                 0
1/6/2012 14:09,         0,         -20643,     0,                 0
1/6/2012 14:29,         0,         -20643,     0,                 0
1/6/2012 14:30,         0,         -20643,     0,            -20643
1/8/2012 18:00,         0,              0,   882,                 0
1/9/2012 8:59,          0,           -924,   882,                 0
1/9/2012 9:00,          0,          -1155,   882,                 0
1/9/2012 10:00,         0,          -3423,   882,                 0
1/9/2012 11:03,         0,          -3549,   882,                 0
1/9/2012 12:10,         0,          -3549,   882,                 0
1/9/2012 13:27,         0,          -3549,   882,                 0
1/9/2012 14:17,      3250,          -3549,   882,             -3549
1/9/2012 14:26,         0,              0,  1218,                 0
1/9/2012 14:29,     -1254,          -3318,  1218,                 0
1/9/2012 14:30,         0,          -3318,  1218,                 0
1/9/2012 18:02,         0,          -3654,  1218,             -3654
1/10/2012 8:22,      1244,              0,  6426,                 0
1/10/2012 9:00,         0,          -1869,  6426,                 0
1/10/2012 9:37,         0,          -2856,  6426,                 0
1/10/2012 10:00,        0,          -3276,  6426,                 0 

I'm trying to create two plots on the same figure - 1 line chart showing the closedPL and one of the MaxDrawPerPeriod represented by a bar graph. X axis would be the date time index. I'd like the bar chart to run along bottom of the graph but with limited height so as not to really interfere with the line chart. So part 1 would be how to add to that the chart and part 2 is below:

stats_df.plot(kind='line', y='ClosedPL_Accum')
stats_df.plot(kind='bar', y='MaxDrawPerPeriod')
plt.show()

For some reason - I can't get the bar chart to run correctly, even when I run it on it's own. This is what it looks like and it takes 10 min just to produce this. Am i doing something wrong wtih my code? enter image description here

novawaly
  • 1,051
  • 3
  • 12
  • 27
  • im so new to plotting that sounds like a different language. Could you possibly point me to some documentation on that? – novawaly Nov 27 '18 at 22:25

1 Answers1

1

You are creating as many bars as you have lines in your dataframe which seem to be a lot. Each has its own label, such that in total it becomes unreadable. For the kind of plot you are looking for you will need to use matplotlib. You will need to convert your index to datetime if it isn't already and plot a bar plot. Then create a twin axes axtwin = ax.twinx(), and plot your line plot to it. Or vice versa.

import matlotlib.pyplot as plt

fig, ax = plt.subplots()

ax.bar(df.index, df['MaxDrawPerPeriod'])
axtwin = ax.twinx()
axtwin.plot(df.index, df['ClosedPL_Accum'])

ax.set_ylim(...)     # you need to set the limits to the numbers you need
axtwin.set_ylim(...)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Ok so this is getting much closer. Ive got them on the same chart now and I see the 2 scales on each side. Can you explain what the ylim does? I know there is a value thats -160,000 but largest bar seems to only go to -60k. When i change the ylim, the axis values will change but the largest bar doesn't seem to change in size – novawaly Nov 27 '18 at 22:54
  • `ylim` sets the limits of the vertical axis. If you want it to go from -160000 to 0, you'd write `ax.set_ylim(-160000, 0)`. – ImportanceOfBeingErnest Nov 27 '18 at 22:57
  • thank you for the help. Accepted as correct - only other thing i'll ask is do you know of any other charting libraries that would have a tracker where i can hover over the data and have it give me the value at the cursor? – novawaly Nov 27 '18 at 23:05
  • With matplotlib you can either [write that yourself](https://stackoverflow.com/questions/50560525/i-want-to-display-the-values-of-x-and-y-while-hover-mouse-over-the-bar-graph), or use the [mpldatacursor](https://github.com/joferkington/mpldatacursor) or [mplcursors](https://mplcursors.readthedocs.io/en/stable/) package. – ImportanceOfBeingErnest Nov 27 '18 at 23:08
  • I'm actually still having some issues with the bar portion of this. I posted in another question so I can show it visually if you dont mind taking a quick peek – novawaly Nov 27 '18 at 23:15
  • https://stackoverflow.com/questions/53509681/scaling-issue-on-a-barchart – novawaly Nov 27 '18 at 23:15