Barplot/line plot on same plot, but different axis and line plot in front of barplot

前端 未结 1 535
悲&欢浪女
悲&欢浪女 2021-01-18 10:30

I\'m using pandas to plot some data.

If I plot this:

import pandas as pd
import matplotlib.pyplot as plt

df          


        
相关标签:
1条回答
  • 2021-01-18 11:15

    you could put line on primary axis.

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({'a': [100, 200, 150, 175],
                       'b': [430, 30, 20, 10]})
    fig, ax1 = plt.subplots(figsize=(15, 10))
    df['b'].plot(kind='bar', color='y')
    df['a'].plot(kind='line', marker='d', secondary_y=True)
    

    Or, create two axes ax1 and ax2 with twinx().

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({'a': [100, 200, 150, 175],
                       'b': [430, 30, 20, 10]})
    fig, ax1 = plt.subplots(figsize=(15, 10))
    ax2 = ax1.twinx()
    df['b'].plot(kind='bar', color='y', ax=ax1)
    df['a'].plot(kind='line', marker='d', ax=ax2)
    ax1.yaxis.tick_right()
    ax2.yaxis.tick_left()
    

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