plotly
plotly graphics library is based on Javascript, aesthetic effect; and the web can be seamlessly connected; the library is the default drawing result of a HTML page file, can be viewed through the browser. (Not supported DataFrame format) created using this library is a diagram of the way, the main thing is to use 1, go.Scatter () to establish rail Figure 2, then go.Layout () Set the layer 3, and then go.Figure ( ) and FIG rail layers were combined, 4, last used py.offline.iplot (fig) is displayed, before returning to Fig step value is
Offline Drawing: plotly.offline.iplot (), plotly.offline.plot ()
plotly.offline.iplot (): method to draw graphics, direct view Jupyter Notebook in
plotly.offline.plot (): creates a new HTML file locally, and can choose whether to open in the browser
import os
import plotly as py
import plotly.graph_objs as go
from plotly.graph_objs import Scatter
os.chdir(r'C:\Users\MAR\Desktop\test')
#添加图轨数据
trace0=Scatter(x=[1,2,3,4],y=[10,15,13,17])
trace1=Scatter(x=[1,2,3,4],y=[6,7,5,9])
data=[trace0,trace1]
py.offline.plot(data,filename='fth.html')
Html image generated as follows
Scatter and line drawing in FIG.
Scatter and line graph is different in different go.Scatter () in the mode, when the mode is a line graph that when the lines, is when the markers, showing scattergram
go.Scatter(x,y,mode,name,marker,line)
The encapsulating data, returns x, packaging y, linear and image tag
parameters:
x, Y: data (horizontal and vertical axis)
MODE: Line Type
name: Legend name
marker: control-related parameter of the
line: a control line color, width
pyplot=py.offline.iplot
trace0=Scatter(x=data.地区,y=data.收入,mode='lines',name='收入',\
line={'width': 2, 'color': 'green'})
trace1=Scatter(x=data.地区,y=data.总收入,mode='lines',name='总收入',\
line={'width': 2, 'color': 'black'})
data_source=[trace0,trace1]
pyplot(data_source)
go.Layout()
FIG modification, labeling and shaft names,
pyplot=py.offline.iplot
trace0=Scatter(x=data.地区,y=data.收入,mode='lines',name='收入',\
line={'width': 2, 'color': 'green'})
trace1=Scatter(x=data.地区,y=data.总收入,mode='lines',name='总收入',\
line={'width': 2, 'color': 'black'})
data_source=[trace0,trace1] #组合成数据轨
#设置图层
layout=go.Layout(title='总收入',xaxis=dict(title='这是一个x轴标题'),legend=dict(x=1,y=0.5),\
yaxis=dict(title='这是一个y的标题'),\
font=dict(size=15,color='red'))
#将数轨和图层合成figure
fig=go.Figure(data=data_source,layout=layout)
pyplot(fig) #打印输出figure
A histogram
No stack
Single column bar chart
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
pyplot=py.offline.iplot
region=data.地区.value_counts()
print (region) #对地区列的数据出现次数进行统计
#第一步得到图的主要参数,xy必须时列表,opacity控制透明度
trace=[go.Bar(x=region.index.tolist(),y=region.values.tolist(),
marker=dict(color=['red','blue','green','gray','darkblue']),opacity=0.9)]
#第二步设置图像的图例标签等
layout=go.Layout(title='不同地区的统计',xaxis=dict(title='地区'))
#第三步将第一第二步合成图片
figure=go.Figure(data=trace,layout=layout)
#第四步显示
pyplot(figure)
Multiple columns of data histogram
ata=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
pyplot=py.offline.iplot
#先使用‘交通方式’作为分类标准,
d1=data[data.交通方式=='交通1']
d2=data[data.交通方式=='交通2']
d3=data[data.交通方式=='交通3']
#再将每种交通分类的地区作为x轴,收入作为y,对应图轨命名
trace1=go.Bar(x=d1.地区,y=d1.收入,name='第一种交通方式')
trace2=go.Bar(x=d2.地区,y=d2.收入,name='第二种交通方式')
trace3=go.Bar(x=d3.地区,y=d3.收入,name='第三种交通方式')
trace=[trace1,trace2,trace3]
layout=go.Layout(title='不同地区交通的收入',xaxis=dict(title='地区'))
figure=go.Figure(data=trace,layout=layout)
pyplot(figure)
There are stacked bar chart
Compared to the previous program, but more than in the command, a (barmode = 'stack')
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
pyplot=py.offline.iplot
d1=data[data.交通方式=='交通1']
d2=data[data.交通方式=='交通2']
d3=data[data.交通方式=='交通3']
trace1=go.Bar(x=d1.地区,y=d1.收入,name='第一种交通方式')
trace2=go.Bar(x=d2.地区,y=d2.收入,name='第二种交通方式')
trace3=go.Bar(x=d3.地区,y=d3.收入,name='第三种交通方式')
trace=[trace1,trace2,trace3]
layout=go.Layout(title='不同地区交通的收入',xaxis=dict(title='地区'),barmode='stack')
figure=go.Figure(data=trace,layout=layout)
pyplot(figure)
Histogram
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
pyplot=py.offline.iplot
figure=[go.Histogram(x=data.地区,histnorm='probability',
marker=dict(color=['blue','yellow','green','red']))]
pyplot(figure)
Pie
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
pyplot=py.offline.iplot
#先对数据分组,并按照求和进行聚合
data1=data.groupby(by='地区')
data2=data1.agg(np.sum)
trace=[go.Pie(labels=data2.index.tolist(),values=data2.收入.tolist(),
hole=0.1,textfont=dict(size=12,color='white'))]
layout=go.Layout(title='不同地区的收入')
fig=go.Figure(data=trace,layout=layout)
pyplot(fig)