簡體   English   中英

如何使用 def 函數創建多個散景圖

[英]How to use def function to create mulitple bokeh graphs

有誰知道如何在 python 中使用“def”來創建多個圖形? 我的代碼在下面,我想在選項卡中包含大約 20 個圖表。 感謝您在下面的代碼上提供幫助,因為我不太擅長使用 def 函數......

import pandas as pd
from math import pi
from bokeh.plotting import figure,output_file,show
from bokeh.models import ColumnDataSource,NumeralTickFormatter,HoverTool,DaysTicker,DatetimeTickFormatter,TickFormatter,Panel,Tabs
from bokeh.layouts import column,row

#1
# intialise data of lists. 
data1 = {'Date':['2020-10-10', '2020-10-09', '2020-10-08', '2020-10-07', '2020-10-06', '2020-10-05', '2020-10-04', '2020-10-03'], 
        'Close':[20, 21, 19, 18, 30, 10, 15, 18 ] } 

# Create DataFrame 
df1 = pd.DataFrame(data1) 

df1['Date_time']   = pd.to_datetime(df1['Date'], errors='coerce')

p1 = figure(x_axis_type="datetime")
p1.xaxis.major_label_orientation = pi/2
p1.grid.grid_line_alpha=0.8
p1.xaxis[0].ticker.desired_num_ticks = 12
p1.xaxis.formatter=DatetimeTickFormatter(days=['%Y-%m-%d'])
p1.line(df1.Date_time, df1.Close)
tab1 = Panel(child=p1, title="1")


#2
# intialise data of lists. 
data2 = {'Date':['2020-10-10', '2020-10-09', '2020-10-08', '2020-10-07', '2020-10-06', '2020-10-05', '2020-10-04', '2020-10-03'], 
        'Close':[200, 250, 190, 180, 100, 100, 150, 108 ] } 

# Create DataFrame 
df2 = pd.DataFrame(data2) 

df2['Date_time']   = pd.to_datetime(df2['Date'], errors='coerce')

p2 = figure(x_axis_type="datetime")
p2.xaxis.major_label_orientation = pi/2
p2.grid.grid_line_alpha=0.8
p2.xaxis[0].ticker.desired_num_ticks = 12
p2.xaxis.formatter=DatetimeTickFormatter(days=['%Y-%m-%d'])
p2.line(df2.Date_time, df2.Close)
tab2 = Panel(child=p2, title="2")


show(Tabs(tabs=[tab1, tab2]))

#更新為使用def

  • 我收到錯誤:未定義名稱“tab1”

  • 這是使用 def 編碼的正確方法嗎?

  • 你能從這里幫助糾正嗎?

    定義圖(y,x,z):

     y['Date_time'] = pd.to_datetime(y['Date'], errors='coerce') p = figure(x_axis_type="datetime") p.line(y.Date_time, y.Close) z = Panel(child=p, title=x)

    圖 (df1,'1','tab1')
    圖 (df2,'2','tab2')

    顯示(標簽(標簽= [標簽1,標簽2]))

您使用 def 來編寫函數。 函數只是你給它一個標簽的 Python 代碼行,以便你可以重用它們。

def some_function(x, y):
    # your code here

因此,您只需編寫代碼來為一個圖形執行此操作,將其全部標記到右側並為其命名。

暫無
暫無

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

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