Plotly 简明教程
Plotly - Bar Chart and Pie Chart
在本章中,我们将学习如何借助 Plotly 制作条形图和饼图。让我们从了解条形图开始。
In this chapter, we will learn how to make bar and pie charts with the help of Plotly. Let us begin by understanding about bar chart.
Bar Chart
条形图使用与其所表示的值成比例的矩形条以显示分类数据,这些条形图具有高度或长度。条形图可以垂直或水平显示。它有助于显示离散类别之间的比较。图表的一条轴显示正在比较的特定类别,另一条轴表示测量值。
A bar chart presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. Bars can be displayed vertically or horizontally. It helps to show comparisons among discrete categories. One axis of the chart shows the specific categories being compared, and the other axis represents a measured value.
以下示例针对不同课程注册的学生人数绘制了一个简单的 bar chart 。 go.Bar() 函数返回一个条形轨迹,其中 x 坐标设置为科目列表,y 坐标设置为学生人数。
Following example plots a simple bar chart about number of students enrolled for different courses. The go.Bar() function returns a bar trace with x coordinate set as list of subjects and y coordinate as number of students.
import plotly.graph_objs as go
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
data = [go.Bar(
x = langs,
y = students
)]
fig = go.Figure(data=data)
iplot(fig)
输出将如下所示 −
The output will be as shown below −

要显示分组条形图,必须将 Layout 对象的 barmode 属性设置为 group 。在以下代码中,表示每个年级学生的多个轨迹相对于科目进行绘制,并显示为分组条形图。
To display a grouped bar chart, the barmode property of Layout object must be set to group. In the following code, multiple traces representing students in each year are plotted against subjects and shown as grouped bar chart.
branches = ['CSE', 'Mech', 'Electronics']
fy = [23,17,35]
sy = [20, 23, 30]
ty = [30,20,15]
trace1 = go.Bar(
x = branches,
y = fy,
name = 'FY'
)
trace2 = go.Bar(
x = branches,
y = sy,
name = 'SY'
)
trace3 = go.Bar(
x = branches,
y = ty,
name = 'TY'
)
data = [trace1, trace2, trace3]
layout = go.Layout(barmode = 'group')
fig = go.Figure(data = data, layout = layout)
iplot(fig)
其输出如下 −
The output of the same is as follows −

barmode 属性确定在图形上如何显示具有相同位置坐标的条形图。定义的值为“堆叠”(条形图彼此堆叠),“相对”(条形图彼此堆叠,负值在轴下方,正值在轴上方),“ group ”(条形图彼此相邻绘制)。
The barmode property determines how bars at the same location coordinate are displayed on the graph. Defined values are "stack" (bars stacked on top of one another), "relative", (bars are stacked on top of one another, with negative values below the axis, positive values above), "group" (bars plotted next to one another).
通过将 barmode 属性更改为“ stack ”,绘制的图形如下所示 −
By changing barmode property to ‘stack’ the plotted graph appears as below −

Pie chart
饼图仅显示一个数据序列。 Pie Charts 显示一个数据序列中项目(称为 wedge )的大小,它与项目的总和成比例。数据点显示为整个饼图的百分比。
A Pie Chart displays only one series of data. Pie Charts show the size of items (called wedge) in one data series, proportional to the sum of the items. Data points are shown as a percentage of the whole pie.
graph_objs 模块中的 pie() 函数 - go.Pie() 返回一个饼图轨迹。两个必需参数是 labels 和 values 。让我们绘制一个简单的语言课程与学生人数的饼图,如以下示例所示。
The pie() function in graph_objs module – go.Pie(), returns a Pie trace. Two required arguments are labels and values. Let us plot a simple pie chart of language courses vs number of students as in the example given herewith.
import plotly
plotly.tools.set_credentials_file(
username = 'lathkar', api_key = 'U7vgRe1hqmRp4ZNf4PTN'
)
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode(connected = True)
import plotly.graph_objs as go
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
trace = go.Pie(labels = langs, values = students)
data = [trace]
fig = go.Figure(data = data)
iplot(fig)
在 Jupyter Notebook 中显示如下输出 -
Following output is displayed in Jupyter notebook −

Donut chart 是一个圆心带一个圆孔的饼图,这使它看起来像一个甜甜圈。在以下示例中,两个甜甜圈图以 1X2 网格布局显示。虽然“ label ”布局对两个饼形迹线相同,但每一部分情节的行和列目的地由域属性决定。
Donut chart is a pie chart with a round hole in the center which makes it look like a donut. In the following example, two donut charts are displayed in 1X2 grid layout. While ‘label’ layout is same for both pie traces, row and column destination of each subplot is decided by domain property.
为此,我们使用 2019 年议会选举中按党派划分的席位和得票率数据。在 Jupyter Notebook 单元格中输入以下代码 -
For this purpose, we use the data of party-wise seats and vote share in 2019 parliamentary elections. Enter the following code in Jupyter notebook cell −
parties = ['BJP', 'CONGRESS', 'DMK', 'TMC', 'YSRC', 'SS', 'JDU','BJD', 'BSP','OTH']
seats = [303,52,23,22,22,18,16,12,10, 65]
percent = [37.36, 19.49, 2.26, 4.07, 2.53, 2.10, 1.46, 1.66, 3.63, 25.44]
import plotly.graph_objs as go
data1 = {
"values": seats,
"labels": parties,
"domain": {"column": 0},
"name": "seats",
"hoverinfo":"label+percent+name",
"hole": .4,
"type": "pie"
}
data2 = {
"values": percent,
"labels": parties,
"domain": {"column": 1},
"name": "vote share",
"hoverinfo":"label+percent+name",
"hole": .4,
"type": "pie"
}
data = [data1,data2]
layout = go.Layout(
{
"title":"Parliamentary Election 2019",
"grid": {"rows": 1, "columns": 2},
"annotations": [
{
"font": {
"size": 20
},
"showarrow": False,
"text": "seats",
"x": 0.20,
"y": 0.5
},
{
"font": {
"size": 20
},
"showarrow": False,
"text": "votes",
"x": 0.8,
"y": 0.5
}
]
}
)
fig = go.Figure(data = data, layout = layout)
iplot(fig)
如下给出相同代码的输出结果 -
The output of the same is given below −
