Altering height range of matplotlib histogram2d

后端 未结 1 1010
孤独总比滥情好
孤独总比滥情好 2021-01-27 00:56

I am trying to plot some 2D empirical probability distributions using matplotlib\'s histogram2d. I want the colours to be on the same scale across several different plots, but c

相关标签:
1条回答
  • 2021-01-27 01:09

    In general, the color scaling of most things in matplotlib is controlled by the vmin and vmax keyword arguments.

    You have to read between the lines a bit, but as the documentation mentions, additional kwargs in hist2d are passed on to pcolorfast. Therefore, you can specify the color limits through the vmin and vmax kwargs.

    For example:

    import numpy as np
    import matplotlib.pyplot as plt
    
    small_data = np.random.random((2, 10))
    large_data = np.random.random((2, 100))
    
    fig, axes = plt.subplots(ncols=2, figsize=(10, 5), sharex=True, sharey=True)
    
    # For consistency's sake, we'll set the bins to be identical
    bins = np.linspace(0, 1, 10)
    
    axes[0].hist2d(*small_data, bins=bins, vmin=0, vmax=5)
    axes[1].hist2d(*large_data, bins=bins, vmin=0, vmax=5)
    
    plt.show()
    

    enter image description here

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