【问题标题】:Matplotlib, I'm making a 2d histogram and I want to a triangular axes extension similar to what is done with colorbarsMatplotlib,我正在制作一个 2d 直方图,我想要一个类似于使用颜色条所做的三角轴扩展
【发布时间】:2016-11-03 02:20:58
【问题描述】:

使用 MATPLOTLIB.PYPLOT 我正在制作一个 2d 直方图,我想将我的视图限制在某个范围的 y 值,但同时我想将所有值聚集在 y 底部的某个数字以下 -轴。我在想的类似于如何使颜色条具有三角形扩展,并且这些扩展中的任何内容都被绘制到特定条件。我想做的是类似的,只是它涉及到 y 轴。

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    我能想到的最简单的实现方法是在调用直方图之前过滤您的数据。这是一个使用np.where 过滤y数据的示例。

    import numpy as np; np.random.seed(17)
    import matplotlib.pyplot as plt
    
    # generate some random normal data
    N = 100000
    x = np.random.normal(-2, 3, N) 
    y = np.random.normal(3, 6, N) 
    
    # use np.where to set all y values less than -10 to -10
    y = np.where(y < -10, -10, y)
    
    # now apply histogram2d
    xbins = np.linspace(-10, 10, 101)
    ybins = np.linspace(-10, 10, 101)
    
    H, xedges, yedges = np.histogram2d(y, x, bins=(xbins, ybins))
    X, Y = np.meshgrid(xedges, yedges)
    
    # plot data with pcolormesh
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    img = ax.pcolormesh(X, Y, H)
    
    ax.set_xlim(xedges.min(), xedges.max())
    ax.set_ylim(yedges.min(), yedges.max())
    
    plt.colorbar(img)
    

    你可以在y轴底部看到所有阈值的分布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多