2017-09-22 172 views
0

我試圖繪製對數刻度(Y軸)的圖形,但我需要顯示Y軸全部的原始值。Python中的對數刻度

我使用的代碼:

# -*- coding: utf-8 -*- 

import math 
import matplotlib.pyplot as plt 
import matplotlib.dates as dates 
from datetime import datetime, timedelta 
import numpy as np 

x = [] 
y = [] 
with open("dataset.csv") as f: 
    for l in f: 
     X,Y = l.split(",") #separador eh a virgula 
     x.append(float(X)) 
     y.append(float (Y)) 
     #y.append(math.log (float (Y))) 

#x1 = [datetime.fromtimestamp(int(d)) for d in x] 
x1 = [str(datetime.fromtimestamp(int(d)))[-8:] for d in x] 
y_pos = [idx for idx, i in enumerate(y)] 

plt.figure(figsize=(17,9)) 
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%m/%d/%Y %H:%M:%S')) 

plt.bar(y_pos, y, align='edge', color="blue", alpha=0.5, width=0.5) # <--- EDICAO PRINCIPAL 
plt.title("Values X Time") 
plt.ylabel("Values") 
plt.xlabel('Time') 
plt.xticks(y_pos, x1, size='small',rotation=35, ha="right") 
#plt.yticks(y) 
#plt.yticks(np.arange(0,max(y),0.3)) 
#plt.yticks(np.arange(0,max(y)+5,1)) 
plt.yscale('log') 
plt.ylim(ymax=sorted(y)[-1]+1) # valor maximo do eixo y 
#plt.ylim(ymin=sorted(y)[0]-1) # valor minimo do eixo y 

plt.show() 

如果數據集是:

 1491828000,3 
    1491828060,195 
    1491828120,220 
    1491828180,240 
    1491828240,230 
    1491828300,238 
    1491828360,310 
    1491828420,280 
    1491828480,263 
    1491828540,271 
    1491828600,282 
    1491828660,302 
    1491828720,298 
    1491828780,257 
    1491828840,245 
    1491828900,200 
    1491828960,170 
    1491829020,138 
    1491829080,59 
    1491829140,39 
    1491829200,48 
    1491829260,95 
    1491829320,151 
    1491829380,155 
    1491829440,175 
    1491829500,93 
    1491829560,25 
    1491829620,3 
    1491829680,185 
    1491829740,233 
    1491829800,210 
    1491829860,86 
    1491829920,32 
    1491829980,46 
    1491830040,51 
    1491830100,201 
    1491830160,129 
    1491830220,116 
    1491830280,105 
    1491830340,200 
    1491830400,203 

但結果是:

I need the orignal values in Y axis

我怎麼能顯示出Y中的原始值軸(對數刻度),而不是10,10 2等? 我努力了,但我只能在Y軸上顯示10,10²!

我需要顯示Y軸全部的原始值。 有什麼想法?

+0

你想在y軸上做什麼值? – wwii

+0

@wwii:我需要顯示所有的實際值:3,195,200,... 203 –

+0

@wwii:這是可能的使用日誌的規模? –

回答

0

您可以指定ether的x或y打勾位置。要將最大值添加到y軸,請使用.yticks()

plt.yscale('log') 
plt.yticks([1,10,100] + [max(y)]) 
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.d')) 

確定運行時日誌比例的主要滴答;找到十個最大功率的數據,然後使全部十的權力下方:

import math 
... 
exp = int(math.log10(max(y))) 
majors = [10**x for x in range(exp+1)] 
#majors = [10**n for n in range(len(str(max(y))))] 
plt.yticks(majors + [max(y)]) 
1

Matplotlib股票的格式是你在找什麼:

from matplotlib.ticker import FormatStrFormatter 

... 

plt.yscale('log') 
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.d')) #Here! 
plt.ylim(ymax=sorted(y)[-1]+1) # valor maximo do eixo y 

plt.show() 

雖然有可能是一個更簡單的方法。使用matplotlibs打勾格式化發現documentation

+0

謝謝,但它沒有顯示所有的實際值:263,170 ...在Y軸上。那可能嗎? - > https://ufile.io/lzxbc –

+0

我覺得這些會更好地顯示在酒吧本身,[matplotlib提供了一個很好的示例條形圖,可能有所幫助](https://matplotlib.org/examples/api/barchart_demo .html) – Oli

+0

謝謝。你能幫助顯示Y軸的實際值嗎?我認爲這會更好! –

1

的問題似乎是如何顯示它們存在於Y軸杆的所有y值,因爲y軸是對數標度。

雖然這可能不是什麼太大的意義,因爲標籤重疊的,這裏是答案:

你可以使用一個FixedLocator來設置標籤的位置和ScalarFormatter設置格式正常十進制數(而不是10的冪)。

 
plt.gca().yaxis.set_major_locator(matplotlib.ticker.FixedLocator(np.unique(y))) 
plt.gca().yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter()) 

完整代碼:

import matplotlib.pyplot as plt 
import matplotlib.dates as dates 
import matplotlib.ticker 
from datetime import datetime, timedelta 
import numpy as np 

x,y = np.loadtxt(io.StringIO(u), delimiter=",", unpack=True) 

x1 = [str(datetime.fromtimestamp(int(d)))[-8:] for d in x] 
y_pos = [idx for idx, i in enumerate(y)] 

plt.figure(figsize=(17,9)) 
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%m/%d/%Y %H:%M:%S')) 

plt.bar(y_pos, y, align='edge', color="blue", alpha=0.5, width=0.5) 
plt.title("Values X Time") 
plt.ylabel("Values") 
plt.xlabel('Time') 
plt.xticks(y_pos, x1, size='small',rotation=35, ha="right") 

plt.yscale('log') 
plt.ylim(ymax=sorted(y)[-1]+1) # valor maximo do eixo y 

plt.gca().yaxis.set_major_locator(matplotlib.ticker.FixedLocator(np.unique(y))) 
plt.gca().yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter()) 

plt.show() 

enter image description here

0

好笑的是,我正是解決這個問題的SO in portuguese

除了對ImportanceOfBeingErnest的回答,您可以添加

plt.gca().minorticks_off() 

刪除日誌規模較小的蜱,因爲他們是重疊的。因爲我相信,這刪除所有次要刻度線,您可以使用另一種方法:

matplotlib.rcParams['xtick.minor.size'] = 0 
matplotlib.rcParams['xtick.minor.width'] = 0 

或者任何this question提到的其他解決方案。