Home > Article > Backend Development > Python visualization | Python visualization advanced essentials - plotly
Plotly is a very famous and powerful open source data visualization framework. It builds an interactive web form based on browser display. Use charts to display information and create dozens of beautiful charts and maps.
There are two ways to draw images in Plotly, online and offline. Drawing requires registering an account to obtain an API key, which is more troublesome, so this article only introduces the offline drawing method.
There are two methods for offline drawing: plotly.offline.plot() and plotly.offline.iplot(). The former generates image files in html format in the current working directory in an offline way. and automatically open;
The latter is a special method in jupyter notebook, which embeds the generated graphics into the ipynb file. This article adopts the latter method (note that plotly is used in jupyter notebook .offline.iplot(), you need to run plotly.offline.init_notebook_mode() before to complete the initialization of the drawing code, otherwise an error will be reported).
The main parameters of plotly.offline.iplot() are as follows:
The following is a simple example:
import plotly import plotly.graph_objs as go '''初始化jupyter notebook中的绘图模式''' plotly.offline.init_notebook_mode() '''绘制一个基本的折线图,控制其尺寸为1600x600''' plotly.offline.iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image_height=600, image_width=1600)##2.2 graph object
Help on package plotly.graph_objs in plotly: NAME plotly.graph_objs DESCRIPTION graph_objs ========== This package imports definitions for all of Plotly's graph objects. For more information, run help(Obj) on any of the following objects defined here. The reason for the package graph_objs and the module graph_objs is to provide a clearer API for users. PACKAGE CONTENTS _area _bar _box _candlestick _carpet _choropleth _cone _contour _contourcarpet _deprecations _figure _figurewidget _frame _heatmap _heatmapgl _histogram _histogram2d _histogram2dcontour _layout _mesh3d _ohlc _parcoords _pie _pointcloud _sankey _scatter _scatter3d _scattercarpet _scattergeo _scattergl _scattermapbox _scatterpolar _scatterpolargl _scatterternary _splom _surface _table _violin area (package) bar (package) box (package) candlestick (package) carpet (package) choropleth (package) cone (package) contour (package) contourcarpet (package) graph_objs graph_objs_tools heatmap (package) heatmapgl (package) histogram (package) histogram2d (package) histogram2dcontour (package) layout (package) mesh3d (package) ohlc (package) parcoords (package) pie (package) pointcloud (package) sankey (package) scatter (package) scatter3d (package) scattercarpet (package) scattergeo (package) scattergl (package) scattermapbox (package) scatterpolar (package) scatterpolargl (package) scatterternary (package) splom (package) surface (package) table (package) violin (package) DATA absolute_import = _Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0... FILE d:anacondalibsite-packagesplotlygraph_objs__init__.pyIt can be seen that graph_objs There are a lot of graphic objects included in it, and this article will also select some of the commonly used ones to introduce.
import plotly import plotly.graph_objs as go import numpy as np '''构造1000个服从二维正态分布的模拟数据''' N = 1000 random_x = np.random.randn(N) random_y = np.random.randn(N) '''构造trace,配置相关参数''' trace = go.Scatter( x = random_x, y = random_y, mode = 'markers' ) '''将trace保存于列表之中''' data = [trace] '''启动绘图''' plotly.offline.init_notebook_mode() plotly.offline.iplot(data, filename='basic-scatter')From the above simple example, you can see the general method of trace creation, and multiple traces can be superimposed on one picture, as in the following example:
import numpy as np import plotly import plotly.graph_objs as go '''创建仿真数据''' N = 100 random_x = np.linspace(0, 1, N) random_y0 = np.random.randn(N)+5 random_y1 = np.random.randn(N) random_y2 = np.random.randn(N)-5 '''构造trace0''' trace0 = go.Scatter( x = random_x, y = random_y0, mode = 'markers', name = 'markers' ) '''构造trace1''' trace1 = go.Scatter( x = random_x, y = random_y1, mode = 'lines+markers', name = 'lines+markers' ) '''构造trace2''' trace2 = go.Scatter( x = random_x, y = random_y2, mode = 'lines', name = 'lines' ) '''将所有trace保存在列表中''' data = [trace0, trace1, trace2] '''启动绘图''' plotly.offline.init_notebook_mode() plotly.offline.iplot(data, filename='scatter-mode')For different graph_obj, the trace configuration format is also different.
2.4.1 Text
Text is a very important part of a picture, and plotly is very powerful. The drawing mechanism of APP has carefully divided the text in a picture, and can personalize the font of a certain component part in a very targeted way:
下面是一个简单的例子:
import plotly import plotly.graph_objs as go import numpy as np '''构造1000个服从二维正态分布的模拟数据''' N = 1000 random_x = np.random.randn(N) random_y = np.random.randn(N) '''构造trace,配置相关参数''' trace = go.Scatter( x = random_x, y = random_y, mode = 'markers' ) '''将trace保存于列表之中''' data = [trace] '''创建layout对象''' layout = go.Layout(title='测试', font={ 'size':22, 'family':'sans-serif', 'color':'9ed900'#将全局字体颜色设置颜色为葱绿 }) '''将graph部分和layout部分组合成figure对象''' fig = go.Figure(data=data, layout=layout) '''启动绘图直接绘制figure对象''' plotly.offline.init_notebook_mode() plotly.offline.iplot(fig,filename='basic-scatter')
标题文字:
下面是一个简单的例子:
import plotly import plotly.graph_objs as go import numpy as np '''构造1000个服从二维正态分布的模拟数据''' N = 1000 random_x = np.random.randn(N) random_y = np.random.randn(N) '''构造trace,配置相关参数''' trace = go.Scatter( x = random_x, y = random_y, mode = 'markers' ) '''将trace保存于列表之中''' data = [trace] '''创建layout对象''' layout = go.Layout(title='测试', titlefont={ 'size':20, 'color':'9ed900'#将标题字体颜色设置颜色为葱绿 }) '''将graph部分和layout部分组合成figure对象''' fig = go.Figure(data=data, layout=layout) '''启动绘图直接绘制figure对象''' plotly.offline.init_notebook_mode() plotly.offline.iplot(fig,filename='basic-scatter')
2.4.2 坐标轴
下面是几个简单的示例。
1. 对横纵坐标轴标题字体进行修改。
import plotly import plotly.graph_objs as go import numpy as np '''构造1000个服从二维正态分布的模拟数据''' N = 1000 random_x = np.random.randn(N) random_y = np.random.randn(N) '''构造trace,配置相关参数''' trace = go.Scatter( x = random_x, y = random_y, mode = 'markers' ) '''将trace保存于列表之中''' data = [trace] '''创建layout对象,对横纵坐标轴的标题进行一定的设置''' layout = go.Layout(xaxis={ 'title':'这是横坐标轴', 'titlefont':{ 'size':30 } },yaxis={ 'title':'这是纵坐标轴', 'titlefont':{ 'size':40 } }) '''将graph部分和layout部分组合成figure对象''' fig = go.Figure(data=data, layout=layout) '''启动绘图直接绘制figure对象''' plotly.offline.init_notebook_mode() plotly.offline.iplot(fig,filename='basic-scatter')
2. 对横纵方向的坐标轴线条及网格进行设置
import plotly import plotly.graph_objs as go import numpy as np '''构造1000个服从二维正态分布的模拟数据''' N = 1000 random_x = np.random.randn(N) random_y = np.random.randn(N) '''构造trace,配置相关参数''' trace = go.Scatter( x = random_x, y = random_y, mode = 'markers' ) '''将trace保存于列表之中''' data = [trace] '''创建layout对象,对横纵坐标轴的线条及网格颜色进行一定的设置''' layout = go.Layout(xaxis={ 'showline':False, 'showgrid':True, 'zeroline':False, 'showgrid':True, 'gridcolor':'7fecad' },yaxis={ 'showline':False, 'showgrid':True, 'gridcolor':'#3d3b4f', 'zeroline':False }) '''将graph部分和layout部分组合成figure对象''' fig = go.Figure(data=data, layout=layout) '''启动绘图直接绘制figure对象''' plotly.offline.init_notebook_mode() plotly.offline.iplot(fig,filename='basic-scatter')
2.4.3 图例
下面是一个简单的例子。
将图例的位置挪动到图像中心,即百分比上(0.5,0.5)的位置:
import numpy as np import plotly import plotly.graph_objs as go '''创建仿真数据''' N = 100 random_x = np.linspace(0, 1, N) random_y0 = np.random.randn(N)+5 random_y1 = np.random.randn(N) random_y2 = np.random.randn(N)-5 '''构造trace0''' trace0 = go.Scatter( x = random_x, y = random_y0, mode = 'markers', name = 'markers' ) '''构造trace1''' trace1 = go.Scatter( x = random_x, y = random_y1, mode = 'lines+markers', name = 'lines+markers' ) '''构造trace2''' trace2 = go.Scatter( x = random_x, y = random_y2, mode = 'lines', name = 'lines' ) '''将所有trace保存在列表中''' data = [trace0, trace1, trace2] '''构造layout对象,对图例位置进行一定的设置''' layout = go.Layout(legend={ 'x':0.5, 'y':0.5 }) '''构造figure对象''' fig = go.Figure(data=data,layout=layout) '''启动绘图''' plotly.offline.init_notebook_mode() plotly.offline.iplot(fig, filename='scatter-mode')
2.4.4 其它杂项
The above is the basic part of plotly drawing. If there are any typos, please point them out.
The above is the detailed content of Python visualization | Python visualization advanced essentials - plotly. For more information, please follow other related articles on the PHP Chinese website!