一、导入包
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import plotly.offline as py #保存图表,相当于plotly.plotly as py,同时增加了离线功能
py.init_notebook_mode(connected=True) #离线绘图时,需要额外进行初始化
import plotly.graph_objs as go #创建各类图表
import plotly.figure_factory as ff #创建table
二、参数说明
- 本文用到的部分参数说明如下,仅供参考,具体见官方文档;
- hoverinfo : 设置悬停的信息,默认为all,可以是任意组合:"label", "text", "value", "percent";
- textinfo : 设置图形标记,可以是任意组合:"label", "text", "value", "percent";
- textfont : 设置标记字体颜色等格式;
- marker : 设置饼图每个扇区的颜色、边框的线条、颜色、宽度等;
三、基本饼图
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
trace = go.Pie(labels=labels, values=values)
py.iplot([trace], filename='basic_pie_chart')
四、风格饼图
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
colors = ['#FEBFB3', '#E1396C', '#96D38C', '#D0F9B1']
trace = go.Pie(labels = labels,
values = values,
hoverinfo = 'label+percent',
textinfo='value',
textfont = dict(size=15),
marker=dict(colors=colors, line=dict(color='#000000', width=2))
)
py.iplot([trace], filename='styled_pie_chart')