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

本文借助Plotly Express提供的几个样例库进行密度图、小提琴图、箱线图、地图、趋势图,还有用于实现数据预探索的各种关系图、直方图等基本图形的实现。

plotly介于seaborn和pyechart之间,在表达丰富度形式上优于seaborn,在定制化程度上高于pyechart。

代码示例

  1. import plotly.express as px

  2. df = px.data.iris().query('species_id==1')

  3. # marginal_x'rug' 密度图, 'box' 箱线图, 'violin' 小提琴图, or 'histogram'直方图。

  4. # 如果设置,则在主图上方绘制一个水平子图,以可视化x分布。

  5. # marginal_y–地毯、盒子、小提琴或柱状图中的一种。

  6. # 如果设置,则在主图的右侧绘制一个垂直子图,以显示y分布。

  7. # 鸢尾花类型=1sepal_widthsepal_length散点图,x轴为密度图,y轴为直方图

  8. fig = px.scatter(df, x="sepal_width", y="sepal_length",

  9. marginal_x="rug", marginal_y="histogram")

  10. fig.show()

  11. 关于Python可视化Dash工具—plotly中级图表_数据

  12. # 鸢尾花类型=1sepal_widthsepal_length散点图,x轴为箱线图,y轴为小提琴图

  13. fig = px.scatter(df, x="sepal_width", y="sepal_length",

  14. marginal_x="box", marginal_y="violin")

  15. fig.show()

  16. 关于Python可视化Dash工具—plotly中级图表_直方图_02

  17. df = px.data.iris()

  18. # 所有花卉,x轴为箱线图,y轴为小提琴图,颜色以鸢尾花类型分类

  19. fig = px.scatter(df, x="sepal_width", y="sepal_length",

  20. color="species", size='petal_length',

  21. hover_data=['petal_width'], hover_name="species",

  22. title="鸢尾花分类展示",

  23. marginal_x="box", marginal_y="violin")

  24. fig.show()

  25. 关于Python可视化Dash工具—plotly中级图表_直方图_03

  26. # 密度热力图,鸢尾花类型=1sepal_widthsepal_length散点图,x轴为密度图,y轴为直方图

  27. fig = px.density_heatmap(df.query('species_id==1'),

  28. x="sepal_width", y="sepal_length",

  29. marginal_x="rug",

  30. marginal_y="histogram"

  31. )

  32. fig.show()

  33. 关于Python可视化Dash工具—plotly中级图表_数据_04

  34. # 密度热力图,行轴用sex,列轴用smoker进行多维度展示

  35. df = px.data.tips()

  36. fig = px.density_heatmap(df, x="total_bill", y="tip",

  37. facet_row="sex", facet_col="smoker")

  38. fig.show()

  39. 关于Python可视化Dash工具—plotly中级图表_直方图_05

  40. df = px.data.iris()

  41. # 直方图,只有x轴,箱线图

  42. fig = px.histogram(df, x="sepal_length", color="species",

  43. marginal="box")

  44. fig.show()

  45. 关于Python可视化Dash工具—plotly中级图表_数据_06

  46. # 在原有直方图基础上,增加20个区间

  47. fig = px.histogram(df, x="sepal_length", color="species",

  48. marginal="box" ,nbins=20)

  49. fig.show()

  50. 关于Python可视化Dash工具—plotly中级图表_直方图_07

  51. # 箱线图

  52. fig = px.box(df, x="species", y="sepal_length",

  53. color='species')

  54. fig.show()

  55. 关于Python可视化Dash工具—plotly中级图表_json_08

  56. # 在箱线图上追加散点

  57. fig = px.box(df, x="species", y="sepal_length",

  58. color='species',points='all')

  59. fig.show()

  60. 关于Python可视化Dash工具—plotly中级图表_直方图_09

  61. # 散点图矩阵,不指定列会把所有列带出来展示

  62. df = px.data.iris()

  63. fig = px.scatter_matrix(df)

  64. fig.show()

  65. 关于Python可视化Dash工具—plotly中级图表_json_10

  66. # 散点图矩阵,以鸢尾花颜色分类

  67. fig = px.scatter_matrix(df,

  68. dimensions=["sepal_width", "sepal_length",

  69. "petal_width", "petal_length"],

  70. color="species")

  71. fig.show()

  72. 关于Python可视化Dash工具—plotly中级图表_json_11

  73. # 散点图,用年份做列分割,每行限定4

  74. df = px.data.gapminder()

  75. fig = px.scatter(df, x='gdpPercap', y='lifeExp', color='continent', size='pop',

  76. facet_col='year', facet_col_wrap=4)

  77. fig.show()

  78. 关于Python可视化Dash工具—plotly中级图表_直方图_12

  79. # 地图展示,不过精确度太差

  80. df = px.data.election()

  81. # pandas.melt(frame, id_vars=None, value_vars=None,

  82. # var_name=None, value_name='value', col_level=None)

  83. # 参数解释:宽数据--->>长数据

  84. # frame:要处理的数据集。

  85. # id_vars:不需要被转换的列名。

  86. # value_vars:需要转换的列名,如果剩下的列全部都要转换,就不用写了。

  87. # var_namevalue_name是自定义设置对应的列名。

  88. # col_level :如果列是MultiIndex,则使用此级别。

  89. df = df.melt(id_vars="district", value_vars=["Coderre", "Bergeron", "Joly"],

  90. var_name="candidate", value_name="votes")

  91. # 获取地图

  92. geojson = px.data.election_geojson()

  93. # 地图展示

  94. fig = px.choropleth(df, geojson=geojson, color="votes", facet_col="candidate",

  95. locations="district", featureidkey="properties.district",

  96. projection="mercator"

  97. )

  98. fig.update_geos(fitbounds="locations", visible=False)

  99. fig.show()

  100. 关于Python可视化Dash工具—plotly中级图表_数据_13

  101. # 条带图

  102. df = px.data.iris()

  103. fig = px.strip(df, x="sepal_width", y="sepal_length",

  104. color="species", facet_col="species")

  105. fig.show()

  106. 关于Python可视化Dash工具—plotly中级图表_json_14

  107. # 密度轮廓图

  108. df = px.data.gapminder().query("year == 2007 | year==1957 | year==1982")

  109. fig = px.density_contour(df, x="gdpPercap", y="lifeExp", z="pop",

  110. facet_col="year",color="continent")

  111. fig.show()

  112. 关于Python可视化Dash工具—plotly中级图表_数据_15

  113. df = px.data.gapminder().query("year == 2007")

  114. fig = px.density_contour(df, x="gdpPercap", y="lifeExp", z="pop")

  115. fig.update_traces(contours_coloring="fill",

  116. contours_showlabels = True)

  117. fig.show()

  118. 关于Python可视化Dash工具—plotly中级图表_json_16

  119. # 散点图加趋势线图

  120. df = px.data.tips()

  121. fig = px.scatter(

  122. df, x='total_bill', y='tip', opacity=0.65,

  123. trendline='ols', trendline_color_override='darkblue'

  124. )

  125. # 散点图加趋势线图

  126. df = px.data.iris()

  127. fig = px.scatter(df, x="sepal_width", y="sepal_length",

  128. color="species", facet_col="species",

  129. trendline='ols',

  130. trendline_color_override='darkblue')

  131. fig.show()

  132. 关于Python可视化Dash工具—plotly中级图表_直方图_17



最后请多多关注,谢谢!

关于Python可视化Dash工具—plotly中级图表_json_18