2017-09-22 22 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 

しかし、結果は次のとおりです。

私はYに元の値を示すことができる方法

I need the orignal values in Y axis

10,102などの代わりに軸(対数スケール)を使用しますか? 私は頑張りましたが、私は10、10²しかY軸に表示できませんでした!

私はY軸に表示する必要がありますすべて元の値です。

+0

y軸にはどのような値が必要ですか? – wwii

+0

@wwii:すべての実際の値を表示する必要があります:3,195,200、... 203 –

+0

@wwii:それはログスケールを使って可能ですか? –

答えて

0

xまたはyティックの位置を指定することができます。 y軸に最大値を追加するには、.yticks()を使用します。

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

実行時のログスケールのメジャーティックを決定するには、次のようにします。その後、は、下に10のすべての権限を作成、データに10の最大電力を見つけること:

質問はy軸上のバーとして存在しているすべてのyの値を表示する方法であるように思わ
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 tickフォーマットを使用して見つかりましたdocumentation

+0

ありがとうございますが、実際の値はすべて表示されませんでした:Y軸に263,170 ...それは可能ですか? - > https://ufile.io/lzxbc –

+0

私はそれらが棒そのものにもっと良く見えると思っています。[matplotlibは役に立つかもしれない良い例の棒グラフを提供します](https://matplotlib.org/examples/api/barchart_demo .html) – Oli

+0

ありがとうございます。 Y軸に実際の値を表示するのに役立つでしょうか?私はそれが良いだろうと思う! –

1

y軸は対数目盛である。

これが原因で重複したラベルの、あまりにもあまり意味がないかもしれないが、ここでの答えは次のとおりです。

あなたは、通常の十進数に書式を設定するラベルとScalarFormatterの位置を設定するためにFixedLocatorを使用することができます(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に言及した他のソリューションのいずれか:私は、これはすべての小目盛りを削除することを信じているとして、あなたは別のアプローチを使用することができます。

関連する問題