【问题标题】:Plotting points on one line in python. 1 dimension在 python 中的一条线上绘制点。 1维
【发布时间】:2022-11-24 14:49:50
【问题描述】:

这是我想绘制的那种图表,没有 y axis 。如果可能的话,我如何使用 matplotlib 在 python 中实现这一点。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    遗憾的是,matplotlib 中没有内置函数来创建这样的图表。

    但是,您可以使用以下代码获得类似的输出。此 sn-p 正在删除不需要的脊柱(左、右和上),然后使用散点图模拟一维图形。

    如下:

    from matplotlib import pyplot as plt
    import numpy as np
    fig, ax = plt.subplots(figsize=(10,1))
    
    x = [1,2,3,4,9,10]
    idx = np.arange(1,len(x)+1)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_position('zero')
    ax.spines['bottom'].set_alpha(0.2)
    ax.get_yaxis().set_visible(False)
    ax.set_xlabel('Gene 1')
    ax.scatter(x, np.zeros(len(x)), s=300, c='lightgreen')
    ax.set_xticks([min(x), max(x)], ['Low Values', 'High Values'])
          
    for i in range(len(idx)):
        ax.annotate(idx[i], (x[i], 0), textcoords="offset points",
                    xytext=(0,0), # distance from text to points (x,y)
                    ha='center')
    plt.show()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多