当前位置:   article > 正文

python plotly 使用教程_pip install plotly

pip install plotly

1、plotly介绍

lotly的Python图形库使互动的出版质量图表成为在线。 如何制作线图,散点图,面积图,条形图,误差线,箱形图,直方图,热图,子图,多轴,极坐标图和气泡图的示例。

推荐最好使用jupyter notebook,使用pycharm的话不是很方便。

2、安装

pip install plotly

2、使用

1)在线使用

在setting里找到用户名和api key

image.png

  1. ##在线使用
  2. import plotly.plotly as py
  3. from plotly import tools
  4. from plotly.graph_objs import *
  5. tools.set_credentials_file(username='yours', api_ke='yours')
  6. trace0 = Scatter(
  7. x=[1, 2, 3, 4],
  8. y=[10, 15, 13, 17],
  9. mode='markers'
  10. )
  11. trace1 = Scatter(
  12. x=[1, 2, 3, 4],
  13. y=[16, 5, 11, 9]
  14. )
  15. data = Data([trace0, trace1])
  16. py.iplot(data)

散点图

散点图.png

2)offline

  1. import plotly.offline as of
  2. import plotly.graph_objs as go
  3. of.offline.init_notebook_mode(connected=True)
  4. trace0 = go.Scatter(
  5. x=[1, 2, 3, 4],
  6. y=[10, 15, 13, 17],
  7. mode='markers'
  8. )
  9. trace1 = go.Scatter(
  10. x=[1, 2, 3, 4],
  11. y=[16, 5, 11, 9]
  12. )
  13. data = go.Data([trace0, trace1])
  14. of.plot(data)

3、其他图

下面我们画几个其他类型的图

柱状图

  1. import plotly.figure_factory as ff
  2. import pandas as pd
  3. df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
  4. data = [Bar(x=df.School,
  5. y=df.Gap)]
  6. py.iplot(data)

image.png

3D图

  1. import numpy as np
  2. s = np.linspace(0, 2 * np.pi, 240)
  3. t = np.linspace(0, np.pi, 240)
  4. tGrid, sGrid = np.meshgrid(s, t)
  5. r = 2 + np.sin(7 * sGrid + 5 * tGrid) # r = 2 + sin(7s+5t)
  6. x = r * np.cos(sGrid) * np.sin(tGrid) # x = r*cos(s)*sin(t)
  7. y = r * np.sin(sGrid) * np.sin(tGrid) # y = r*sin(s)*sin(t)
  8. z = r * np.cos(tGrid) # z = r*cos(t)
  9. surface = Surface(x=x, y=y, z=z)
  10. data = Data([surface])
  11. layout = Layout(
  12. title='Parametric Plot',
  13. scene=Scene(
  14. xaxis=XAxis(
  15. gridcolor='rgb(255, 255, 255)',
  16. zerolinecolor='rgb(255, 255, 255)',
  17. showbackground=True,
  18. backgroundcolor='rgb(230, 230,230)'
  19. ),
  20. yaxis=YAxis(
  21. gridcolor='rgb(255, 255, 255)',
  22. zerolinecolor='rgb(255, 255, 255)',
  23. showbackground=True,
  24. backgroundcolor='rgb(230, 230,230)'
  25. ),
  26. zaxis=ZAxis(
  27. gridcolor='rgb(255, 255, 255)',
  28. zerolinecolor='rgb(255, 255, 255)',
  29. showbackground=True,
  30. backgroundcolor='rgb(230, 230,230)'
  31. )
  32. )
  33. )
  34. fig = Figure(data=data, layout=layout)
  35. py.iplot(fig,)

image.png

折线图

  1. import numpy as np
  2. N = 100
  3. random_x = np.linspace(0, 1, N)
  4. random_y0 = np.random.randn(N)+5
  5. random_y1 = np.random.randn(N)
  6. random_y2 = np.random.randn(N)-5
  7. # Create traces
  8. trace0 = go.Scatter(
  9. x = random_x,
  10. y = random_y0,
  11. mode = 'markers',
  12. name = 'markers'
  13. )
  14. trace1 = go.Scatter(
  15. x = random_x,
  16. y = random_y1,
  17. mode = 'lines+markers',
  18. name = 'lines+markers'
  19. )
  20. trace2 = go.Scatter(
  21. x = random_x,
  22. y = random_y2,
  23. mode = 'lines',
  24. name = 'lines'
  25. )
  26. data = [trace0, trace1, trace2]
  27. py.iplot(data)

image.png

堆叠图

  1. trace1 = go.Bar(
  2. x=['giraffes', 'orangutans', 'monkeys'],
  3. y=[20, 14, 23],
  4. name='SF Zoo'
  5. )
  6. trace2 = go.Bar(
  7. x=['giraffes', 'orangutans', 'monkeys'],
  8. y=[12, 18, 29],
  9. name='LA Zoo'
  10. )
  11. data = [trace1, trace2]
  12. layout = go.Layout(
  13. barmode='stack'
  14. )
  15. fig = go.Figure(data=data, layout=layout)
  16. py.iplot(fig)

image.png

pie

  1. labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
  2. values = [4500,2500,1053,500]
  3. colors = ['#FEBFB3', '#E1396C', '#96D38C', '#D0F9B1']
  4. trace = go.Pie(labels=labels, values=values,
  5. hoverinfo='label+percent', textinfo='value',
  6. textfont=dict(size=20),
  7. marker=dict(colors=colors,
  8. line=dict(color='#000000', width=2)))
  9. py.iplot([trace])

image.png

不知道叫什么图

  1. title = 'Main Source for News'
  2. labels = ['Television', 'Newspaper', 'Internet', 'Radio']
  3. colors = ['rgba(67,67,67,1)', 'rgba(115,115,115,1)', 'rgba(49,130,189, 1)', 'rgba(189,189,189,1)']
  4. mode_size = [8, 8, 12, 8]
  5. line_size = [2, 2, 4, 2]
  6. x_data = [
  7. [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],
  8. [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],
  9. [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],
  10. [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],
  11. ]
  12. y_data = [
  13. [74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69],
  14. [45, 42, 50, 46, 36, 36, 34, 35, 32, 31, 31, 28],
  15. [13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50],
  16. [18, 21, 18, 21, 16, 14, 13, 18, 17, 16, 19, 23],
  17. ]
  18. traces = []
  19. for i in range(0, 4):
  20. traces.append(go.Scatter(
  21. x=x_data[i],
  22. y=y_data[i],
  23. mode='lines',
  24. line=dict(color=colors[i], width=line_size[i]),
  25. connectgaps=True,
  26. ))
  27. traces.append(go.Scatter(
  28. x=[x_data[i][0], x_data[i][11]],
  29. y=[y_data[i][0], y_data[i][11]],
  30. mode='markers',
  31. marker=dict(color=colors[i], size=mode_size[i])
  32. ))
  33. layout = go.Layout(
  34. xaxis=dict(
  35. showline=True,
  36. showgrid=False,
  37. showticklabels=True,
  38. linecolor='rgb(204, 204, 204)',
  39. linewidth=2,
  40. autotick=False,
  41. ticks='outside',
  42. tickcolor='rgb(204, 204, 204)',
  43. tickwidth=2,
  44. ticklen=5,
  45. tickfont=dict(
  46. family='Arial',
  47. size=12,
  48. color='rgb(82, 82, 82)',
  49. ),
  50. ),
  51. yaxis=dict(
  52. showgrid=False,
  53. zeroline=False,
  54. showline=False,
  55. showticklabels=False,
  56. ),
  57. autosize=False,
  58. margin=dict(
  59. autoexpand=False,
  60. l=100,
  61. r=20,
  62. t=110,
  63. ),
  64. showlegend=False,
  65. )
  66. annotations = []
  67. # Adding labels
  68. for y_trace, label, color in zip(y_data, labels, colors):
  69. # labeling the left_side of the plot
  70. annotations.append(dict(xref='paper', x=0.05, y=y_trace[0],
  71. xanchor='right', yanchor='middle',
  72. text=label + ' {}%'.format(y_trace[0]),
  73. font=dict(family='Arial',
  74. size=16,
  75. color=colors,),
  76. showarrow=False))
  77. # labeling the right_side of the plot
  78. annotations.append(dict(xref='paper', x=0.95, y=y_trace[11],
  79. xanchor='left', yanchor='middle',
  80. text='{}%'.format(y_trace[11]),
  81. font=dict(family='Arial',
  82. size=16,
  83. color=colors,),
  84. showarrow=False))
  85. # Title
  86. annotations.append(dict(xref='paper', yref='paper', x=0.0, y=1.05,
  87. xanchor='left', yanchor='bottom',
  88. text='Main Source for News',
  89. font=dict(family='Arial',
  90. size=30,
  91. color='rgb(37,37,37)'),
  92. showarrow=False))
  93. # Source
  94. annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.1,
  95. xanchor='center', yanchor='top',
  96. text='Source: PewResearch Center & ' +
  97. 'Storytelling with data',
  98. font=dict(family='Arial',
  99. size=12,
  100. color='rgb(150,150,150)'),
  101. showarrow=False))
  102. layout['annotations'] = annotations
  103. fig = go.Figure(data=traces, layout=layout)
  104. py.iplot(fig)

image.png

4、各种具体语法

pdf

image.png

5、总结

画的图真是好看,而且划过的图会自动上传到云端。

image.png

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

闽ICP备14008679号