【问题标题】:Plotting a graph with matplotlib where X axis values are not evenly spread使用 matplotlib 绘制 X 轴值分布不均匀的图形
【发布时间】:2015-08-22 00:24:12
【问题描述】:

我使用DataFrame 中的两列在matplotlib 中绘制图表。两列都有 568 个值。用于 X 轴 (colA) 的 DataFrame 列的值从 1.12 开始,到 13.11 结束,但是,X 轴的大部分值介于 1.12 和 2.84 之间 - 大约 415 个值568

我在matplotlib 中创建了一个简单的折线图,其代码和输出如下:

# Plot two columns in dataframe, which have 568 values each    
plt.plot(df.colA,df.colB,lw=2.5) 

# Array for x axis, which is col.A at set intervals 
x_axis_array = [ 1.12  1.2  1.24  1.28  1.33  1.38  1.44  1.5  1.57  1.65 1.73  1.79  1.84  1.9   1.96  2.03  2.08  2.11  2.16  2.22  2.28  2.34  2.4 2.46 2.55  2.68  2.84  3.07  3.3  3.61  3.98  4.52  5.08  5.84  6.55  8.59]

# Add array to the line chart      
plt.xticks((x_axis_array),fontsize=14,rotation=90)

plt.show()

x_axis_array 有 36 个值,基本上由来自df.colA 的值按一定间隔组成。

我没有足够的声望点来上传图表的图像。基本上大多数 x 轴值都向左倾斜。它们聚集在一起,难以辨认。随着您沿轴走得更远,一些值会在 x 轴上展开,占用更多空间。

我想知道是否有一种方法可以有效地将图形向右拉伸,从而使所有 x 轴值均匀分布?谢谢

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    log scaled x axis 可能会做你想做的事。

    from matplotlib import pyplot as plt
    import numpy as np
    
    x_axis_array = [ 1.12,  1.2,  1.24,  1.28,  1.33,  1.38,
                     1.44,  1.5,  1.57,  1.65, 1.73,  1.79,
                     1.84,  1.9,   1.96,  2.03,  2.08,  2.11,
                     2.16,  2.22,  2.28,  2.34,  2.4, 2.46, 2.55,
                     2.68,  2.84,  3.07,  3.3,  3.61,  3.98,
                     4.52, 5.08, 5.84,  6.55,  8.59]
    
    plt.plot(x_axis_array, np.ones((len(x_axis_array), 1)), '*')
    
    ax = plt.gca()
    ax.set_xscale('log')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多