Plotly 简明教程

OHLC Chart, Waterfall Chart and Funnel Chart

本章重点介绍了其他三种类型的图表,包括 OHLC、瀑布图和漏斗图,这些图表可以在 Plotly 的帮助下制作。

This chapter focusses on other three types of charts including OHLC, Waterfall and Funnel Chart which can be made with the help of Plotly.

OHLC Chart

一种 open-high-low-close 图表(也称作 OHLC)是一种 bar chart ,通常用于说明诸如股票等金融工具价格的变动。OHLC 图表很有用,因为它们显示了一段期间内的四个主要数据点。这类图表之所以有用,是因为它可以显示上升或下降的动能。高点和低点数据对于评估波动很有用。

An open-high-low-close chart (also OHLC) is a type of bar chart typically used to illustrate movements in the price of a financial instrument such as shares. OHLC charts are useful since they show the four major data points over a period. The chart type is useful because it can show increasing or decreasing momentum. The high and low data points are useful in assessing volatility.

图表上的每条垂直线均显示一个时间单位(如一天或一小时)内的价格范围(最高价和最低价)。标号从每条线的两侧突出显示,左侧表示开盘价(例如,对于日线图,这将是该天的开盘价),右侧表示该时间段的收盘价。

Each vertical line on the chart shows the price range (the highest and lowest prices) over one unit of time, such as day or hour. Tick marks project from each side of the line indicating the opening price (e.g., for a daily bar chart this would be the starting price for that day) on the left, and the closing price for that time period on the right.

下面显示了用于演示 OHLC 图表的示例数据。此数据具有与相应日期字符串相对应的代表高、低、开盘和收盘值的对象列表。使用 datetime 模块中的 strtp() 函数将字符串的日期表示转换为日期对象。

Sample data for demonstration of OHLC chart is shown below. It has list objects corresponding to high, low, open and close values as on corresponding date strings. The date representation of string is converted to date object by using strtp() function from datetime module.

open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
date_data = ['10-10-2013', '11-10-2013', '12-10-2013','01-10-2014','02-10-2014']
import datetime
dates = [
   datetime.datetime.strptime(date_str, '%m-%d-%Y').date()
   for date_str in date_data
]

必须使用上述日期对象作为 x 参数,以及 go.Ohlc() 函数所需的开盘、高、低和收盘参数,该函数返回 OHLC 轨迹。

We have to use above dates object as x parameter and others for open, high, low and close parameters required for go.Ohlc() function that returns OHLC trace.

trace = go.Ohlc(
   x = dates,
   open = open_data,
   high = high_data,
   low = low_data,
   close = close_data
)
data = [trace]
fig = go.Figure(data = data)
iplot(fig)

下面给出代码的输出 −

The output of the code is given below −

ohlc chart

Candlestick Chart

candlestick chart 与 OHLC 图表类似。它类似于 line-chartbar-chart 的组合。这些方框表示开盘价和收盘价之间的价差,而线段表示低价和高价之间的价差。收盘价高于(低于)开盘价的示例点被称为上升(下降)。

The candlestick chart is similar to OHLC chart. It is like a combination of line-chart and a bar-chart. The boxes represent the spread between the open and close values and the lines represent the spread between the low and high values. Sample points where the close value is higher (lower) then the open value are called increasing (decreasing).

go.Candlestick() function 返回烛台轨迹。我们使用相同数据(与 OHLC 图表相同)来渲染烛台图,如下所示 -

Candlestrick trace is returned by go.Candlestick() function. We use same data (as for OHLC chart) to render candlestick chart as given below −

trace = go.Candlestick(
   x = dates,
   open = open_data,
   high = high_data,
   low = low_data,
   close = close_data
)

以下是给定代码的输出 -

Output of the above given code is mentioned below −

candlestick chart

Waterfall chart

瀑布图(也称为 flying bricks chart or Mario chart )有助于理解按顺序引入的正或负值(其可以基于时间或基于类别)的累积效应。

A waterfall chart (also known as flying bricks chart or Mario chart) helps in understanding the cumulative effect of sequentially introduced positive or negative values which can either be time based or category based.

起始值和结束值显示为柱状图,各个负调整值和正调整值显示为浮动步长。一些瀑布图连接柱状图之间的线段,以使图表看起来像桥梁。

Initial and final values are shown as columns with the individual negative and positive adjustments depicted as floating steps. Some waterfall charts connect the lines between the columns to make the chart look like a bridge.

go.Waterfall() 函数返回一个瀑布追踪。该对象可以通过各种命名参数或属性进行定制。这里,x 和 y 属性设置了图形的 x 和 y 坐标的数据。两者都可以是 Python 列表、numpy 数组、Pandas 时间序列、字符串或日期时间对象。

go.Waterfall() function returns a Waterfall trace. This object can be customized by various named arguments or attributes. Here, x and y attributes set up data for x and y coordinates of the graph. Both can be a Python list, numpy array or Pandas series or strings or date time objects.

另一个属性是 measure ,它是一个包含值类型的数组。默认情况下,这些值被认为是 relative 。将其设置为“total”以计算总和。如果它等于 absolute ,它会重置计算的总和或根据需要声明一个初始值。“base”属性设置条形基准绘制的位置(以位置轴单位为单位)。

Another attribute is measure which is an array containing types of values. By default, the values are considered as relative. Set it to 'total' to compute the sums. If it is equal to absolute it resets the computed total or to declare an initial value where needed. The 'base' attribute sets where the bar base is drawn (in position axis units).

以下代码呈现一张瀑布图 −

Following code renders a waterfall chart −

s1=[
   "Sales",
   "Consulting",
   "Net revenue",
   "Purchases",
   "Other expenses",
   "Profit before tax"
]
s2 = [60, 80, 0, -40, -20, 0]
trace = go.Waterfall(
   x = s1,
   y = s2,
   base = 200,
   measure = [
      "relative",
      "relative",
      "total",
      "relative",
      "relative",
      "total"
   ]
)
data = [trace]
fig = go.Figure(data = data)
iplot(fig)

下面提到的输出是上面给出的代码的结果。

Below mentioned output is a result of the code given above.

waterfall chart

Funnel Chart

漏斗图以业务流程的不同阶段表示数据。它是商业智能中识别流程潜在问题区域的重要机制。漏斗图用于可视化数据在从一个阶段传递到另一个阶段时如何逐步减少。这些阶段中的每个阶段的数据都表示为 100%(全部)的不同部分。

Funnel charts represent data in different stages of a business process. It is an important mechanism in Business Intelligence to identify potential problem areas of a process. Funnel chart is used to visualize how data reduces progressively as it passes from one phase to another. Data in each of these phases is represented as different portions of 100% (the whole).

与饼图一样,漏斗图也不使用任何轴。它也可以被认为类似于 stacked percent bar chart 。任何漏斗都由称为头部(或底部)的上部和称为颈部的下部组成。漏斗图最常见的用途是可视化销售转化数据。

Like the Pie chart, the Funnel chart does not use any axes either. It can also be treated as similar to a stacked percent bar chart. Any funnel consists of the higher part called head (or base) and the lower part referred to as neck. The most common use of the Funnel chart is in visualizing sales conversion data.

Plotly 的 go.Funnel() 函数生成漏斗追踪。向此函数提供的必要属性为 x 和 y 。每个属性都分配了一个项目 Python 列表或一个数组。

Plotly’s go.Funnel() function produces Funnel trace. Essential attributes to be provided to this function are x and y. Each of them is assigned a Python list of items or an array.

from plotly import graph_objects as go
fig = go.Figure(
   go.Funnel(
      y = [
         "Website visit",
         "Downloads",
         "Potential customers",
         "Requested price",
         "invoice sent"
      ],
      x = [39, 27.4, 20.6, 11, 2]
   )
)
fig.show()

输出如下所示:

The output is as given below −

funnel chart