This article mainly introduces the method that Python uses plotly to draw the data chart, the example analyzes the technique of plotly drawing, has certain reference value, the interested small partners may refer to
Introduction: Using the Python-plotly module to draw the data of the pressure measurement, and generate static HTML page results display.
A lot of small partners in the development process has the experience of the module to test, after the end of the test people often like to use Excel to process pressure measurement data and draw a visual view of the data, but it is not easy to use the Web page for data presentation. This article will introduce the use of the python-plotly module for the plotting of the measured data, and generate static HTML pages to facilitate the presentation of results.
plotly Introduction
Plotly is a cartographic tool developed using JavaScript that provides APIs for interacting with the mainstream data analysis language (e.g. Python, R, MATLAB). You can go to the official website https://plot.ly/to learn more detailed information. Plotly is able to draw beautiful charts with user interaction capabilities.
python-plotly Installation
This document is mainly about the use of Plotly Python API to do several simple chart drawing, more plotly usage please refer to https://plot.ly/python/
Python-plotly can be installed using PIP, and it is best to use the Python2.7 version and above, if you use the Python2.6 version, install the Python2.7 and the corresponding PIP on your own.
Plotly Drawing Instances
Line-plots
Drawing effect:
The resulting HTML page provides a rich interactive tool in the upper-right corner.
Code:
def line_plots (name): " draw normal line graph" ' #数据, X is horizontal, y,z is two indicators of ordinate, three array length is same as DataSet = {' X ': [ 0,1,2,3,4,5,6,7,8,9], ' y ': [5,4,1,3,11,2,6,7,19,20], ' z ': [12,9,0,0,3,25,8,17,22,5]} data_g = [] # Insert Y, z tr_x = scatter ( x = dataset[' x '], y = dataset[' y '), and name = ' y ' ) data_g.append ( tr_x) tr_z = Scatter ( x = dataset[' x '], y = dataset[' z '), name = ' Z ' ) data_g.append (tr_ Z) #设置layout, specify chart title,x axis and y-axis name layout = layout (title= "line plots", xaxis={' title ': ' X '}, yaxis={' title ': ' Value '}) #将layout设置到图表 fig = figure (Data=data_g, layout=layout) #绘图, the output path is specified for the name parameter pltoff.plot (Fig, Filename=name)
Scatter-plots
Drawing effect:
Code:
def scatter_plots (name): " plot scatter Plot" ' DataSet = {' X ': [0,1,2,3,4,5,6,7,8,9], ' y ': [ 5,4,1,3,11,2,6,7,19,20], ' text ': [' 5_txt ', ' 4_txt ', ' 1_txt ', ' 3_txt ', ' 11_txt ', ' 2_txt ', ' 6_txt ', ' 7_txt ', ' 19_txt ' , ' 20_txt '} data_g = [] tr_x = Scatter ( x = dataset[' x '], y = dataset[' y '), text = dataset[' text '], textposition= ' top center ', mode= ' Markers+text ', name = ' y ' ) data_g.append (tr_x) Layout = layout (title= "Scatter plots", xaxis={' title ': ' X '}, yaxis={' title ': ' Value '}) fig = figure (Data=data_g, Layout=layout) Pltoff.plot (Fig, filename=name)
Bar-charts
Drawing effect:
Code:
def bar_charts (name): " draw histogram " ' DataSet = {' x ': [' Windows ', ' Linux ', ' Unix ', ' MacOS '], ' y1 ': [ "y2": [+, +, +]} data_g = [] tr_y1 = Bar ( x = dataset[' x '], y = dataset[' y1 '],< C10/>name = ' v1 ' ) data_g.append (tr_y1) tr_y2 = Bar ( x = dataset[' x '], y = dataset[' y2 '), name = ' v2 ' ) Data_g.append (tr_y2) layout = layout (title= "bar charts", xaxis={' title ': ' X '}, yaxis={' title ': ' Value '}) Fig = figure (Data=data_g, Layout=layout) pltoff.plot (Fig, filename=name)
Pie-charts
Drawing effect:
Code:
def pie_charts (name): " Draw pie chart" ' DataSet = {' Labels ': [' Windows ', ' Linux ', ' Unix ', ' MacOS ', ' Android ', ' IOS '], ' values ': [280, +, ten, +, +]} data_g = [] tr_p = Pie ( labels = dataset[' labels ' ], values = dataset[' values ' ) data_g.append (tr_p) layout = layout (title= "pie charts") fig = Figure (Data=data_g, Layout=layout) pltoff.plot (Fig, filename=name)
Filled-area-plots
This example is to draw a stacked line chart with fill effect, suitable for analyzing data with a stacked percent attribute
Drawing effect:
Code:
def filled_area_plots (name): ' Draw a stacked filled line graph ' ' DataSet = {' X ': [0,1,2,3,4,5,6,7,8,9], ' y1 ': [5,4,1,3,11,2,6,7,19, ], ' y2 ': [12,9,0,0,3,25,8,17,22,5], ' y3 ': [13,22,46,1,15,4,18,11,17,20]} #计算y1, Y2,y3 's stack ratio dataset[' Y1_stac K '] = dataset[' y1 '] dataset[' y2_stack '] = [y1+y2 for y1, y2 in zip (dataset[' y1 '], dataset[' y2 '])] [dataset[' y3_stack '] = [Y1+y2+y3 for Y1, y2, y3 in zip (dataset[' y1 '], dataset[' y2 '], dataset[' Y3 '])] [dataset[' y1_text '] = ['%s (%s%%) '% (Y1, y1*10 0/y3_s) for Y1, y3_s in zip (dataset[' y1 '], dataset[' Y3_stack '])] [dataset[' y2_text '] = ['%s (%s%%) '% (y2, y2*100/y3_s) for Y 2, y3_s in zip (dataset[' y2 '), dataset[' Y3_stack '])] [dataset[' y3_text '] = ['%s (%s%%) '% (Y3, y3*100/y3_s) for Y3, y3_s in Zi P (dataset[' Y3 '), dataset[' Y3_stack '])] Data_g = [] tr_1 = scatter (x = dataset[' x '], y = dataset[' y1_stack '], t ext = dataset[' Y1_text '], hoverinfo = ' X+text ', mode = ' lines ', name = ' Y1 ', fill = ' Tozeroy ' #填充方式: to x-axis) Data_g.append (tr_1) tr_2 = scatter (x = dataset[' x '], y = dataset[' y2_stack '), Text = dataset[' Y2_text '], hoverinfo = ' X+text ', MO de = ' lines ', name = ' y2 ', fill = ' tonexty ' #填充方式: to another line below) Data_g.append (tr_2) tr_3 = scatter (x = dataset[ ' X '], y = dataset[' y3_stack ', text = dataset[' Y3_text '], hoverinfo = ' X+text ', mode = ' lines ', name = ' Y3 ' , fill = ' tonexty ') data_g.append (tr_3) layout = layout (title= "field area plots", xaxis={' title ': ' X '}, yaxis={' Titl E ': ' Value '}) FIG = figure (Data=data_g, layout=layout) pltoff.plot (Fig, filename=name)
Summary
This paper introduces the method of drawing data graph by using python-plotly, example of line plots, scatter plot (scatter plots), histogram (bar charts), pie chart (pie charts), and fill stacking plot (filled Area plots these five typical charts cover most types of test data, and the small partners can deform and draw more beautiful icons.