Question

I would like to plot two candlestick charts representing two different time series that need to be plot with different scales. Following the documentation I assume I should do something like this:

from pylab import * 
from matplotlib.finance import candlestick

price1 = [(734542.0, 1.326, 1.3287, 1.3322, 1.3215), (734543.0, 1.3286, 1.3198, 1.3292, 1.3155), (734546.0, 1.321, 1.3187, 1.3284, 1.3186), (734547.0, 1.3186, 1.3133, 1.3217, 1.308)]
price2 = [(734542.0, 1.5819, 1.5819, 1.5886, 1.5792), (734543.0, 1.5817, 1.5756, 1.5851, 1.5729), (734546.0, 1.578, 1.5766, 1.583, 1.5753), (734547.0, 1.5765, 1.5692, 1.5772, 1.5645)]

fig, ax = subplots()
candlestick(ax,price1,width=0.5)
ax.xaxis_date()
ax.autoscale_view()

ax2 = ax.twinx()
candlestick(ax2,price2,width=0.5)
ax2.xaxis_date()
ax2.autoscale_view()

plt.show()

What I'm trying to get is something like this; however, it's enough running this code to see the two price series are kinda "joint" together not giving the expected effect. Can anyone help me out with this and tell me what I'm doing wrong?

EDIT: basically I don't see the distinction between the two price series. If you would run this code, that plots only the first price series:

from pylab import * 
from matplotlib.finance import candlestick

price1 = [(734542.0, 1.326, 1.3287, 1.3322, 1.3215), (734543.0, 1.3286, 1.3198, 1.3292, 1.3155), (734546.0, 1.321, 1.3187, 1.3284, 1.3186), (734547.0, 1.3186, 1.3133, 1.3217, 1.308)]
price2 = [(734542.0, 1.5819, 1.5819, 1.5886, 1.5792), (734543.0, 1.5817, 1.5756, 1.5851, 1.5729), (734546.0, 1.578, 1.5766, 1.583, 1.5753), (734547.0, 1.5765, 1.5692, 1.5772, 1.5645)]

fig, ax = subplots()
candlestick(ax,price1,width=0.5)
ax.xaxis_date()
ax.autoscale_view()
"""
ax2 = ax.twinx()
candlestick(ax2,price2,width=0.5)
ax2.xaxis_date()
ax2.autoscale_view()
"""
plt.show()

you can see that the difference between the first and the second is almost nothing, apart for some small changes in the values that make me think there's a kind of overlap. How can I keep the two price series distinct and clearly visible they are two different things?

Was it helpful?

Solution

I saw the answer above, but that only works if you know what the data is and can play around with it so it doesn't overlap. What you really need is to play around with the formatting of the bars. I have a silly example below for examples of how. Below I change the colors of one, add transparency, have them at different widths etc.

from Core.Models.Program import Program
from pylab import * 
from matplotlib.finance import candlestick

price1 = [(734542.0, 1.326, 1.3287, 1.3322, 1.3215), (734543.0, 1.3286, 1.3198, 1.3292,    1.3155), (734546.0, 1.321, 1.3187, 1.3284, 1.3186), (734547.0, 1.3186, 1.3133, 1.3217, 1.308)]
price2 = [(734542.0, 1.5819, 1.5819, 1.5886, 1.5792), (734543.0, 1.5817, 1.5756, 1.5851, 1.5729), (734546.0, 1.578, 1.5766, 1.583, 1.5753), (734547.0, 1.5765, 1.5692, 1.5772, 1.5645)]

fig, ax = subplots()
candlestick(ax,price1,width=1,colorup="blue",colordown="orange")
ax.xaxis_date()

ax2 = ax.twinx()

candlestick(ax2,price2,width=.5,colordown="green",alpha=.5)
ax2.xaxis_date()


plt.show()

alpha sets the transparency (0.5 is half visible) colorup,colourdown set the colour (colour up if it starts ends higher than it finishes) width adjusts the width

I don't have time to do it but play around with it until it looks nice.

NOTE: Matplotlib 'paints' the charts in the order you plot them, so the one you plot last will appear on top.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top