Python:Matplotlib twin()问题:密谋传播

时间:2016-09-04 19:56:17

标签: python matplotlib

我在运行代码时遇到了一些问题。有问题的两行如下(没有它们的代码运行正常)。

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

我正在寻找双胞胎的x并在我当前的图表上显示另一个情节(显示我的数据的差价)。

这是我的源代码。任何帮助深表感谢。

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()

1 个答案:

答案 0 :(得分:1)

从我们对OP的评论中,您的alpha值过大。它必须介于0和1之间。请参阅heredoc。你想要的是

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