2016-11-30 5 views
1

Ich habe den folgenden Code geschrieben, um ein Liniendiagramm basierend auf Daten aus einem Pandas-Datenframe zu erstellen. Der Index des Datenrahmens ist eine Zeitreihe.Python Bokeh zusätzliche Y-Achse Probleme

Der folgende Code funktionierte gut, aber ich entschied, dass ich eine weitere Datenreihe hinzufügen wollte (aus der dritten Spalte in meinem Datenrahmen, im Code als 'Col3' bezeichnet). Ich wollte diese dritte Serie auf einer separaten Y-Achse haben. Als ich jedoch den Code dazu hinzugefügt habe, der unten durch den *** hervorgehoben ist, scheint Bokeh nicht in der Lage zu sein, eine Handlung zu produzieren. Hat jemand eine Ahnung was ich falsch mache?

from bokeh.plotting import figure, output_file, show, save 
from bokeh.models import ColumnDataSource 
from bokeh.models import Range1d, LinearAxis 
import pandas as pd 
from pandas import HDFStore 
from bokeh.palettes import Spectral11 

store = pd.HDFStore(<dataframe location>) 
df = pd.DataFrame(store['d1']) 
df = df.rename_axis('Time') 
df.fillna(0) 

numlines = len(df.columns) 

#import colour pallete 
mypalette = Spectral11[0:numlines] 

# remove unwanted columns 
col_list = ['Col1', 'Col2', 'Col3'] 
df = df[col_list] 

# make a list of our columns 
col = [] 
[col.append(i) for i in df.columns] 

# make the figure, 
p = figure(x_axis_type="datetime", title="<Plot Title>", width = 800, height = 450) 
p.xaxis.axis_label = 'Date' 
p.yaxis.axis_label = '<Name of Primary Y Axis>' 

*** # add extra y axis 
p.extra_y_ranges = {'Col3': Range1d(start=0, end=0.25)} 
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right') *** 

# loop through our columns and colors 
for (columnnames, colore) in zip(col, mypalette): 
p.line(df.index, df[columnnames], legend = columnnames, color = colore) 

# creates an output file 
output_file('<outputlocation>') 

#save the plot 
save(p) 

Als Referenz sieht meine Datenrahmen wie folgt aus:

Datenrahmen wie folgt aussieht:

 Time   Col1  Col2  Col3  Col4 
29/11/2016 00:00 4  41  41  55 
29/11/2016 01:00 55  15  61  81 
29/11/2016 02:00 51  75  2   4 
29/11/2016 03:00 21  21  51  9 
etc. 
+0

Bitte geben Sie eine Probe von 'df' (siehe [MCVE] (http://stackoverflow.com/help/mcve)) –

+0

es auf die ursprüngliche Frage fügen Sie bitte –

+0

Ein Beispiel eines Datenrahmens wurde jetzt zur ursprünglichen Frage hinzugefügt. – pottolom

Antwort

1

Ein paar Fragen:

Wenn Sie dies tun:

p.extra_y_ranges = {'Col3': Range1d(start=0, end=0.25)} 
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right') 

Du bist kreativ ng anderen y_range mit dem Namen Col3 in der ersten Zeile. Sie müssen diesen Namen in der zweiten Zeile verwenden, nicht Performance ratio %. (Oder noch besser, nennen Sie es auch in der ersten Zeile Performance ratio %).

Auch in Ihrer Schleife legen Sie alle 3 Serien auf der gleichen y-Achse, müssen Sie für die dritte angeben.

Die folgenden Werke:

numlines = len(df.columns) 

#import colour pallete 
mypalette = Spectral11[0:numlines] 

# remove unwanted columns 
col_list = ['Col1', 'Col2', 'Col3'] 
df = df[col_list] 

# make the figure, 
p = figure(x_axis_type="datetime", title="<Plot Title>", 
      width = 800, height = 450, y_range=(0,100)) 
p.xaxis.axis_label = 'Date' 
p.yaxis.axis_label = '<Name of Primary Y Axis>' 


p.line(df.index, df['Col1'], legend = 'Col1', color = mypalette[0]) 
p.line(df.index, df['Col2'], legend = 'Col2', color = mypalette[1]) 

# add extra y axis 
p.extra_y_ranges = {'Performance ratio %': Range1d(start=0, end=50)} 
p.line(df.index, df['Col3'], legend = 'Col3', color = mypalette[2], 
     y_range_name='Performance ratio %') 
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right') 

# creates an output file 
output_file('bokeh.html') 

#save the plot 
show(p) 
+1

Danke. Ihre Kommentare sind sinnvoll und der von Ihnen bereitgestellte Code funktioniert einwandfrei. Sehr geschätzt. – pottolom