python库中文参考及测试开发(自动化性能接口安全测试运维等)程序员工具癖

[雪峰磁针石博客]Bokeh数据可视化工具3其他数据结构绘图

2018-08-05  本文已影响33人  oychw

使用numpy创建线状图

#Creating line plots using NumPy arrays
#Import required packages
import numpy as np
import random
from bokeh.io import output_file, show
from bokeh.plotting import figure

#Creating an array for the points along the x and y axes
array_x =np.array([1,2,3,4,5,6])
array_y = np.array([5,6,7,8,9,10])

#Creating a line plot
plot = figure()
plot.line(array_x, array_y)

#Output the plot
output_file('numpy_line.html')
show(plot)

image.png

使用numpy创建散列图

#Creating scatter plots using NumPy arrays
#Import required packages
import numpy as np
import random
from bokeh.io import output_file, show
from bokeh.plotting import figure

#Creating arrays for two different categories of points
x_red = np.array([1,2,3,4,5])
y_red = np.array([5,6,7,8,9])
x_blue = np.array([10,11,12,13])
y_blue = np.array([14,15,16,17])

#Creating the categorical scatter plot 

plot = figure()
plot.circle(x_red, y_red, size = 9, color = 'red', alpha = 0.8)
plot.circle(x_blue, y_blue, size = 9, color = 'blue', alpha = 0.8)

#Output the plot 
output_file('numpy_scatter.html')
show(plot)
image.png

使用pandas DataFrame创建时序图

苹果股票的高值:

#Creating a time series plot using a Pandas DataFrame
#Importing the required packages
import pandas as pd

#Read in the data
df = pd.read_csv('/home/andrew/code/kaggle-code/stock_data/all_stocks_5yr.csv')

#Filtering for apple stocks
df_apple = df[df['Name'] == 'AAL']
#df_apple.loc['date'] = df_apple['date'].astype('datetime64')
df_apple['date'] = pd.to_datetime(df_apple['date'])
print(df_apple.dtypes)

#Import the required packages
from bokeh.io import output_file, show
from bokeh.plotting import figure

#Create the time series plot
plot = figure(x_axis_type = 'datetime', x_axis_label = 'date', y_axis_label = 'High Prices')
plot.line(x = df_apple['date'], y = df_apple['high'])

#Output the plot
output_file('pandas_time.html')
show(plot)

image.png

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.astype.html
https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas

另外一个更简单的演示:

import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import row

output_file('fig.html')

test = pd.DataFrame({'datetime':pd.date_range('1/1/1880', periods=2000),'foo':np.arange(2000)})

print(test['datetime'])

fig = figure(x_axis_type="datetime")
fig.line(x='datetime',y='foo', source=test)

test = test.set_index('datetime')

fig2 = figure(x_axis_type="datetime")
fig2.line(x='datetime', y='foo', source=test)
show(row(fig, fig2))
image.png

可爱的python测试开发库 请在github上点赞,谢谢!
python中文库文档汇总
[雪峰磁针石博客]python3标准库-中文版
[雪峰磁针石博客]python3快速入门教程
接口自动化性能测试线上培训大纲
python测试开发自动化测试数据分析人工智能自学每周一练
更多内容请关注 雪峰磁针石:简书

https://stackoverflow.com/questions/34974615/timeseries-in-bokeh-using-a-dataframe-with-index

https://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html

https://github.com/CNuge/kaggle-code

上一篇下一篇

猜你喜欢

热点阅读