Plotly 简明教程
Plotly with Matplotlib and Chart Studio
本章介绍了名为 Matplotlib 的数据可视化库和名为 Chart Studio 的在线图表制作工具。
This chapter deals with data visualization library titled Matplotlib and online plot maker named Chart Studio.
Matplotlib
Matplotlib 是一个流行的 Python 数据可视化库,能够制作可用于生产但静态的图表。您可以借助 plotly.tools 模块中的 mpl_to_plotly() 函数将静态 matplotlib figures 转换为交互式图表。
Matplotlib is a popular Python data visualization library capable of producing production-ready but static plots. you can convert your static matplotlib figures into interactive plots with the help of mpl_to_plotly() function in plotly.tools module.
以下脚本使用 Matplotlib’s PyPlot API 生成 Sine wave Line plot 。
Following script produces a Sine wave Line plot using Matplotlib’s PyPlot API.
from matplotlib import pyplot as plt
import numpy as np
import math
#needed for definition of pi
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)
plt.plot(x,y)
plt.xlabel("angle")
plt.ylabel("sine")
plt.title('sine wave')
plt.show()
现在,我们按如下方式将其转换为绘图线图形:
Now we shall convert it into a plotly figure as follows −
fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly(fig)
py.iplot(plotly_fig)
代码的输出如下所示 −
The output of the code is as given below −

Chart Studio
Chart Studio 是 Plotly 提供的在线图表制作工具。它提供了一个图形用户界面,用于将数据导入网格并对其进行分析,并使用统计工具。可以嵌入或下载图表。它主要用于更快速、更高效地创建图表。
Chart Studio is an online plot maker tool made available by Plotly. It provides a graphical user interface for importing and analyzing data into a grid and using stats tools. Graphs can be embedded or downloaded. It is mainly used to enable creating graphs faster and more efficiently.
登录 plotly 帐户后,通过访问链接 https://plot.ly/create 启动图表工作室应用程序。网页在图表区域下方提供了一个空白工作表。Chart Studio 允许您通过按 + trace button 来添加图表轨迹。
After logging in to plotly’s account, start the chart studio app by visiting the link https://plot.ly/create. The web page offers a blank work sheet below the plot area. Chart Studio lets you to add plot traces by pushing + trace button.

菜单中提供了各种图表结构元素,如注释、样式等,以及保存、导出和共享图表的工具。
Various plot structure elements such as annotations, style etc. as well as facility to save, export and share the plots is available in the menu.
让我们在工作表中添加数据并从轨迹类型中添加 choose bar plot trace 。
Let us add data in the worksheet and add choose bar plot trace from the trace types.

单击类型文本框,然后选择条形图。
Click in the type text box and select bar plot.

然后,为 x 和 y 轴提供数据列,并输入图表标题。
Then, provide data columns for x and y axes and enter plot title.
