Python 如何使用def函数创建多个bokeh图
有人知道如何在python中使用“def”来创建多个图形吗? 下面是我的代码,我想在选项卡中包含大约20个图表。 如果您在下面的代码上提供帮助,我将不胜感激,因为我不太擅长使用def函数Python 如何使用def函数创建多个bokeh图,python,function,bokeh,Python,Function,Bokeh,有人知道如何在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,Ho
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编码的正确方法吗
- 你能帮我纠正一下吗
def图形(y、x、z):
图形(df1,'1','tab1')
图(df2,'2','tab2') 显示(选项卡(选项卡=[tab1,tab2]))
def一些函数(x,y):
#你的代码在这里
因此,您只需为一个图形编写代码,将其全部添加到右边并为其命名。您完全了解如何编写函数吗?谢谢。我了解函数的结构。我的问题是我不能给函数名分配变量名,对吗?函数中的参数就是这样的。调用其余代码中调用的所有其他函数和方法时放在括号中的位。我尝试使用def,但仍有错误,下面是我的代码。def graph(x,y):y['Date\u time']=pd.to\u datetime(y['Date'],errors='concurve')p=figure(x\u axis\u type=“datetime”)p.xaxis.major\u label\u orientation=pi/2 p.grid.grid\u line\u alpha=0.8 p.xaxis[0].ticker.desired\u num\u ticks=12 p.xaxis.formatter=DatetimeTickFormatter(days=['%Y-%m-%d'])p.line(Y.Date\u time,Y.Close)tab+str(x)=面板(child=p,title=str(x))graph(1,df1)graph(2,df2)您好,谢谢查看。对不起,这是密码。让我稍后再发嗨,我再发一次代码。你能帮我看看吗?图表上的部分似乎没有运行。Tks
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)