How can I make a barplot and a lineplot in the same seaborn plot with different Y axes nicely?

后端 未结 2 368
耶瑟儿~
耶瑟儿~ 2021-01-12 05:51

I have two different sets of data with a common index, and I want to represent the first one as a barplot and the second one as a lineplot in the same graph. My current appr

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 06:38

    You have to remove grid lines of the second axis. Add to the code ax2.grid(False). However y-ticks of the second axis will be not align to y-ticks of the first y-axis, like here:

    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    import pandas as pd
    
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(pd.Series(np.random.uniform(0,1,size=10)), color='g')
    ax2 = ax1.twinx()
    ax2.plot(pd.Series(np.random.uniform(0,17,size=10)), color='r')
    ax2.grid(False)
    plt.show()
    

提交回复
热议问题