How to get a matplotlib Axes instance to plot to?

前端 未结 2 1714
滥情空心
滥情空心 2020-12-03 00:09

I need to make a candlestick chart (something like this) using some stock data. For this I want to use the function matplotlib.finance.candlestick(). To this function I need

相关标签:
2条回答
  • 2020-12-03 01:06

    Use the gca ("get current axes") helper function:

    ax = plt.gca()
    

    Example:

    import matplotlib.pyplot as plt
    import matplotlib.finance
    quotes = [(1, 5, 6, 7, 4), (2, 6, 9, 9, 6), (3, 9, 8, 10, 8), (4, 8, 8, 9, 8), (5, 8, 11, 13, 7)]
    ax = plt.gca()
    h = matplotlib.finance.candlestick(ax, quotes)
    plt.show()
    

    enter image description here

    0 讨论(0)
  • 2020-12-03 01:07

    You can either

    fig, ax = plt.subplots()  #create figure and axes
    candlestick(ax, quotes, ...)
    

    or

    candlestick(plt.gca(), quotes) #get the axis when calling the function
    

    The first gives you more flexibility. The second is much easier if candlestick is the only thing you want to plot

    0 讨论(0)
提交回复
热议问题