这篇文章主要讲解了“怎么用Python matplotlib plotly绘制图表”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Python matplotlib plotly绘制图表”吧!
一、整理数据
以300部电影作为数据源
import pandas as pd cnboo=pd.read_excel("cnboNPPD1.xls")
cnboo
import seaborn as snsimport numpy as np import matplotlib as mplfrom matplotlib import pyplot as plt import pandas as pd from datetime import datetime,timedelta%matplotlib inlineplt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号from datetime import datetime
! pip install plotly # 安装import matplotlib.pyplot as pltimport plotlyfrom plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
x=cnboo['BO'].tolist()y=cnboo['PERSONS'].tolist()dict01={"x":x,"y":y}dict01
二、折线图
# 折线图iplot([dict01])
三、散点图
import plotly.graph_objs as goiplot([go.Scatter(x=x,y=y,mode='markers')])
# 随机生成的点图import numpy as npiplot([go.Scatter(x=np.random.randn(100),y=np.random.randn(100),mode='markers')])
go
trace=go.Scatter(x=cnboo['PRICE'],y=y,mode='markers',)data=[trace]iplot(data)
trace=go.Scatter(x=cnboo['PRICE'],y=y,mode='markers',marker=dict(color='red',size=9,opacity=0.4))data=[trace]iplot(data)
四、饼图
colors=['#dc2624','#2b4750','#45a0a2','#e87a59','#7dcaa9','#649E7D','#dc8018', '#C89F91','#6c6d6c','#4f6268','#c7cccf']
filmtype=cnboo['TYPE']filmbo=cnboo['PRICE']trace=go.Pie(labels=filmtype,values=filmbo, hoverinfo='label+percent',textinfo='value',textfont=dict(size=10), marker=dict(colors=colors,line=dict(color='#000000',width=3)))data=[trace]
iplot(data)
filmtype=cnboo['TYPE']filmbo=cnboo['PRICE']trace=go.Pie(labels=filmtype,values=filmbo, hoverinfo='label+percent',textinfo='value',textfont=dict(size=12), marker=dict(colors=colors))data=[trace]iplot(data)
五、柱形图
# plotly bartrace1=go.Bar(x=cnboo['TYPE'],y=cnboo['PRICE'],name="类型与票价")trace2=go.Bar(x=cnboo['TYPE'],y=y,name="类型与人数")layout=go.Layout(title="中国电影类型与票价,人数的关系",xaxis=dict(title='电影类型'))data=[trace1,trace2]fig=go.Figure(data,layout=layout)iplot(fig)
六、点图(设置多个go对象)
trace1=go.Scatter(x=cnboo['TYPE'],y=cnboo['PRICE'],name="类型与票价",mode="markers", marker=dict(color="red",size=8))trace2=go.Scatter(x=cnboo['TYPE'],y=cnboo['PERSONS'],name="类型与人数",mode="markers", marker=dict(color="blue",size=5))data=[trace1,trace2]iplot(data)
trace1=go.Scatter(x=cnboo['TYPE'],y=cnboo['PRICE'],name="类型与票价",mode="markers", marker=dict(color="red",size=8))trace2=go.Scatter(x=cnboo['TYPE'],y=cnboo['PERSONS'],name="类型与人数",mode="markers", marker=dict(color="blue",size=5))layout=go.Layout(title="中国电影类型与票价,人数的关系",plot_bgcolor="#FFFFFF")data=[trace1,trace2]fig=go.Figure(data,layout=layout)iplot(fig)
七、2D密度图
import plotly.figure_factory as ff
fig=ff.create_2d_density(x,y,colorscale=colors,hist_color='#dc2624',point_size=5)iplot(fig,filename='评分与人次')
colorscale=['rgb(20, 38, 220)', 'rgb(255, 255, 255)'] # 最后一个颜色都是调用背景
fig=ff.create_2d_density(x,y,colorscale=colorscale,hist_color='#dc2624',point_size=5)iplot(fig,filename='评分与人次')
八、简单3D图
layout=go.Layout(title="中国电影票房与人次,票价的关系",barmode="group")
trace01=go.Scatter3d( x=cnboo['BO'], y=cnboo['PRICE'], z=cnboo['PERSONS'], mode='markers', marker=dict(size=12,color=colors,colorscale='Viridis', opacity=0.5,showscale=True) #opacity是透明度)
data=[trace01]fig=go.Figure(data=data,layout=layout)iplot(fig,filename='3d')
感谢各位的阅读,以上就是“怎么用Python matplotlib plotly绘制图表”的内容了,经过本文的学习后,相信大家对怎么用Python matplotlib plotly绘制图表这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!