How do I align gridlines for two y-axis scales using Matplotlib?

前端 未结 7 1563
遇见更好的自我
遇见更好的自我 2020-11-29 00:16

I\'m plotting two datasets with different units on the y-axis. Is there a way to make the ticks and gridlines aligned on both y-axes?

The first image shows what I ge

7条回答
  •  死守一世寂寞
    2020-11-29 00:55

    I could solve it by deactivating ax.grid(None) in one of the grid`s axes:

    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)))
    ax2 = ax1.twinx()
    ax2.plot(pd.Series(np.random.uniform(10, 20, size=10)), color='r')
    ax2.grid(None)
    
    plt.show()
    

    Figure Result

提交回复
热议问题