python四维数据可视化_Python数据可视化:基于Bokeh的可视化绘图最新章节_屈希峰著_掌阅小说网...
2.1 绘图方法figure(**kwargs)对象提供多种绘图方法(见1.3节),而这些方法的实现大都基于Bokeh.models接口。本节将展示些基本图形的绘制方法。代码示例 2-11. import numpy as np2. from bokeh.layouts import gridplot3. # 数据4. N = 95. x = np.linspace(-2, 2, N)6. y =
2.1 绘图方法
figure(**kwargs)对象提供多种绘图方法(见1.3节),而这些方法的实现大都基于Bokeh.models接口。本节将展示些基本图形的绘制方法。
代码示例 2-1
1. import numpy as np
2. from bokeh.layouts import gridplot
3. # 数据
4. N = 9
5. x = np.linspace(-2, 2, N)
6. y = x**2
7. sizes = np.linspace(10, 20, N)
8. xpts = np.array([-.09, -.12, .0, .12, .09])
9. ypts = np.array([-.1, .02, .1, .02, -.1])
10. # 画布列表
11. figures = []
12. p = figure(title="annular_wedge")
13. p.annular_wedge(x, y, 10, 20, 0.6, 4.1, color="#8888ee",
14. inner_radius_units="screen", outer_radius_units="screen")
15. figures.append(p)
16. p = figure(title="annulus")
17. p.annulus(x, y, 10, 20, color="#7FC97F",
18. inner_radius_units="screen", outer_radius_units = "screen")
19. figures.append(p)
20. p = figure(title="arc")
21. p.arc(x, y, 20, 0.6, 4.1,
22. radius_units="screen", color="#BEAED4", line_width=3)
23. figures.append(p)
24. p = figure(title="bezier")
25. p.bezier(x, y, x+0.2, y, x+0.1, y+0.1, x-0.1, y-0.1,
26. color="#D95F02", line_width=2)
27. figures.append(p)
28. p = figure(title="circle")
29. p.circle(x, y, radius=0.1, color="#3288BD")
30. figures.append(p)
31. p = figure(title="ellipse")
32. p.ellipse(x, y, 15, 25, angle=-0.7, color="#1D91C0",
33. width_units="screen", height_units="screen")
34. figures.append(p)
35. p = figure(title="line")
36. p.line(x, y, color="#F46D43")
37. figures.append(p)
38. p = figure(title="multi_line")
39. p.multi_line([xpts+xx for xx in x], [ypts+yy for yy in y], color="#8073AC",
line_width=2)
40. figures.append(p)
41. p = figure(title="multi_polygons")
42. p.multi_polygons([[[xpts*2+xx, xpts+xx]] for xx in x], [[[ypts*3+yy,
ypts+yy]] for yy in y], color="#FB9A99")
43. figures.append(p)
44. p = figure(title="oval")
45. p.oval(x, y, 15, 25, angle=-0.7, color="#1D91C0",
46. width_units="screen", height_units="screen")
47. figures.append(p)
48. p = figure(title="patch")
49. p.patch(x, y, color="#A6CEE3")
50. figures.append(p)
51. p = figure(title="patches")
52. p.patches([xpts+xx for xx in x], [ypts+yy for yy in y], color="#FB9A99")
53. figures.append(p)
54. p = figure(title="quad")
55. p.quad(x, x-0.1, y, y-0.1, color="#B3DE69")
56. figures.append(p)
57. p = figure(title="quadratic")
58. p.quadratic(x, y, x+0.2, y, x+0.1, y+0.1, color="#4DAF4A", line_width=3)
59. figures.append(p)
60. p = figure(title="ray")
61. p.ray(x, y, 45, -0.7, color="#FB8072", line_width=2)
62. figures.append(p)
63. p = figure(title="rect")
64. p.rect(x, y, 10, 20, color="#CAB2D6", width_units="screen", height_units="screen")
65. figures.append(p)
66. p = figure(title="segment")
67. p.segment(x, y, x-0.1, y-0.1, color="#F4A582", line_width=3)
68. figures.append(p)
69. p = figure(title="square")
70. p.square(x, y, size=sizes, color="#74ADD1")
71. figures.append(p)
72. p = figure(title="wedge")
73. p.wedge(x, y, 15, 0.6, 4.1, radius_units="screen", color="#B3DE69")
74. figures.append(p)
75. p = figure(title="circle_x")
76. p.scatter(x, y, marker="circle_x", size=sizes, color="#DD1C77", fill_
color=None)
77. figures.append(p)
78. p = figure(title="triangle")
79. p.scatter(x, y, marker="triangle", size=sizes, color="#99D594", line_width=2)
80. figures.append(p)
81. p = figure(title="circle")
82. p.scatter(x, y, marker="o", size=sizes, color="#80B1D3", line_width=3)
83. figures.append(p)
84. p = figure(title="cross")
85. p.scatter(x, y, marker="cross", size=sizes, color="#E6550D", line_width=2)
86. figures.append(p)
87. p = figure(title="diamond")
88. p.scatter(x, y, marker="diamond", size=sizes, color="#1C9099", line_width=2)
89. figures.append(p)
90. p = figure(title="inverted_triangle")
91. p.scatter(x, y, marker="inverted_triangle", size=sizes, color="#DE2D26")
92. figures.append(p)
93. p = figure(title="square_x")
94. p.scatter(x, y, marker="square_x", size=sizes, color="#FDAE6B",
95. fill_color=None, line_width=2)
96. figures.append(p)
97. p = figure(title="asterisk")
98. p.scatter(x, y, marker="asterisk", size=sizes, color="#F0027F",
99. line_width=2)
100. figures.append(p)
101. p = figure(title="square_cross")
102. p.scatter(x, y, marker="square_cross", size=sizes, color="#7FC97F",
103. fill_color=None, line_width=2)
104. figures.append(p)
105. p = figure(title="diamond_cross")
106. p.scatter(x, y, marker="diamond_cross", size=sizes, color="#386CB0",
107. fill_color=None, line_width=2)
108. figures.append(p)
109. p = figure(title="circle_cross")
110. p.scatter(x, y, marker="circle_cross", size=sizes, color="#FB8072",
111. fill_color=None, line_width=2)
112. figures.append(p)
113. # 网格展示
114. show(gridplot(figures, ncols=4, plot_width=200, plot_height=200))
运行结果如图2-1和图2-2所示。
图2-1 代码示例2-1运行结果(1)
图2-2 代码示例2-1运行结果(2)
代码示例2-1展示了Bokeh figure()类的30种基本图元绘制方法,灵活地使用这些方法,可以绘制出各式各样的复杂图形,其中,有些图形可以用不同的基本图元实现,读者在学习过程中可以做不同的尝试。
代码示例 2-2
1. from bokeh.sampledata.iris import flowers
2. from bokeh.transform import factor_cmap, factor_mark
3. # 鸢尾花品种及分类标记
4. SPECIES = ['setosa', 'versicolor', 'virginica']
5. MARKERS = ['hex', 'circle_x', 'triangle']
6. # 画布
7. p = figure(title = "Iris Morphology", background_fill_color="#fafafa")
8. # 绘图
9. p.scatter("petal_length", "sepal_width", source=flowers, legend="species", fill_alpha=0.4, size=12,
10. marker=factor_mark('species', MARKERS, SPECIES),
11. color=factor_cmap('species', 'Category10_3', SPECIES))
12. # 其他
13. p.xaxis.axis_label = 'Petal Length'
14. p.yaxis.axis_label = 'Sepal Width'
15. # 显示
16. show(p)
运行结果如图2-3所示。
图2-3 代码示例2-2运行结果
代码示例2-2以散点图展示了机器学习中大名鼎鼎的鸢尾花数据集的分类结果。在2.2节中,我们可以看到circle的方法和scatter方法均可以实现散点图的绘制。本节着重了解figure()类的常用基本属性。
figure(**kwargs)常用基本属性。
'plot_height':400(画布高度,默认值400)
'plot_width':400
'renderers':[GlyphRenderer(id='1037',...)](自定义渲染数据)
'x_range':DataRange1d(id='1003',...)(x轴范围)
'x_scale':LinearScale(id='1007',...)(x轴刻度比例)
'y_range':DataRange1d(id='1005',...)(y轴范围)
'y_scale':LinearScale(id='1009',...)(y轴刻度比例)
'title':Title(id='1040',...)(标题)
'toolbar':Toolbar(id='1027',...)(工具栏)
id6W2lU72KnhUy4S97iJiB5dCbWWrSJXDIG8vpKeH/VJqUv4KfaUqZHD/vlj3bJt
更多推荐
所有评论(0)