飞道的博客

关于Python可视化Dash工具—plotly基本图形

222人阅读  评论(0)

Plotly Express是对 Plotly.py 的高级封装,内置了大量实用、现代的绘图模板,用户只需调用简单的API函数,即可快速生成漂亮的互动图表,可满足90%以上的应用场景。

本文借助Plotly Express提供的几个样例库进行散点图、折线图、饼图、柱状图、气泡图、桑基图、玫瑰环图、堆积图、二维面积图、甘特图等基本图形的实现。

代码示例


   
  1. import plotly.express as px
  2. df = px.data.iris()
  3. #Index([ 'sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species', 'species_id'],dtype= 'object')
  4. # sepal_length sepal_width ... species species_id
  5. # 0 5.1 3.5 ... setosa 1
  6. # 1 4.9 3.0 ... setosa 1
  7. # 2 4.7 3.2 ... setosa 1
  8. # .. ... ... ... ... ...
  9. # 149 5.9 3.0 ... virginica 3
  10. # plotly.express.scatter(data_frame=None, x=None, y=None,
  11. # color=None, symbol=None, size=None,
  12. # hover_name=None, hover_data=None, custom_data=None, text=None,
  13. # facet_row=None, facet_col=None, facet_col_wrap= 0, facet_row_spacing=None, facet_col_spacing=None,
  14. # error_x=None, error_x_minus=None, error_y=None, error_y_minus=None,
  15. # animation_frame=None, animation_group=None,
  16. # category_orders=None, labels=None, orientation=None,
  17. # color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None,
  18. # range_color=None, color_continuous_midpoint=None,
  19. # symbol_sequence=None, symbol_map=None, opacity=None,
  20. # size_max=None, marginal_x=None, marginal_y=None,
  21. # trendline=None, trendline_color_override=None,
  22. # log_x=False, log_y=False, range_x=None, range_y=None,
  23. # render_mode= 'auto', title=None, template=None, width=None, height=None)
  24. # 以sepal_width,sepal_length制作标准散点图
  25. fig = px.scatter(df, x= "sepal_width", y= "sepal_length")
  26. fig.show()
  27. #以鸢尾花类型-species作为不同颜色区分标志 color
  28. fig = px.scatter(df, x= "sepal_width", y= "sepal_length", color= "species")
  29. fig.show()
  30. #追加petal_length作为散点大小,变位气泡图 size
  31. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  32. color= "species",size= 'petal_length')
  33. fig.show()
  34. #追加petal_width作为额外列,在悬停工具提示中显示为额外数据 hover_data
  35. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  36. color= "species", size= 'petal_length',
  37. hover_data=[ 'petal_width'])
  38. fig.show()
  39. #以鸢尾花类型-species区分散点的形状 symbol
  40. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  41. symbol= "species" ,color= "species",
  42. size= 'petal_length',
  43. hover_data=[ 'petal_width'])
  44. fig.show()
  45. #追加petal_width作为额外列,在悬停工具提示中以粗体显示。 hover_name
  46. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  47. symbol= "species" ,color= "species",
  48. size= 'petal_length',
  49. hover_data=[ 'petal_width'], hover_name= "species")
  50. fig.show()
  51. #以鸢尾花类型编码-species_id作为散点的文本值 text
  52. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  53. symbol= "species" ,color= "species",
  54. size= 'petal_length',
  55. hover_data=[ 'petal_width'], hover_name= "species",
  56. text= "species_id")
  57. fig.show()
  58. #追加图表标题 title
  59. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  60. symbol= "species" ,color= "species", size= 'petal_length',
  61. hover_data=[ 'petal_width'], hover_name= "species",
  62. text= "species_id",title= "鸢尾花分类展示")
  63. fig.show()
  64. #以鸢尾花类型-species作为动画播放模式 animation_frame
  65. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  66. symbol= "species" ,color= "species", size= 'petal_length',
  67. hover_data=[ 'petal_width'], hover_name= "species",
  68. text= "species_id",title= "鸢尾花分类展示",
  69. animation_frame= "species")
  70. fig.show()
  71. #固定X、Y最大值最小值范围range_x,range_y,防止动画播放时超出数值显示
  72. fig = px.scatter(df, x= "sepal_width", y= "sepal_length",
  73. symbol= "species" ,color= "species", size= 'petal_length',
  74. hover_data=[ 'petal_width'], hover_name= "species",
  75. text= "species_id",title= "鸢尾花分类展示",
  76. animation_frame= "species",range_x=[ 1.5, 4.5],range_y=[ 4, 8.5])
  77. fig.show()
  78. df = px.data.gapminder().query( "country=='China'")
  79. # Index([ 'country', 'continent', 'year', 'lifeExp', 'pop', 'gdpPercap', 'iso_alpha', 'iso_num'],dtype= 'object')
  80. # country continent year ... gdpPercap iso_alpha iso_num
  81. # 288 China Asia 1952 ... 400.448611 CHN 156
  82. # 289 China Asia 1957 ... 575.987001 CHN 156
  83. # 290 China Asia 1962 ... 487.674018 CHN 156
  84. # plotly.express.line(data_frame=None, x=None, y=None,
  85. # line_group=None, color=None, line_dash=None,
  86. # hover_name=None, hover_data=None, custom_data=None, text=None,
  87. # facet_row=None, facet_col=None, facet_col_wrap= 0,
  88. # facet_row_spacing=None, facet_col_spacing=None,
  89. # error_x=None, error_x_minus=None, error_y=None, error_y_minus=None,
  90. # animation_frame=None, animation_group=None,
  91. # category_orders=None, labels=None, orientation=None,
  92. # color_discrete_sequence=None, color_discrete_map=None,
  93. # line_dash_sequence=None, line_dash_map=None,
  94. # log_x=False, log_y=False,
  95. # range_x=None, range_y=None,
  96. # line_shape=None, render_mode= 'auto', title=None,
  97. # template=None, width=None, height=None)
  98. # 显示中国的人均寿命
  99. fig = px.line(df, x= "year", y= "lifeExp", title= '中国人均寿命')
  100. fig.show()
  101. # 以不同颜色显示亚洲各国的人均寿命
  102. df = px.data.gapminder().query( "continent == 'Asia'")
  103. fig = px.line(df, x= "year", y= "lifeExp", color= "country",
  104. hover_name= "country")
  105. fig.show()
  106. # line_group= "country" 达到按国家去重的目的
  107. df = px.data.gapminder().query( "continent != 'Asia'") # remove Asia for visibility
  108. fig = px.line(df, x= "year", y= "lifeExp", color= "continent",
  109. line_group= "country", hover_name= "country")
  110. fig.show()
  111. # bar图
  112. df = px.data.gapminder().query( "country == 'China'")
  113. fig = px.bar(df, x= 'year', y= 'lifeExp')
  114. fig.show()
  115. df = px.data.gapminder().query( "continent == 'Asia'")
  116. fig = px.bar(df, x= 'year', y= 'lifeExp',color= "country" )
  117. fig.show()
  118. df = px.data.gapminder().query( "country == 'China'")
  119. fig = px.bar(df, x= 'year', y= 'pop',
  120. hover_data=[ 'lifeExp', 'gdpPercap'], color= 'lifeExp',
  121. labels={ 'pop': 'population of China'}, height= 400)
  122. fig.show()
  123. fig = px.bar(df, x= 'year', y= 'pop',
  124. hover_data=[ 'lifeExp', 'gdpPercap'], color= 'pop',
  125. labels={ 'pop': 'population of China'}, height= 400)
  126. fig.show()
  127. df = px.data.medals_long()
  128. # # nation medal count
  129. # # 0 South Korea gold 24
  130. # # 1 China gold 10
  131. # # 2 Canada gold 9
  132. # # 3 South Korea silver 13
  133. # # 4 China silver 15
  134. # # 5 Canada silver 12
  135. # # 6 South Korea bronze 11
  136. # # 7 China bronze 8
  137. # # 8 Canada bronze 12
  138. fig = px.bar(df, x= "nation", y= "count", color= "medal",
  139. title= "Long-Form Input")
  140. fig.show()
  141. # 气泡图
  142. df = px.data.gapminder()
  143. # X轴以对数形式展现
  144. fig = px.scatter(df.query( "year==2007"), x= "gdpPercap", y= "lifeExp",
  145. size= "pop",
  146. color= "continent",hover_name= "country",
  147. log_x=True, size_max= 60)
  148. fig.show()
  149. # X轴以标准形式展现
  150. fig = px.scatter(df.query( "year==2007"), x= "gdpPercap", y= "lifeExp",
  151. size= "pop",
  152. color= "continent",hover_name= "country",
  153. log_x=False, size_max= 60)
  154. fig.show()
  155. # 饼状图
  156. px.data.gapminder().query( "year == 2007").groupby( 'continent').count()
  157. # country year lifeExp pop gdpPercap iso_alpha iso_num
  158. # continent
  159. # Africa 52 52 52 52 52 52 52
  160. # Americas 25 25 25 25 25 25 25
  161. # Asia 33 33 33 33 33 33 33
  162. # Europe 30 30 30 30 30 30 30
  163. # Oceania 2 2 2 2 2 2 2
  164. df = px.data.gapminder().query( "year == 2007").query( "continent == 'Americas'")
  165. fig = px.pie(df, values= 'pop', names= 'country',
  166. title= 'Population of European continent')
  167. fig.show()
  168. df.loc[df[ 'pop'] < 10000000, 'country'] = 'Other countries'
  169. fig = px.pie(df, values= 'pop', names= 'country',
  170. title= 'Population of European continent',
  171. hover_name= 'country',labels= 'country')
  172. fig.update_traces(textposition= 'inside', textinfo= 'percent+label')
  173. fig.show()
  174. df.loc[df[ 'pop'] < 10000000, 'country'] = 'Other countries'
  175. fig = px.pie(df, values= 'pop', names= 'country',
  176. title= 'Population of European continent',
  177. hover_name= 'country',labels= 'country',
  178. color_discrete_sequence=px.colors.sequential.Blues)
  179. fig.update_traces(textposition= 'inside', textinfo= 'percent+label')
  180. fig.show()
  181. # 二维面积图
  182. df = px.data.gapminder()
  183. fig = px.area(df, x= "year", y= "pop", color= "continent",
  184. line_group= "country")
  185. fig.show()
  186. fig = px.area(df, x= "year", y= "pop", color= "continent",
  187. line_group= "country",
  188. color_discrete_sequence=px.colors.sequential.Blues)
  189. fig.show()
  190. df = px.data.gapminder().query( "year == 2007")
  191. fig = px.bar(df, x= "pop", y= "continent", orientation= 'h',
  192. hover_name= 'country',
  193. text= 'country',color= 'continent')
  194. fig.show()
  195. # 甘特图
  196. import pandas as pd
  197. df = pd.DataFrame([
  198. dict(Task= "Job A", Start= '2009-01-01', Finish= '2009-02-28',
  199. Completion_pct= 50, Resource= "Alex"),
  200. dict(Task= "Job B", Start= '2009-03-05', Finish= '2009-04-15',
  201. Completion_pct= 25, Resource= "Alex"),
  202. dict(Task= "Job C", Start= '2009-02-20', Finish= '2009-05-30',
  203. Completion_pct= 75, Resource= "Max")
  204. ])
  205. fig = px.timeline(df, x_start= "Start", x_end= "Finish", y= "Task",
  206. color= "Completion_pct")
  207. fig.update_yaxes(autorange= "reversed")
  208. fig.show()
  209. fig = px.timeline(df, x_start= "Start", x_end= "Finish", y= "Resource",
  210. color= "Resource")
  211. fig.update_yaxes(autorange= "reversed")
  212. fig.show()
  213. # 玫瑰环图
  214. df = px.data.tips()
  215. # total_bill tip sex smoker day time size
  216. # 0 16.99 1.01 Female No Sun Dinner 2
  217. # 1 10.34 1.66 Male No Sun Dinner 3
  218. # 2 21.01 3.50 Male No Sun Dinner 3
  219. # 3 23.68 3.31 Male No Sun Dinner 2
  220. # 4 24.59 3.61 Female No Sun Dinner 4
  221. fig = px.sunburst(df, path=[ 'day', 'time', 'sex'],
  222. values= 'total_bill')
  223. fig.show()
  224. import numpy as np
  225. df = px.data.gapminder().query( "year == 2007")
  226. fig = px.sunburst(df, path=[ 'continent', 'country'],
  227. values= 'pop',
  228. color= 'lifeExp', hover_data=[ 'iso_alpha'],
  229. color_continuous_scale= 'RdBu',
  230. color_continuous_midpoint=np.average(df[ 'lifeExp'],
  231. weights=df[ 'pop']))
  232. fig.show()
  233. df = px.data.gapminder().query( "year == 2007")
  234. fig = px.sunburst(df, path=[ 'continent', 'country'],
  235. values= 'pop',
  236. color= 'pop', hover_data=[ 'iso_alpha'],
  237. color_continuous_scale= 'RdBu')
  238. fig.show()
  239. # treemap图
  240. import numpy as np
  241. df = px.data.gapminder().query( "year == 2007")
  242. df[ "world"] = "world" # in order to have a single root node
  243. fig = px.treemap(df, path=[ 'world', 'continent', 'country'],
  244. values= 'pop',
  245. color= 'lifeExp', hover_data=[ 'iso_alpha'],
  246. color_continuous_scale= 'RdBu',
  247. color_continuous_midpoint=np.average(df[ 'lifeExp'],
  248. weights=df[ 'pop']))
  249. fig.show()
  250. fig = px.treemap(df, path=[ 'world', 'continent', 'country'], values= 'pop',
  251. color= 'pop', hover_data=[ 'iso_alpha'],
  252. color_continuous_scale= 'RdBu',
  253. color_continuous_midpoint=np.average(df[ 'lifeExp'],
  254. weights=df[ 'pop']))
  255. fig.show()
  256. fig = px.treemap(df, path=[ 'world', 'continent', 'country'], values= 'pop',
  257. color= 'lifeExp', hover_data=[ 'iso_alpha'],
  258. color_continuous_scale= 'RdBu')
  259. fig.show()
  260. fig = px.treemap(df, path=[ 'continent', 'country'], values= 'pop',
  261. color= 'lifeExp', hover_data=[ 'iso_alpha'],
  262. color_continuous_scale= 'RdBu')
  263. fig.show()
  264. fig = px.treemap(df, path=[ 'country'], values= 'pop',
  265. color= 'lifeExp', hover_data=[ 'iso_alpha'],
  266. color_continuous_scale= 'RdBu')
  267. fig.show()
  268. # 桑基图
  269. tips = px.data.tips()
  270. fig = px.parallel_categories(tips, color= "size",
  271. color_continuous_scale=px.colors.sequential.Inferno)
  272. fig.show()



转载:https://blog.csdn.net/baoqiangwang/article/details/114316272
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场