Plots coming out empty with pandas.plot

前端 未结 1 1122
孤街浪徒
孤街浪徒 2021-01-20 06:47

I am trying to plot a df that must have two y-axes. I can get the plot to work using only one axis, but when I use two it comes out empty. I\'ve tried separating into two se

相关标签:
1条回答
  • 2021-01-20 07:08

    It looks like you're explicitly plotting them both on the same axes. You've made a new figure and a second axes called ax2, but you're plotting the second dataframe on the first axes by calling df2.plot(..., ax=ax) instead of df2.plot(..., ax=ax2)

    As a simplified example, you're basically doing:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # Generate some placeholder data
    df1 = pd.DataFrame(np.random.random(10))
    df2 = pd.DataFrame(np.random.random(10))
    
    fig, ax = plt.subplots()
    df1.plot(ax=ax)
    
    fig, ax2 = plt.subplots()
    df2.plot(ax=ax)
    
    plt.show()
    

    When you want something more like:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # Generate some placeholder data
    df1 = pd.DataFrame(np.random.random(10))
    df2 = pd.DataFrame(np.random.random(10))
    
    fig, ax = plt.subplots()
    df1.plot(ax=ax)
    
    fig, ax2 = plt.subplots()
    df2.plot(ax=ax2) # Note that I'm specifying the new axes object
    
    plt.show()
    
    0 讨论(0)
提交回复
热议问题