python根据表格数据生成折线图_Python交互图表可视化Bokeh:4. 折线图| 面积图
折線圖與面積圖
① 單線圖、多線圖
② 面積圖、堆疊面積圖
1. 折線圖--單線圖
importnumpy as npimportpandas as pdimportmatplotlib.pyplot as plt%matplotlib inlineimportwarnings
warnings.filterwarnings('ignore')#不發(fā)出警告
from bokeh.io importoutput_notebook
output_notebook()#導(dǎo)入notebook繪圖模塊
from bokeh.plotting importfigure,show#導(dǎo)入圖表繪制、圖標(biāo)展示模塊
source = ColumnDataSource(data = df) 這里df中index、columns都必須有名稱字段
p.line(x='index',y='value',source = source, line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4])
# 繪制折線圖
p.circle(x='index',y='value',source = source, size = 2,color = 'red',alpha = 0.8) # 繪制折點(diǎn)
#1、折線圖 - 單線圖
from bokeh.models importColumnDataSource#導(dǎo)入ColumnDataSource模塊#將數(shù)據(jù)存儲(chǔ)為ColumnDataSource對象#參考文檔:http://bokeh.pydata.org/en/latest/docs/user_guide/data.html#可以將dict、Dataframe、group對象轉(zhuǎn)化為ColumnDataSource對象
df= pd.DataFrame({'value':np.random.randn(100).cumsum()})#創(chuàng)建數(shù)據(jù)
df.index.name = 'index'source= ColumnDataSource(data =df)#轉(zhuǎn)化為ColumnDataSource對象#這里注意了,index和columns都必須有名稱字段
p= figure(plot_width=600, plot_height=400)
p.line(x='index',y='value',source = source, #設(shè)置x,y值, source → 數(shù)據(jù)源
line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4]) #線型基本設(shè)置#繪制折線圖
p.circle(x='index',y='value',source =source,
size= 2,color = 'red',alpha = 0.8)#繪制折點(diǎn)
show(p)
df.head()
可以將dict、Dataframe、group對象轉(zhuǎn)化為ColumnDataSource對象
dic = {'index':df.index.tolist(), 'value':df['value'].tolist()} #一般把它先變成字典的格式
source = ColumnDataSource(data=dic)
#不轉(zhuǎn)換為字典也可以,把index提取出來df.index.name = 'a' --->>> source = ColumnDataSource(data = df)
from bokeh.models importColumnDataSource#導(dǎo)入ColumnDataSource模塊#將數(shù)據(jù)存儲(chǔ)為ColumnDataSource對象#參考文檔:http://bokeh.pydata.org/en/latest/docs/user_guide/data.html#可以將dict、Dataframe、group對象轉(zhuǎn)化為ColumnDataSource對象
dic= {'index':df.index.tolist(), 'value':df['value'].tolist()} #一般把它先變成字典的格式
source = ColumnDataSource(data=dic)print(source)
p= figure(plot_width=600, plot_height=400)
p.line(x='index',y='value',source = source, #設(shè)置x,y值, source → 數(shù)據(jù)源
line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4]) #線型基本設(shè)置#繪制折線圖
show(p)
df.index.name = 'a' #不轉(zhuǎn)換為字典也可以,把index提取出來
source = ColumnDataSource(data =df)
p= figure(plot_width=600, plot_height=400)
p.line(x='a',y='value',source = source, #設(shè)置x,y值, source → 數(shù)據(jù)源
line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4]) #線型基本設(shè)置#繪制折線圖
show(p)
df.index.name = 'a'source= ColumnDataSource(data =df)
p=figure()
p.line(x='a',y='value',source = source) #設(shè)置x,y值, source → 數(shù)據(jù)源
show(p)
2. 折線圖--多線圖
① multi_line
p.multi_line([df.index, df.index], [df['A'], df['B']], color=["firebrick", "navy"], alpha=[0.8, 0.6], line_width=[2,1],)
#2、折線圖 - 多線圖#① multi_line
df= pd.DataFrame({'A':np.random.randn(100).cumsum(),"B":np.random.randn(100).cumsum()})#創(chuàng)建數(shù)據(jù)
p= figure(plot_width=600, plot_height=400)
p.multi_line([df.index, df.index],#第一條線的橫坐標(biāo)和第二條線的橫坐標(biāo)
[df['A'], df['B']], #注意x,y值的設(shè)置 → [x1,x2,x3,..], [y1,y2,y3,...] 第一條線的Y值和第二條線的Y值
color=["firebrick", "navy"], #可同時(shí)設(shè)置 → color= "firebrick";也可以統(tǒng)一弄成一個(gè)顏色。
alpha=[0.8, 0.6], #可同時(shí)設(shè)置 → alpha = 0.6
line_width=[2,1], #可同時(shí)設(shè)置 → line_width = 2
)#繪制多段線#這里由于需要輸入具體值,故直接用dataframe,或者dict即可
show(p)
② 多個(gè)line
p.line(x, 10**(x**2), legend="y=10^(x^2)",line_color="coral", line_dash="dashed", line_width=2)
#2、折線圖 - 多線圖#② 多個(gè)line
x= np.linspace(0.1, 5, 100)#創(chuàng)建x值
p= figure(title="log axis example", y_axis_type="log",y_range=(0.001, 10**22))#這里設(shè)置對數(shù)坐標(biāo)軸
p.line(x, np.sqrt(x), legend="y=sqrt(x)",
line_color="tomato", line_dash="dotdash")#line1
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x")#line2,折線圖+散點(diǎn)圖
p.line(x, x**2, legend="y=x**2")
p.circle(x, x**2, legend="y=x**2",fill_color=None, line_color="olivedrab")#line3
p.line(x,10**x, legend="y=10^x",line_color="gold", line_width=2)#line4
p.line(x, x**x, legend="y=x^x",line_dash="dotted", line_color="indigo", line_width=2)#line5
p.line(x,10**(x**2), legend="y=10^(x^2)",line_color="coral", line_dash="dashed", line_width=2)#line6
p.legend.location= "top_left"p.xaxis.axis_label= 'Domain'p.yaxis.axis_label= 'Values (log scale)'
#設(shè)置圖例及l(fā)abel
show(p)
3. 面積圖
#3、面積圖 - 單維度面積圖
s= pd.Series(np.random.randn(100).cumsum())
s.iloc[0]=0
s.iloc[-1] =0#創(chuàng)建數(shù)據(jù)#注意設(shè)定起始值和終點(diǎn)值為最低點(diǎn)
p = figure(plot_width=600, plot_height=400)
p.patch(s.index, s.values,#設(shè)置x,y值
line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4], #線型基本設(shè)置
fill_color = 'black',fill_alpha = 0.2)#繪制面積圖#.patch將會(huì)把所有點(diǎn)連接成一個(gè)閉合面
show(p)
p.circle(s.index, s.values,size = 5,color = 'red',alpha = 0.8)#繪制折點(diǎn)
#3、面積圖 - 面積堆疊圖
from bokeh.palettes importbrewer#導(dǎo)入brewer模塊
N= 20cats= 10 #分類
rng = np.random.RandomState(1)
df= pd.DataFrame(rng.randint(10, 100, size=(N, cats))).add_prefix('y')#創(chuàng)建數(shù)據(jù),shape為(20,10)
df_top = df.cumsum(axis=1) #每一個(gè)堆疊面積圖的最高點(diǎn)
df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1] #每一個(gè)堆疊面積圖的最低點(diǎn),并反向
df_stack = pd.concat([df_bottom, df_top], ignore_index=True) #數(shù)據(jù)合并,每一組數(shù)據(jù)都是一個(gè)可以圍合成一個(gè)面的散點(diǎn)集合#得到堆疊面積數(shù)據(jù)#print(df.head())#print(df_top.tail())#print(df_bottom.head())#df_stack
colors= brewer['Spectral'][df_stack.shape[1]] #根據(jù)變量數(shù)拆分顏色
x = np.hstack((df.index[::-1], df.index)) #得到圍合順序的index,這里由于一列是20個(gè)元素,所以連接成面需要40個(gè)點(diǎn)
print(x)
p= figure(x_range=(0, N-1), y_range=(0, 700))
p.patches([x]* df_stack.shape[1], #得到10組index
[df_stack[c].values for c in df_stack], #c為df_stack的列名,這里得到10組對應(yīng)的valyes
color=colors, alpha=0.8, line_color=None) #設(shè)置其他參數(shù)
show(p)
[19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的python根据表格数据生成折线图_Python交互图表可视化Bokeh:4. 折线图| 面积图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python做一个考试系统_1218Py
- 下一篇: python serial_Python