Python交互图表可视化 bokeh(四) -- 折线图与面积图

折线图与面积图

  1. 单线图、多线图
  2. 面积图、堆叠面积图
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

# 不发出警告
import warnings
warnings.filterwarnings('ignore')

# 导入notebook绘图模块
from bokeh.io import output_notebook
output_notebook()

# 导入图表绘制、图表展示模块
from  bokeh.plotting import figure,show

1、折线图 - 单线图

# 导入ColumnDataSource模块,将数据存储为ColumnDataSource对象
from bokeh.models import ColumnDataSource
# 也可以将dict、DataFrame、group对象转化为ColumnDataSource对象

# 创建数据
df = pd.DataFrame({'value':np.random.randn(100).cumsum()})
df.index.name='index'
df.head()

# 转化为ColumnDataSource对象
source = ColumnDataSource(data=df)
# 这里要注意,index和columns都必须有名称字段

# 绘制折线图
p = figure(plot_width=600, plot_height=400)
p.line(x='index', y='value', source=source,  # 设置x,y值,source -> 数据源
       line_width=1, line_alpha=0.8, line_color='black', line_dash=[8,4]      
      )

# 绘制散点
p.circle(x='index', y='value', source=source, size=4, color='blue', alpha=0.8)

show(p)

在这里插入图片描述

2、折线图 - 多线图:multi_line()

df = pd.DataFrame({'A': np.random.randn(100).cumsum(), 'B': np.random.randn(100).cumsum()})

p = figure(plot_width=600, plot_height=400)

# 绘制多线图
p.multi_line([df.index, df.index], 
             [df['A'], df['B']],   # 注意x,y的设置:[x1,x2,...xn], [y1,y2,...yn]
             color=['firebrick', 'navy'],  # 可同时设置:color='firebrick'
             alpha=[0.8, 0.4],   # 可同时设置:alpha=0.8
             line_width=[2,1],  # 可同时设置:line_wdith=2
            )

show(p)

在这里插入图片描述

3、折线图 - 多线图:多个line

# 创建x值
x = np.linspace(0.1, 5, 100)

# 这里设置对数坐标轴
p = figure(title="log axis example", y_axis_type="log", y_range=(0.001, 10**22))

# line1
p.line(x, np.sqrt(x), legend_label="y=sqrt(x)", line_color="tomato", line_dash="dotdash", line_width=2)

# line2: 折线图+散点图
p.line(x, x, legend_label='y=x')
p.circle(x, x, legend_label='y=x')

# line3
p.line(x, x**2, legend_label='y=x**2')
p.circle(x, x**2, legend_label='y=x**2', fill_color=None, line_color="olivedrab")

# line4
p.line(x, 10**x, legend_label="y=10^x", line_color='gold', line_width=2)

# line5
p.line(x, x**x, legend_label="y=x^x", line_color='indigo', line_width=2, line_dash="dotted")

# line6
p.line(x, 10**(x**2), legend_label="y=10^(x^2)", line_color='coral', line_width=2, line_dash="dashed")

# 设置图例及label
p.legend.location='top_left'
p.xaxis.axis_label='Domain'
p.yaxis.axis_label='Values (log scale)'


show(p)

在这里插入图片描述

4、面积图 - 单维度面积图

# 创建数据,设定起始点和终点值为0
s = pd.Series(np.random.randn(100).cumsum())
s.iloc[0]=0
s.iloc[-1]=0

# 绘制面积图
p = figure(plot_width=600, plot_height=400)
p.patch(s.index, s.values,  # 设置x,y值
        line_width=2, line_alpha=0.8, line_color='blue', line_dash=[10,4],
        fill_color='black', fill_alpha=0.2
       )
# .patch将会把所有点连接成一个闭合面

# 绘制折点
p.circle(s.index, s.values, size=4, color='red', alpha=0.8)

show(p)

在这里插入图片描述

5、面积图 - 面积堆叠图

# 导入brewer模块
from bokeh.palettes import brewer

N=20
cats=10
rng=np.random.RandomState(2)
df = pd.DataFrame(rng.randint(10,100, size=(N, cats))).add_prefix('y')  # 创建一个shape为(20,10)的df
df.head()

df_top=df.cumsum(axis=1)   # 每一堆叠面积图的最高点
df_bottom=df_top.shift(axis=1).fillna({'y0':0})[::-1]    # 每一个堆叠面积图的最低点,并反向
df_stack=pd.concat([df_bottom, df_top], ignore_index=True)  # 数据合并,每一组数据都是一个可以合围成一个面的散点集合
# 得到对面积数据

colors = brewer['Spectral'][df_stack.shape[1]]   # 根据变量数拆分颜色
x = np.hstack((df.index[::-1], df.index))  # 得到围合顺序的index,这里由于一列是20个元素,所以链接成面需要40个点

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组对应的values
          color=colors, alpha=0.8, line_color=None
         )

show(p)

在这里插入图片描述

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jepson2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值