日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python根据表格数据生成折线图_Python交互图表可视化Bokeh:4. 折线图| 面积图

發(fā)布時(shí)間:2024/2/28 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python根据表格数据生成折线图_Python交互图表可视化Bokeh:4. 折线图| 面积图 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

折線圖與面積圖

① 單線圖、多線圖

② 面積圖、堆疊面積圖

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。