1

I'm having some issue getting my code to run. The two lines in question are as follows (the code runs fine without them).

ax1_2 = ax1.twinx()
ax1_2.fill_between(date, 0, (ask-bid), alpha = 3, facecolor='g')

I'm looking to twin their x's and show another plot ontop of my current graph (displaying spread for my data).

Here's my source code. Any help is much appreciated.

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import numpy as np

from matplotlib import style
style.use('ggplot')


def bytespdate2num(fmt, encoding='utf-8'):
        strconverter = mdates.strpdate2num(fmt)
        def bytesconverter(b):
                s = b.decode(encoding)
                return strconverter(s)
        return bytesconverter

date_converter = bytespdate2num("%Y%m%d%H%M%S")

def graphRawFX():
    date,bid,ask=np.loadtxt('GBPUSD1d.txt', unpack=True,
                            delimiter=',',
                            converters = {0: date_converter})
    fig = plt.figure(figsize=(10,7))
    ax1 = plt.subplot2grid((40,40), (0,0), rowspan=40, colspan=40)

    ax1.plot(date,bid)
    ax1.plot(date,ask)
    plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
    # gca() here because we want to offset here

    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
    for label in ax1.xaxis.get_ticklabels():
        label.set_rotation(45)

    ax1_2 = ax1.twinx()
    ax1_2.fill_between(date, 0, (ask-bid), alpha = 3, facecolor='g') #fills betwween date and zero, ask-bid is the spread

    plt.subplots_adjust(bottom=0.23)


    plt.grid(True)
    plt.show()

graphRawFX()
4
  • What exactly is the problem? Could you attach a figure? Commented Sep 4, 2016 at 20:57
  • Sorry, I just realized I hadn't given any information on the error. The left image shows the error, and the right image is the base plot that appears when I comment out the two lines I had mentioned in my original post. imgur.com/a/WNQKk Commented Sep 4, 2016 at 21:16
  • I do not have a computer at hand, but try to use an alpha-value between 0 and 1. 0=no color and 1=full color. Commented Sep 4, 2016 at 21:34
  • 1
    Sometimes, I realize the stupidity of the questions I ask. I meant to assign .3 to alpha, but missed the decimal point... Thanks for the help! If you want to post a solution, I'll give it a vote. Commented Sep 4, 2016 at 21:46

1 Answer 1

1

From our comments to the OP, your alpha value is too big. It has to be between 0 and 1. See here or the doc. What you want is

ax1_2.fill_between(date, 0, (ask-bid), alpha = 0.3, facecolor='g')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.