SoFunction
Updated on 2024-12-13

Python matplotlib plotly plotting charts in detail

I. Organizing data

Using 300 movies as a data source

import pandas as pd 
cnboo=pd.read_excel("")
cnboo 

在这里插入图片描述

import seaborn as sns
import numpy as np 
import matplotlib as mpl
from matplotlib import pyplot as plt 
import pandas as pd 
from datetime import datetime,timedelta
%matplotlib inline
['-serif']=['SimHei'] # Used to display Chinese labels properly
['axes.unicode_minus']=False # Used to display the negative sign normally
from datetime import datetime 
! pip install plotly # Installation
import  as plt
import plotly
from  import download_plotlyjs,init_notebook_mode,plot,iplot
x=cnboo['BO'].tolist()
y=cnboo['PERSONS'].tolist()
dict01={"x":x,"y":y}
dict01

II. Line graphs

# Line graphs
iplot([dict01])

在这里插入图片描述

III. Scatterplot

import plotly.graph_objs as go
iplot([(x=x,y=y,mode='markers')])

在这里插入图片描述

请添加图片描述

# Randomly generated dot plots
import numpy as np
iplot([(x=(100),y=(100),mode='markers')])
go 

在这里插入图片描述

trace=(x=cnboo['PRICE'],y=y,mode='markers',)
data=[trace]
iplot(data)

在这里插入图片描述

trace=(x=cnboo['PRICE'],y=y,mode='markers',marker=dict(color='red',size=9,opacity=0.4))
data=[trace]
iplot(data)

在这里插入图片描述

IV. Pie charts

colors=['#dc2624','#2b4750','#45a0a2','#e87a59','#7dcaa9','#649E7D','#dc8018',
       '#C89F91','#6c6d6c','#4f6268','#c7cccf']
filmtype=cnboo['TYPE']
filmbo=cnboo['PRICE']
trace=(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=(labels=filmtype,values=filmbo,
            hoverinfo='label+percent',textinfo='value',textfont=dict(size=12),
             marker=dict(colors=colors))
data=[trace]
iplot(data)

在这里插入图片描述

V. Bar charts

# plotly bar
trace1=(x=cnboo['TYPE'],y=cnboo['PRICE'],name="Genre and Fare")
trace2=(x=cnboo['TYPE'],y=y,name="Type and number")
layout=(title="Chinese Movie Genres vs. Ticket Prices, Numbers of People.",xaxis=dict(title='Movie genre'))
data=[trace1,trace2]
fig=(data,layout=layout)
iplot(fig)

在这里插入图片描述

VI. Point map (setting up multiple GO objects)

trace1=(x=cnboo['TYPE'],y=cnboo['PRICE'],name="Genre and Fare",mode="markers",
                  marker=dict(color="red",size=8))
trace2=(x=cnboo['TYPE'],y=cnboo['PERSONS'],name="Type and number",mode="markers",
                  marker=dict(color="blue",size=5))
data=[trace1,trace2]
iplot(data)

在这里插入图片描述

trace1=(x=cnboo['TYPE'],y=cnboo['PRICE'],name="Genre and Fare",mode="markers",
                  marker=dict(color="red",size=8))
trace2=(x=cnboo['TYPE'],y=cnboo['PERSONS'],name="Type and number",mode="markers",
                  marker=dict(color="blue",size=5))
layout=(title="Chinese Movie Genres vs. Ticket Prices, Numbers of People.",plot_bgcolor="#FFFFFF")
data=[trace1,trace2]
fig=(data,layout=layout)
iplot(fig)

在这里插入图片描述

VII. 2D density map

import plotly.figure_factory as ff
fig=ff.create_2d_density(x,y,colorscale=colors,hist_color='#dc2624',point_size=5)
iplot(fig,filename='Ratings and person counts')

在这里插入图片描述

colorscale=['rgb(20, 38, 220)',
 'rgb(255, 255, 255)'] # The last color is all about calling the background
fig=ff.create_2d_density(x,y,colorscale=colorscale,hist_color='#dc2624',point_size=5)
iplot(fig,filename='Ratings and person counts')

在这里插入图片描述

VIII. Simple 3D diagrams

layout=(title="China's Movie Box Office in Relation to Attendance, Ticket Prices.",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 is transparency
)
data=[trace01]
fig=(data=data,layout=layout)
iplot(fig,filename='3d')

请添加图片描述

Above is Python matplotlib plotly plotting charts in detail, more information about Python matplotlib plotly please pay attention to my other related articles!