当前位置:   article > 正文

1)Plotly可视化画Scatter图_plotly scatter

plotly scatter

 文章最前: 我是Octopus,这个名字来源于我的中文名--章鱼;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github ;这博客是记录我学习的点点滴滴,如果您对 Python、Java、AI、算法有兴趣,可以关注我的动态,一起学习,共同进步。

最近由于工作需要,需要对数据进行一些可视化操作,找到了Plotly库,现在就学习一下plotly进行绘图

1.首先导入包 

  1. import numpy as np
  2. import pandas as pd
  3. import plotly.graph_objects as go
  4. import plotly.offline as po
  5. from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
  6. import matplotlib.pyplot as plt
  7. import plotly.express as px
  8. import random
  9. import plotly.figure_factory as ff

 2)基础散点图可视化

  1. random_x = np.random.randint(1, 100, 50)
  2. random_y = np.random.randint(1, 100, 50)
  3. data = [
  4. go.Scatter(x = random_x, y = random_y, mode = 'markers')
  5. ]
  6. layout = go.Layout(
  7. xaxis = dict(
  8. showgrid=True, # Hide Gridlines
  9. showline=False, # Hide X-Axis
  10. ),
  11. yaxis = dict(
  12. showgrid=True, # Hide Gridlines
  13. showline=False, # Hide X-Axis
  14. ),
  15. )
  16. fig = go.Figure(data = data, layout = layout)
  17. iplot(fig)

3) 散点图设置大小

  1. # Changing Marker size , shape & color using Marker parameter
  2. x_val = np.random.randint(1,100,50)
  3. y_val = np.random.randint(1,100,50)
  4. data = [go.Scatter(
  5. x = x_val,
  6. y = y_val,
  7. mode = 'markers',
  8. marker = dict(
  9. size = 10,
  10. color = '#91bd3a', #color of marker
  11. symbol = 'circle', # Shape of scatter plot
  12. line = dict(width = 1) #width of boundary
  13. )
  14. )
  15. ]
  16. layout = go.Layout(
  17. xaxis=dict(
  18. showgrid=False, # Hide Gridlines
  19. showline=False, # Hide X-Axis
  20. ),
  21. yaxis=dict(
  22. showgrid=False, # Hide Gridlines
  23. showline=False, # Hide X-Axis
  24. ),
  25. )
  26. fig = go.Figure(data=data,layout=layout)
  27. iplot(fig)

4)定义标题和x轴以及y轴

  1. # Defining Labels (X-Axis & Y-Axis label , Graph tile)
  2. x_val = np.random.randint(1,100,50)
  3. y_val = np.random.randint(1,100,50)
  4. data = [go.Scatter(
  5. x = x_val,
  6. y = y_val,
  7. mode = 'markers',
  8. marker = dict(
  9. size = 10,
  10. color = '#4ED700',
  11. symbol = 'circle',
  12. line = dict(width = 1,color = '#0E8700')
  13. )
  14. )
  15. ]
  16. layout = go.Layout(
  17. title = '$Scatter Plot$', # Title
  18. xaxis = dict(title = '$X-Axis$',showgrid=False,showline=False), # x-axis label
  19. yaxis = dict(title = '$Y-Axis$',showgrid=False,showline=False), # y-axis label
  20. )
  21. fig = go.Figure(data=data, layout=layout)
  22. iplot(fig)

5)设置不同的颜色

  1. x_values = np.linspace(0, 100, 100) # 100 evenly spaced values
  2. y_values = np.random.randn(100) # 100 random values
  3. trace0 = go.Scatter(
  4. x = x_values,
  5. y = y_values,
  6. mode = 'markers',
  7. marker = dict(
  8. size = 7,
  9. color = '#F4D03F',
  10. symbol = 'circle',
  11. line = dict(width = 1,color = '#0E8700')
  12. )
  13. )
  14. trace1 = go.Scatter(
  15. x = x_values,
  16. y = y_values-5,
  17. mode = 'markers',
  18. marker = dict(size = 7,
  19. color = '#A9DFBF',
  20. symbol = 'circle',
  21. line = dict(width = 1,color = '#0E8700')
  22. )
  23. )
  24. data = [trace0, trace1]
  25. layout = go.Layout(
  26. xaxis=dict(
  27. showgrid=False, # Hide Gridlines
  28. showline=False, # Hide X-Axis
  29. ),
  30. yaxis=dict(
  31. showgrid=False, # Hide Gridlines
  32. showline=False, # Hide X-Axis
  33. ),
  34. )
  35. fig = go.Figure(data=data,layout=layout)
  36. iplot(fig)

 6)去掉坐标轴

  1. data = [go.Scatter(
  2. x = auto_df["engine-size"],
  3. y = auto_df["wheel-base"],
  4. mode = 'markers',
  5. marker = dict(size = 7,
  6. color = '#4ED700',
  7. symbol = 'circle',
  8. line = dict(width = 1,color = '#0E8700')
  9. )
  10. )
  11. ]
  12. layout = go.Layout(
  13. title = 'EngineSize-WheelBase', # Chart Title
  14. xaxis = dict(title = 'Engine Size$',showgrid=False,showline=False), # x-axis label
  15. yaxis = dict(title = '$Wheel Base$',showgrid=False,showline=False), # y-axis label
  16. )
  17. fig = go.Figure(data=data, layout=layout)
  18. iplot(fig)

 7) 绘制不同颜色

  1. # trace0 will capture all smokers
  2. trace0 = go.Scatter(
  3. x = auto_df[auto_df["fuel-type"]=='gas']["engine-size"],
  4. y = auto_df[auto_df["fuel-type"]=='gas']["wheel-base"],
  5. mode = 'markers',
  6. name = 'Gas',
  7. marker = dict(size = 7, color = '#F39C12',symbol = 'circle',line = dict(width = 1))
  8. )
  9. # trace1 will capture all non-smokers
  10. trace1 = go.Scatter(
  11. x = auto_df[auto_df["fuel-type"]=='diesel']["engine-size"],
  12. y = auto_df[auto_df["fuel-type"]=='diesel']["wheel-base"],
  13. mode = 'markers',
  14. name = 'Diesel',
  15. marker = dict(size = 7, color = '#8BC34A',symbol = 'circle',line = dict(width = 1))
  16. )
  17. layout = go.Layout(
  18. title = '$Scatter Plot$', # Title
  19. xaxis = dict(title = '$Engine Size$',showgrid=False,showline=False), # x-axis label
  20. yaxis = dict(title = '$Wheel Base$',showgrid=False,showline=False), # y-axis label
  21. )
  22. data = [trace0, trace1]
  23. fig = go.Figure(data=data,layout=layout)
  24. iplot(fig)

8)不同的点设置子图 

  1. # Display multiple Scatter plots in one figure using Subplots
  2. from plotly.subplots import make_subplots
  3. #Subplot initialization
  4. fig = make_subplots(
  5. rows=1,
  6. cols=2,
  7. subplot_titles=("Subplot-1", "Subplot-2")
  8. )
  9. # Subplot - 1 (Add graph object trace to a figure)
  10. fig.add_trace(go.Scatter
  11. (
  12. x = x_val,
  13. y = y_val,
  14. mode = 'markers',
  15. marker = dict(size = 10, color = 'crimson',symbol = 'circle',line = dict(width = 1,color = '#0E8700'))
  16. ),
  17. row=1, col=1
  18. )
  19. # Add graph object trace to a figure (Subplot-2)
  20. fig.add_trace(go.Scatter
  21. (
  22. x = x_val,
  23. y = y_val,
  24. mode = 'markers',
  25. marker = dict(size = 10, color = 'gold',symbol = 'circle',line = dict(width = 1))
  26. ),
  27. row=1, col=2
  28. )
  29. # Hide grid lines
  30. fig.update_xaxes(showgrid=False)
  31. fig.update_yaxes(showgrid=False)
  32. fig.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/527414
推荐阅读
相关标签
  

闽ICP备14008679号