plotly principle drawing

table of Contents

  1, the relevant library described plotly
  2, import the relevant library
  3, plotly principle drawing
  4, the pattern stored in two ways
  5, double y-axis plotted in FIG.
  6, FIG draw more children: a plurality of graphic drawing on the canvas

1, plotly library of related presentations

1) Instructions
  • plotly is a javascript-based graphics library, drawing plotly variety, aesthetic effect;
  • Easy to store and share the drawing of plotly results, and can be integrated seamlessly with the Web;
  • ploty default drawing result is a HTML page file, can be viewed directly through the browser;

Here Insert Picture Description

2) the relationship between plotly and matplotlib, seaborn of

  Note that, ployly matplotlib graphics library and graphics library, seaborn graphics library does not matter. That said plotly is a separate graphics library has its own unique grammar drawing, drawing parameters and principles of drawing, so we need to learn it alone.

2, import the relevant library

  For the staff we do data analysis, the general use of graphics libraries are offline. Online mapping library needs, you can Baidu own research.

import os
import numpy as np
import pandas as pd

import plotly as py
import plotly.graph_objs as go
import plotly.expression as px
from plotly import tools

import warnings
warnings.filterwarnings("ignore")

  

3, plotly principle drawing

1) ployly conventional drawing two modules: graph_objs and expression

  graph_objs and expression is very common plotly which two graphics library, equivalent graph_objs matplotlib, the data organization in more strenuous, but any course simpler than drawing matplotlib look better. Here that is hard for the relative expression library. expression library of equivalent status seaborn, easier on the data organization, compared to seaborn drawing, they are also easier. Here you have the impression to the heart, know that these two graphics libraries are cattle, on the line.
  For graph_objs graphics library, we often named the "go" (import plotly.graph_objs as go ); plotting library for expression, we often named as "px" (import plotly.expression as px ).

2) the principle of drawing graph_objs ( "go") library
① simple case description
df = pd.read_excel("plot.xlsx")
# 步骤一
trace0 = go.Scatter(x=df["年份"],y=df["城镇居民"],name="城镇居民")
trace1 = go.Scatter(x=df["年份"],y=df["农村居民"],name="农村居民")
# 步骤二
data = [trace0,trace1]
# 步骤三
fig = go.Figure(data)
# 步骤四
fig.update_layout(
    title="城乡居民家庭人均收入",
    xaxis_title="年份",
    yaxis_title="人均收入(元)"
)
# 步骤五
fig.show()

The results are as follows:
Here Insert Picture Description

② Rationale
  • 1, draw graphics track, called the trace in ployly inside each track is a trace.
  • 2, wrapped into a list of trajectory, forming a "track list." A track in a list, a plurality of tracks is also in a list.
  • 3, create a canvas at the same time, and the above-mentioned "track list", passed to the Figure () in.
  • 4, using the Layout to add other drawing parameters, improving the graphics.
  • 5, presentation graphics.
Drawing principle 3) expression ( "px") library
① simple case description
iris = pd.read_excel("iris.xlsx",sheet_name="Sheet2")

fig = px.scatter(iris,x="花萼长度",y="花萼宽度",color="属种")
fig.show()

The results are as follows:
Here Insert Picture Description

② Rationale
  • 1, drawing directly px call a method automatically creates the canvas, and draw graphics.
  • 2, a presentation graphics.
      

4, save graphics in two ways

1) Direct download: Save the still pictures into png

Here Insert Picture Description

2) Use py.offline.plot (fig, filename = "XXX.html") code is saved as html pages dynamic picture
iris = pd.read_excel("iris.xlsx",sheet_name="Sheet2")

fig = px.scatter(iris,x="花萼长度",y="花萼宽度",color="属种")
py.offline.plot(fig,filename="iris1.html")

The results are as follows: This file is a html file, not upload here, go on their own to try to know.

3) summary explanation

  Using the "camera" that download button, you can directly download pictures stored locally, but this image is a static image, there is no interactivity. But using py.offline.plot () method, you can save the image into a html page format, others can open on your computer directly to this html page, and retain the picture's original style, interactive.
  

5, double y-axis plotted in FIG.

1) data set as follows

Here Insert Picture Description

2) Draw different parts of the "task completion quantity" and "task completion rate" situation
df = pd.read_excel("double_y.xlsx")

x = df["地区"]
y1 = df["完成量"]
y2 = df["完成率"]

trace0 = go.Bar(x=x,y=y1,
                marker=dict(color=["red","blue","green","darkgrey","darkblue","orange"]),
                opacity=0.5,
               name="不同地区的任务完成量")

trace1 = go.Scatter(x=x,y=y2,
                    mode="lines",
                    name="不同地区的任务完成率",
                    # 【步骤一】:使用这个参数yaxis="y2",就是绘制双y轴图
                    yaxis="y2")

data = [trace0,trace1]

layout = go.Layout(title="不同地区的任务完成量和任务完成率情况",
                   xaxis=dict(title="地区"),
                   yaxis=dict(title="不同地区的任务完成量"),
                   # 【步骤二】:给第二个y轴,添加标题,指定第二个y轴,在右侧。
                   yaxis2=dict(title="不同地区的任务完成率",overlaying="y",side="right"),
                   legend=dict(x=0.78,y=0.98,font=dict(size=12,color="black")))

fig = go.Figure(data=data,layout=layout)
fig.show()

The results are as follows:
Here Insert Picture Description

6, FIG draw more children: a plurality of pattern on a drawing canvas

1) associated libraries and methods of presentation
  • 1, a plurality of sub FIG drawing, the library needs to be imported tools. from plotly import tools
  • 2, tools.make_subplots (rows =, cols =) is used to specify the layout drawing, rows and cols canvas shows a layout of a plurality of rows columns.
  • 3, fig.append_trace () of each track pattern trace, drawn in different positions.
2) are plotted the situation, "the task is completed quantity" of different regions and "task completion rate."
# 步骤一:导入相关库
from plotly import tools
# 步骤二:指定绘图布局
fig = tools.make_subplots(rows=2,cols=1)
# 步骤三:绘制图形轨迹
trace0 = go.Bar(x=x,y=y1,
                marker=dict(color=["red","blue","green","darkgrey","darkblue","orange"]),
                opacity=0.5,
                name="不同地区的任务完成量")       
trace1 = go.Scatter(x=x,y=y2,
                    mode="lines",
                    name="不同地区的任务完成率",
                    line=dict(width=2,color="red"))
 # 步骤四:将第一个轨迹,添加到第1行的第1个位置
 #        将第二个轨迹,添加到第2行的第1个位置                  
fig.append_trace(trace0,1,1)
fig.append_trace(trace1,2,1)
# 步骤四:根据自己的需求,给图形添加标题。height、width参数用于指定图形的宽和高
fig.update_layout(title="不同地区的任务量与完成量",height=800,width=800)
# 步骤五:展示图形
fig.show()

The results are as follows:
Here Insert Picture Description

Published 89 original articles · won praise 115 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_41261833/article/details/104579877