折线图与面积图

① 单线图、多线图
② 面积图、堆叠面积图

1. 折线图--单线图

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. % matplotlib inline
  5.  
  6. import warnings
  7. warnings.filterwarnings('ignore')
  8. # 不发出警告
  9.  
  10. from bokeh.io import output_notebook
  11. output_notebook()
  12. # 导入notebook绘图模块
  13.  
  14. from bokeh.plotting import figure,show
  15. # 导入图表绘制、图标展示模块

  1. source = ColumnDataSource(data = df) 这里dfindexcolumns都必须有名称字段
  1. p.line(x='index',y='value',source = source, line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4])
  2. # 绘制折线图
  3. p.circle(x='index',y='value',source = source, size = 2,color = 'red',alpha = 0.8) # 绘制折点
  1. # 1、折线图 - 单线图
  2.  
  3. from bokeh.models import ColumnDataSource
  4. # 导入ColumnDataSource模块
  5. # 将数据存储为ColumnDataSource对象
  6. # 参考文档:http://bokeh.pydata.org/en/latest/docs/user_guide/data.html
  7. # 可以将dict、Dataframe、group对象转化为ColumnDataSource对象
  8.  
  9. df = pd.DataFrame({'value':np.random.randn(100).cumsum()})
  10. # 创建数据
  11. df.index.name = 'index'
  12. source = ColumnDataSource(data = df)
  13. # 转化为ColumnDataSource对象
  14. # 这里注意了,index和columns都必须有名称字段
  15.  
  16. p = figure(plot_width=600, plot_height=400)
  17. p.line(x='index',y='value',source = source, # 设置x,y值, source → 数据源
  18. line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4]) # 线型基本设置
  19. # 绘制折线图
  20. p.circle(x='index',y='value',source = source,
  21. size = 2,color = 'red',alpha = 0.8)
  22. # 绘制折点
  23.  
  24. show(p)
  25. df.head()

 

  1. 可以将dictDataframegroup对象转化为ColumnDataSource对象
  2. dic = {'index':df.index.tolist(), 'value':df['value'].tolist()} #一般把它先变成字典的格式
  3. source = ColumnDataSource(data=dic)
  1. #不转换为字典也可以,把index提取出来df.index.name = 'a' --->>> source = ColumnDataSource(data = df)
  1.  
  1. from bokeh.models import ColumnDataSource
  2. # 导入ColumnDataSource模块
  3. # 将数据存储为ColumnDataSource对象
  4. # 参考文档:http://bokeh.pydata.org/en/latest/docs/user_guide/data.html
  5. # 可以将dict、Dataframe、group对象转化为ColumnDataSource对象
  6.  
  7. dic = {'index':df.index.tolist(), 'value':df['value'].tolist()} #一般把它先变成字典的格式
  8. source = ColumnDataSource(data=dic)
  9. print(source)
  10.  
  11. p = figure(plot_width=600, plot_height=400)
  12. p.line(x='index',y='value',source = source, # 设置x,y值, source → 数据源
  13. line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4]) # 线型基本设置
  14. # 绘制折线图
  15.  
  16. show(p)

  1. df.index.name = 'a' #不转换为字典也可以,把index提取出来
  2. source = ColumnDataSource(data = df)
  3. p = figure(plot_width=600, plot_height=400)
  4. p.line(x='a',y='value',source = source, # 设置x,y值, source → 数据源
  5. line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4]) # 线型基本设置
  6. # 绘制折线图
  7. show(p)

  1. df.index.name = 'a'
  2. source = ColumnDataSource(data = df)
  3. p = figure()
  4. p.line(x='a',y='value',source = source) # 设置x,y值, source → 数据源
  5.  
  6. show(p)

2. 折线图--多线图

  1. multi_line
  1. p.multi_line([df.index, df.index], [df['A'], df['B']], color=["firebrick", "navy"], alpha=[0.8, 0.6], line_width=[2,1],)
  1. # 2、折线图 - 多线图
  2. # ① multi_line
  3.  
  4. df = pd.DataFrame({'A':np.random.randn(100).cumsum(),"B":np.random.randn(100).cumsum()})
  5. # 创建数据
  6.  
  7. p = figure(plot_width=600, plot_height=400)
  8. p.multi_line([df.index, df.index], #第一条线的横坐标和第二条线的横坐标
  9. [df['A'], df['B']], # 注意x,y值的设置 → [x1,x2,x3,..], [y1,y2,y3,...] 第一条线的Y值和第二条线的Y值
  10. color=["firebrick", "navy"], # 可同时设置 → color= "firebrick";也可以统一弄成一个颜色。
  11. alpha=[0.8, 0.6], # 可同时设置 → alpha = 0.6
  12. line_width=[2,1], # 可同时设置 → line_width = 2
  13. )
  14. # 绘制多段线
  15. # 这里由于需要输入具体值,故直接用dataframe,或者dict即可
  16.  
  17. show(p)

  1. 多个line
  1. p.line(x, 10**(x**2), legend="y=10^(x^2)",line_color="coral", line_dash="dashed", line_width=2)
  1. # 2、折线图 - 多线图
  2. # ② 多个line
  3.  
  4. x = np.linspace(0.1, 5, 100)
  5. # 创建x值
  6.  
  7. p = figure(title="log axis example", y_axis_type="log",y_range=(0.001, 10**22))
  8. # 这里设置对数坐标轴
  9.  
  10. p.line(x, np.sqrt(x), legend="y=sqrt(x)",
  11. line_color="tomato", line_dash="dotdash")
  12. # line1
  13.  
  14. p.line(x, x, legend="y=x")
  15. p.circle(x, x, legend="y=x")
  16. # line2,折线图+散点图
  17.  
  18. p.line(x, x**2, legend="y=x**2")
  19. p.circle(x, x**2, legend="y=x**2",fill_color=None, line_color="olivedrab")
  20. # line3
  21.  
  22. p.line(x, 10**x, legend="y=10^x",line_color="gold", line_width=2)
  23. # line4
  24.  
  25. p.line(x, x**x, legend="y=x^x",line_dash="dotted", line_color="indigo", line_width=2)
  26. # line5
  27.  
  28. p.line(x, 10**(x**2), legend="y=10^(x^2)",line_color="coral", line_dash="dashed", line_width=2)
  29. # line6
  30.  
  31. p.legend.location = "top_left"
  32. p.xaxis.axis_label = 'Domain'
  33. p.yaxis.axis_label = 'Values (log scale)'
  34. # 设置图例及label
  35.  
  36. show(p)

3. 面积图

  1. # 3、面积图 - 单维度面积图
  2.  
  3. s = pd.Series(np.random.randn(100).cumsum())
  4. s.iloc[0] = 0
  5. s.iloc[-1] = 0
  6. # 创建数据
  7. # 注意设定起始值和终点值为最低点
  8. p = figure(plot_width=600, plot_height=400)
  9. p.patch(s.index, s.values, # 设置x,y值
  10. line_width=1, line_alpha = 0.8, line_color = 'black',line_dash = [10,4], # 线型基本设置
  11. fill_color = 'black',fill_alpha = 0.2
  12. )
  13. # 绘制面积图
  14. # .patch将会把所有点连接成一个闭合面
  15.  
  16. show(p)

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

  1. # 3、面积图 - 面积堆叠图
  2.  
  3. from bokeh.palettes import brewer
  4. # 导入brewer模块
  5.  
  6. N = 20
  7. cats = 10 #分类
  8. rng = np.random.RandomState(1)
  9. df = pd.DataFrame(rng.randint(10, 100, size=(N, cats))).add_prefix('y')
  10. # 创建数据,shape为(20,10)
  11. df_top = df.cumsum(axis=1) # 每一个堆叠面积图的最高点
  12. df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1] # 每一个堆叠面积图的最低点,并反向
  13. df_stack = pd.concat([df_bottom, df_top], ignore_index=True) # 数据合并,每一组数据都是一个可以围合成一个面的散点集合
  14. # 得到堆叠面积数据
  15. # print(df.head())
  16. # print(df_top.tail())
  17. # print(df_bottom.head())
  18. # df_stack
  19.  
  20. colors = brewer['Spectral'][df_stack.shape[1]] # 根据变量数拆分颜色
  21. x = np.hstack((df.index[::-1], df.index)) # 得到围合顺序的index,这里由于一列是20个元素,所以连接成面需要40个点
  22. print(x)
  23. p = figure(x_range=(0, N-1), y_range=(0, 700))
  24. p.patches([x] * df_stack.shape[1], # 得到10组index
  25. [df_stack[c].values for c in df_stack], # c为df_stack的列名,这里得到10组对应的valyes
  26. color=colors, alpha=0.8, line_color=None) # 设置其他参数
  27.  
  28. show(p)
  1. [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
  2. 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

Python交互图表可视化Bokeh:4. 折线图| 面积图的更多相关文章

  1. Python交互图表可视化Bokeh:5 柱状图| 堆叠图| 直方图

    柱状图/堆叠图/直方图 ① 单系列柱状图② 多系列柱状图③ 堆叠图④ 直方图 1.单系列柱状图 import numpy as np import pandas as pd import matplo ...

  2. Python交互图表可视化Bokeh:1. 可视交互化原理| 基本设置

    Bokeh pandas和matplotlib就可以直接出分析的图表了,最基本的出图方式.是面向数据分析过程中出图的工具:Seaborn相比matplotlib封装了一些对数据的组合和识别的功能:用S ...

  3. Python交互图表可视化Bokeh:7. 工具栏

    ToolBar工具栏设置 ① 位置设置② 移动.放大缩小.存储.刷新③ 选择④ 提示框.十字线 1. 位置设置 import numpy as np import pandas as pd impor ...

  4. Python交互图表可视化Bokeh:6. 轴线| 浮动| 多图表

    绘图表达进阶操作 ① 轴线设置② 浮动设置③ 多图表设置 1. 轴线标签设置 设置字符串 import numpy as np import pandas as pd import matplotli ...

  5. Python交互图表可视化Bokeh:3. 散点图

    散点图 ① 基本散点图绘制② 散点图颜色.大小设置方法③ 不同符号的散点图 1. 基本散点图绘制 import numpy as np import pandas as pd import matpl ...

  6. Python交互图表可视化Bokeh:2. 辅助参数

    图表辅助参数设置 辅助标注.注释.矢量箭头 参考官方文档:https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#col ...

  7. 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图

    1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...

  8. pyecharts v1 版本 学习笔记 折线图,面积图

    折线图 折线图 基本demo import pyecharts.options as opts from pyecharts.charts import Line c = ( Line() .add_ ...

  9. Python:数据可视化pyecharts的使用

    什么是pyecharts? pyecharts 是一个用于生成 Echarts 图表的类库. echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化.pyecharts 是一个用于生 ...

随机推荐

  1. O(big oh) (big omega) (big theta)

    big oh big omega big theta more

  2. .NET NPOI操作Excel 让单元格的内容换行

    HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿 ISheet sheet = workbook.CreateSheet("会员列表&qu ...

  3. 缺失dll的问题

    不小心运行一下什么程序就会出现缺失xxx.dll的问题,太烦了,遇到好多,一直没有记录.现在开始记录,以便日后查看~ 1. api-ms-win-crt-runtime-l1-1-0.dll 64位系 ...

  4. Confluence 6 缓存性能优化

    Confluence 的运行状态与缓存状态有这密切的关系.针对 Confluence 的管理员来说,尤其是大型站点的 Confluence 管理员,设置好缓存尤其显得关键. 希望修改缓存的大小: 进入 ...

  5. Confluence 6 数据库和临时目录

    数据库 所有的其他数据库,包括有页面,内容都存储在数据库中.如果你安装的 Confluence 是用于评估或者你选择使用的是 Embedded H2 Database 数据库.数据库有关的文件将会存储 ...

  6. (一)python 数据模型

    1.通过实现特殊方法,自定义类型可以表现的跟内置类型一样: 如下代码,实现len, getitem,可使自定义类型表现得如同列表一样. import collections from random i ...

  7. What Are You Talking About (map)

    Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians u ...

  8. Python编程:从入门到实践(选记)

    本文参考< Python 编程:从入门到实践>一书,作者: [ 美 ] Eric Matthes 第1章 起步 1.1     搭建python环境 在不同的操作系统中, Python 存 ...

  9. Python实操

    有两个列表,分别存放报名学习linux和python课程的学生名字 linux=['钢弹','小壁虎','小虎比','alex','wupeiqi','yuanhao'] python=['drago ...

  10. requests中get和post传参

    get请求 get(url, params=None, **kwargs) requests实现get请求传参的两种方式 方式一: import requests url = 'http://www. ...