Home>

I created a candlestick chart using matplotlib.pyplot in python3.
An error has occurred when trying to add a moving average line to the same image.

  fig = plt.figure (figsize = (8, 12), face color ='# 101010')
    ax1 = fig.add_subplot (211, face color ='#cccccc')
    ax2 = fig.add_subplot (212, facecolor ='# bbbbbb')
    ax1.set_title ('chart:'+ pair, color ='# 000000')
    ax1.grid ()
    ax2.set_title ('RSI', color ='# 000000')
    ax2.grid ()
    mpf.candlestick2_ochl (ax1, opens = hist1min ['oMid'], closes = hist1min ['cMid'], highs = hist1min ['hMid'], lows = hist1min ['lMid'], width = 0.5, color up ='k ', colordown ='r', alpha = 0.75)
    ax1.plot (hist1min.index, hist1min ['up2000'])
    ## error ## ax1.plot (hist1min.index, hist1min ['cMid']. Rolling (4) .mean ())

    ax1.set_xticklabels ([(hist1min.index [int (x)]. strftime ("% H:% M") if x<hist1min.shape [0] else x) for x in ax1.get_xticks ()], rotation = 30)
    ax2.set_xticklabels ([(hist1min.index [int (x)]. strftime ("% H:% M") if x<hist1min.shape [0] else x) for x in ax2.get_xticks ()], rotation = 30)
    bottom, top = ax1.get_ylim ()
    ax1.set_ylim (bottom-(top --bottom)/4, top)
    ax1b = ax1.twinx ()
    volume_overlay (ax1b, hist1min ['oMid'], hist1min ['cMid'], hist1min ['tickqty'], width = 0.5, colorup = "g", colordown = "g")
    ax1b.set_ylim ([0, hist1min ['tickqty']. max () * 4])
    ax1b.set_ylabel ('volume')
    with plt.style.context ('grayscale'):
         plt.savefig ('zzz_graph.png', transparent = False)
    plt.close ()

If i do not enter the commented out part with the above ## error ##, you can make a graph, but if you execute the commented part, the following error will occur.

Traceback (most recent call last):
  File "05_gazo.py", line 463, in<module>
    getLast (cnt)
  File "05_gazo.py", line 243, in getLast
    ax1.set_xticklabels ([(hist1min.index [int (x)]. strftime ("% H:% M") if x<hist1min.shape [0] else x) for x in ax1.get_xticks ()], rotation = 30)
  File "/usr/bin/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 3191, in get_xticks
    return self.xaxis.get_ticklocs (minor = minor)
  File "/usr/bin/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/axis.py", line 1329, in get_ticklocs
    return self.major.locator ()
  File "/usr/bin/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/dates.py", line 1249, in __call__
    self.refresh ()
  File "/usr/bin/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/dates.py", line 1269, in refresh
    dmin, dmax = self.viewlim_to_dt ()
  File "/usr/bin/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/dates.py", line 1026, in viewlim_to_dt
    .format (vmin))
ValueError: view limit minimum -36854.15829861111 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

I made it with reference to the following site, but I can't find a workaround.
https://note.nkmk.me/python-pandas-matplotlib-candlestick-chart/

Sorry to trouble you, but if you can understand the solution, I would appreciate it if you could teach me.

  • Answer # 1

    I was able to respond with the following.
    Is it because there is no value for 20 in bb calculation?

    ax1.plot (range (len (hist1min)), hist1min ['up2000'], color = "# ff00ff")
    ax1.plot (range (len (hist1min)), hist1min ['dn2000'], color = "# ff00ff")