簡體   English   中英

matplotlib可以生成X == 0的日志條形圖作為有效數據點嗎?

[英]Can matplotlib produce a log bar-chart with X==0 as a valid data-point?

我想創建一個條形圖 ,其中X軸將包含數十萬個數據點。

因此,我需要采用對數刻度 X == 0X == 0是有效的數據點。
順便說一句,Y軸應采用線性刻度(其中y是分布, 0 < Y <= 1 )。

以下是最小的 演示代碼

$ cat stack_example.py 
#!/usr/bin/env python

def test_plot3():
    import pylab as pl

    _graph = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
    epsilon = 0.00000000001
    x = [ pl.log(k) if k > 0 else pl.log(epsilon) for k in _graph ]
    y = [ _graph[k] for k in _graph ]
    lx = pl.xlabel("in degree (logarithmic scale)")
    ly = pl.ylabel("normalized distribution (0 to 1)")
    tl = pl.title("graph in-degree normalized distribution")
    _width = 1.0 / (len(x) * 5.0)
    pl.bar(x, y, width=_width, log=True)
    pl.xscale('log')
    pl.yscale('linear')
    pl.show()

if __name__ == "__main__":
    test_plot3()

產生了以下無效圖(左側的大藍色矩形似乎是一個錯誤):

半對數條形圖

您能否建議一種通過Python生成正確的條形圖的方法 ,該方法將在X axis,使用對數刻度 ,在X axis,使用線性刻度 Y axis,並接受0作為有效的x點?

編輯1

基於@Ed的評論,我將代碼修改為:

#!/usr/bin/env python

def test_plot3():
    import pylab as pl

    _graph = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
    epsilon = 0.1
    x = [ pl.log(k) if k > 0 else pl.log(epsilon) for k in _graph ]
    y = [ _graph[k] for k in _graph ]
    lx = pl.xlabel("in degree (logarithmic scale)")
    ly = pl.ylabel("normalized distribution (0 to 1)")
    tl = pl.title("graph in-degree normalized distribution")
    _width = 1.0 / (len(x) * 5.0)
    pl.bar(x, y, width=_width, color="blue", log=True)
    pl.xscale('symlog', linthreshx=2)
    pl.yscale('linear')
    pl.show()

if __name__ == "__main__":
    test_plot3()


    if __name__ == "__main__":
        test_plot3()

但是生成的圖形仍然看起來不正確:

修正圖

您可以使用symlog代替log,它包括負數和接近零的小線性區域。 例如,

#!/usr/bin/env python

def test_plot3():
    import pylab as pl

    _graph = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
    epsilon = 0.00000000001
    x = [ pl.log(k) if k > 0 else pl.log(epsilon) for k in _graph ]
    y = [ _graph[k] for k in _graph ]
    lx = pl.xlabel("in degree (logarithmic scale)")
    ly = pl.ylabel("normalized distribution (0 to 1)")
    tl = pl.title("graph in-degree normalized distribution")
    _width = 1.0 / (len(x) * 5.0)
    pl.bar(x, y, width=_width, log=True)
    pl.xscale('symlog')
    pl.yscale('linear')
    pl.show()

if __name__ == "__main__":
    test_plot3()

您可以使用linthreshx參數xscale來調整線性區域的大小。 查看此問題以獲取有關如何使用它的詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM