Skip to content

Commit 3f063b4

Browse files
finish up fill_between tests; deepcopy to not modify input
1 parent e1cc573 commit 3f063b4

File tree

4 files changed

+85
-34
lines changed

4 files changed

+85
-34
lines changed

examples/fill_between.ipynb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@
745745
"\n",
746746
"fb_macd_up = dict(y1=macd.values,y2=signal.values,where=signal<macd,color=\"#93c47d\",alpha=0.6,interpolate=True)\n",
747747
"fb_macd_dn = dict(y1=macd.values,y2=signal.values,where=signal>macd,color=\"#e06666\",alpha=0.6,interpolate=True)\n",
748-
"#fb_macd_up['panel'] = 1\n",
749-
"#fb_macd_dn['panel'] = 1\n",
748+
"fb_macd_up['panel'] = 1\n",
749+
"fb_macd_dn['panel'] = 1\n",
750750
"\n",
751751
"fb_macd = [fb_macd_up,fb_macd_dn]\n",
752752
"\n",
@@ -755,19 +755,26 @@
755755
" mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,\n",
756756
" color='dimgray',alpha=0.65,secondary_y=True),\n",
757757
" mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=False),\n",
758-
" mpf.make_addplot(signal,panel=1,color='b',secondary_y=False,fill_between=fb_macd),\n",
758+
" mpf.make_addplot(signal,panel=1,color='b',secondary_y=False)#,fill_between=fb_macd),\n",
759759
" ]\n",
760760
"\n",
761761
"s = mpf.make_mpf_style(base_mpf_style='blueskies',facecolor='aliceblue')#,rc={'figure.facecolor':'lightcyan'})\n",
762762
"\n",
763763
"mpf.plot(df,type='candle',addplot=apds,figscale=1.6,figratio=(1,1),title='\\n\\nMACD',\n",
764764
" style=s,volume=True,volume_panel=2,panel_ratios=(3,4,1),tight_layout=True,\n",
765-
" fill_between=fb_exp12)\n",
765+
" fill_between=fb_macd+fb_exp12)\n",
766766
"\n",
767767
"\n",
768768
"\n"
769769
]
770770
},
771+
{
772+
"cell_type": "code",
773+
"execution_count": null,
774+
"metadata": {},
775+
"outputs": [],
776+
"source": []
777+
},
771778
{
772779
"cell_type": "code",
773780
"execution_count": null,

src/mplfinance/plotting.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,7 @@ def plot( data, **kwargs ):
703703
# fill_between is NOT supported for external_axes_mode
704704
# (caller can easily call ax.fill_between() themselves).
705705
if config['fill_between'] is not None and not external_axes_mode:
706-
fblist = config['fill_between']
707-
panid = config['main_panel']
706+
fblist = copy.deepcopy(config['fill_between'])
708707
if _num_or_seq_of_num(fblist):
709708
fblist = [dict(y1=fblist),]
710709
elif isinstance(fblist,dict):
@@ -714,10 +713,11 @@ def plot( data, **kwargs ):
714713
for fb in fblist:
715714
if 'x' in fb:
716715
raise ValueError('fill_between dict may not contain `x`')
716+
panid = config['main_panel']
717717
if 'panel' in fb:
718718
panid = fb['panel']
719719
del fb['panel']
720-
fb['x'] = xdates
720+
fb['x'] = xdates # add 'x' to caller's fb dict
721721
ax = panels.at[panid,'axes'][0]
722722
ax.fill_between(**fb)
723723

@@ -1063,13 +1063,18 @@ def _addplot_apply_supplements(ax,apdict,xdates):
10631063
ax.set_yscale(ysd)
10641064
# added by Wen
10651065
if "fill_between" in apdict and apdict['fill_between'] is not None:
1066-
fblist = apdict['fill_between']
1066+
# deep copy because mplfinance code sometimes modifies the fill_between dict
1067+
fblist = copy.deepcopy(apdict['fill_between'])
10671068
if isinstance(fblist,dict):
10681069
fblist = [fblist,]
10691070
if _list_of_dict(fblist):
10701071
for fb in fblist:
1071-
fb['x'] = xdates
1072+
if 'x' in fb:
1073+
raise ValueError('fill_between dict may not contain `x`')
1074+
fb['x'] = xdates # add 'x' to caller's fb dict
10721075
ax.fill_between(**fb)
1076+
else:
1077+
raise ValueError('Invalid addplot fill_between: must be `dict` or `list of dict`')
10731078

10741079
def _set_ylabels_side(ax_pri,ax_sec,primary_on_right):
10751080
# put the primary axis on one side,
-53 KB
Loading

tests/test_fill_between.py

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -163,35 +163,74 @@ def test_fill_between06(bolldata):
163163
signal = macd.ewm(span=9, adjust=False).mean()
164164
histogram = macd - signal
165165

166-
fb_12up = dict(y1=exp12.values,y2=exp26.values,where=exp12>exp26,color="#93c47d",alpha=0.6,interpolate=True)
167-
fb_12dn = dict(y1=exp12.values,y2=exp26.values,where=exp12<exp26,color="#e06666",alpha=0.6,interpolate=True)
166+
fb_12up = dict(y1=exp12.values,y2=exp26.values,where=(exp12>exp26).values,color="#93c47d",alpha=0.6,interpolate=True)
167+
fb_12dn = dict(y1=exp12.values,y2=exp26.values,where=(exp12<exp26).values,color="#e06666",alpha=0.6,interpolate=True)
168168
fb_exp12 = [fb_12up,fb_12dn]
169169

170-
fb_macd_up = dict(y1=macd.values,y2=signal.values,where=signal<macd,color="#93c47d",alpha=0.6,interpolate=True)
171-
fb_macd_dn = dict(y1=macd.values,y2=signal.values,where=signal>macd,color="#e06666",alpha=0.6,interpolate=True)
172-
#fb_macd_up['panel'] = 1
173-
#fb_macd_dn['panel'] = 1
170+
fb_macd_up = dict(y1=macd.values,y2=signal.values,where=(signal<macd).values,color="#93c47d",alpha=0.6,interpolate=True)
171+
fb_macd_dn = dict(y1=macd.values,y2=signal.values,where=(signal>macd).values,color="#e06666",alpha=0.6,interpolate=True)
174172

175173
fb_macd = [fb_macd_up,fb_macd_dn]
176174

177-
apds = [mpf.make_addplot(exp12,color='lime'),
178-
mpf.make_addplot(exp26,color='c'),
179-
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,
180-
color='dimgray',alpha=0.65,secondary_y=True),
181-
mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=False),
182-
mpf.make_addplot(signal,panel=1,color='b',secondary_y=False,fill_between=fb_macd),
183-
]
175+
s = mpf.make_mpf_style(base_mpf_style='blueskies',facecolor='aliceblue')
176+
177+
import pprint
178+
pp = pprint.PrettyPrinter(indent=4)
179+
180+
for tag in ['a','b','c']:
181+
apds = [mpf.make_addplot(exp12,color='lime'),
182+
mpf.make_addplot(exp26,color='c'),
183+
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,
184+
color='dimgray',alpha=0.65,secondary_y=True),
185+
mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=False),
186+
mpf.make_addplot(signal,panel=1,color='b',secondary_y=False)
187+
]
188+
189+
new_tname = tname[0:-4]+tag+tname[-4:]
190+
if tag == 'a':
191+
print('fb_exp12')
192+
pp.pprint(fb_exp12)
193+
print('fb_macd')
194+
pp.pprint(fb_macd)
195+
apds[ 0] = mpf.make_addplot(exp12,color='lime',fill_between=fb_exp12)
196+
apds[-1] = mpf.make_addplot(signal,panel=1,color='b',secondary_y=False,fill_between=fb_macd)
197+
mpf.plot(df,type='candle',addplot=apds,figscale=0.8,figratio=(1,1),title='\n\nMACD',
198+
style=s,volume=True,volume_panel=2,panel_ratios=(3,4,1),tight_layout=True,
199+
savefig=new_tname)
200+
elif tag == 'b':
201+
print('fb_exp12')
202+
pp.pprint(fb_exp12)
203+
print('fb_macd')
204+
pp.pprint(fb_macd)
205+
apds[ 0] = mpf.make_addplot(exp12,color='lime')
206+
apds[-1] = mpf.make_addplot(signal,panel=1,color='b',secondary_y=False,fill_between=fb_macd)
207+
mpf.plot(df,type='candle',addplot=apds,figscale=0.8,figratio=(1,1),title='\n\nMACD',
208+
style=s,volume=True,volume_panel=2,panel_ratios=(3,4,1),tight_layout=True,
209+
fill_between=fb_exp12,
210+
savefig=new_tname)
211+
elif tag == 'c':
212+
apds[ 0] = mpf.make_addplot(exp12,color='lime')
213+
apds[-1] = mpf.make_addplot(signal,panel=1,color='b',secondary_y=False)
214+
fb_macd[0]['panel'] = 1
215+
fb_macd[1]['panel'] = 1
216+
print('fb_exp12')
217+
pp.pprint(fb_exp12)
218+
print('fb_macd')
219+
pp.pprint(fb_macd)
220+
print('fb_macd+fb_exp12')
221+
pp.pprint(fb_macd+fb_exp12)
222+
mpf.plot(df,type='candle',addplot=apds,figscale=0.8,figratio=(1,1),title='\n\nMACD',
223+
style=s,volume=True,volume_panel=2,panel_ratios=(3,4,1),tight_layout=True,
224+
fill_between=fb_macd+fb_exp12,
225+
savefig=new_tname)
226+
else:
227+
print('Should NEVER get to here!')
228+
raise ValueError('Should NEVER get to here!')
184229

185-
s = mpf.make_mpf_style(base_mpf_style='blueskies',facecolor='aliceblue')#,rc={'figure.facecolor':'lightcyan'})
186-
187-
mpf.plot(df,type='candle',addplot=apds,figscale=1.6,figratio=(1,1),title='\n\nMACD',
188-
style=s,volume=True,volume_panel=2,panel_ratios=(3,4,1),tight_layout=True,
189-
fill_between=fb_exp12,
190-
savefig=tname)
191-
192-
_report_file_sizes(tname,rname)
230+
_report_file_sizes(new_tname,rname)
231+
232+
result = compare_images(rname,new_tname,tol=IMGCOMP_TOLERANCE)
233+
if result is not None:
234+
print('result=',result)
235+
assert result is None
193236

194-
result = compare_images(rname,tname,tol=IMGCOMP_TOLERANCE)
195-
if result is not None:
196-
print('result=',result)
197-
assert result is None

0 commit comments

Comments
 (0)