Plotly 简明教程

Plotly - Distplots Density Plot and Error Bar Plot

在本章中,我们将详细了解 distplot、密度图和误差棒图。让我们从了解 distplot 开始。

In this chapter, we will understand about distplots, density plot and error bar plot in detail. Let us begin by learning about distplots.

Distplots

distplot 图表工厂显示数值数据的统计表示组合形式,例如直方图、核密度估计或正态曲线,以及 rug 图。

The distplot figure factory displays a combination of statistical representations of numerical data, such as histogram, kernel density estimation or normal curve, and rug plot.

distplot 可以由以下 3 个组件的全部或任意组合组成 −

The distplot can be composed of all or any combination of the following 3 components −

  1. histogram

  2. curve: (a) kernel density estimation or (b) normal curve, and

  3. rug plot

figure_factory 模块具有 create_distplot() 函数,需要称为 hist_data 的强制参数。

The figure_factory module has create_distplot() function which needs a mandatory parameter called hist_data.

以下代码创建一个基本 distplot,它包含一个直方图、一个 kde 图和一个 rug 图。

Following code creates a basic distplot consisting of a histogram, a kde plot and a rug plot.

x = np.random.randn(1000)
hist_data = [x]
group_labels = ['distplot']
fig = ff.create_distplot(hist_data, group_labels)
iplot(fig)

以上提到的这段代码的输出内容如下 −

The output of the code mentioned above is as follows −

distplots

Density Plot

密度图是根据数据估计出的直方图的平滑连续版本。最常见的估计形式称为 kernel density estimation (KDE) 。在此方法中,在每个单独数据点上绘制一个连续曲线(核),然后将所有这些曲线加在一起以进行单次平滑密度估计。

A density plot is a smoothed, continuous version of a histogram estimated from the data. The most common form of estimation is known as kernel density estimation (KDE). In this method, a continuous curve (the kernel) is drawn at every individual data point and all of these curves are then added together to make a single smooth density estimation.

模块 plotly.figure_factory._2d_density 中的 create_2d_density() 函数返回用于二维密度图的图表对象。

The create_2d_density() function in module plotly.figure_factory._2d_density returns a figure object for a 2D density plot.

以下代码用于根据直方图数据生成二维密度图。

Following code is used to produce 2D Density plot over histogram data.

t = np.linspace(-1, 1.2, 2000)
x = (t**3) + (0.3 * np.random.randn(2000))
y = (t**6) + (0.3 * np.random.randn(2000))
fig = ff.create_2d_density( x, y)
iplot(fig)

下面提及的内容是以上给出的代码的输出。

Below mentioned is the output of the above given code.

density plot

Error Bar Plot

误差棒是数据中误差或不确定性的图形化表示,它们有助于正确的解释。出于科学目的,理解给出数据时报告误差至关重要。

Error bars are graphical representations of the error or uncertainty in data, and they assist correct interpretation. For scientific purposes, reporting of errors is crucial in understanding the given data.

误差棒对解决问题者十分有用,因为误差棒显示出一组测量或计算值中的置信度或精确度。

Error bars are useful to problem solvers because error bars show the confidence or precision in a set of measurements or calculated values.

错误线通常表示数据集的范围和标准差。它们可以帮助显示数据如何围绕平均值分布。可以在各种绘图上生成错误线,例如条形图、折线图、散点图等。

Mostly error bars represent range and standard deviation of a dataset. They can help visualize how the data is spread around the mean value. Error bars can be generated on variety of plots such as bar plot, line plot, scatter plot etc.

go.Scatter() 函数具有 error_xerror_y 属性,用于控制如何生成错误线。

The go.Scatter() function has error_x and error_y properties that control how error bars are generated.

  1. visible (boolean) − Determines whether or not this set of error bars is visible.

Type 属性具有可能取值 " percent " | " constant " | " sqrt " | data ”。它设置用于生成错误线的规则。如果为 "percent",则条形长度对应于基础数据的百分比。在 value 中设置此百分比。如果为 "sqrt",则条形长度对应于基础数据的平方。如果为 "data",则条形长度使用数据集 array 设置。

Type property has possible values "percent" | "constant" | "sqrt" | "data”. It sets the rule used to generate the error bars. If "percent", the bar lengths correspond to a percentage of underlying data. Set this percentage in value. If "sqrt", the bar lengths correspond to the square of the underlying data. If "data", the bar lengths are set with data set array.

  1. symmetric property can be true or false. Accordingly, the error bars will have the same length in both direction or not (top/bottom for vertical bars, left/right for horizontal bars.

  2. array − sets the data corresponding the length of each error bar. Values are plotted relative to the underlying data.

  3. arrayminus − Sets the data corresponding the length of each error bar in the bottom (left) direction for vertical (horizontal) bars Values are plotted relative to the underlying data.

以下代码在散点图上显示对称错误线 −

Following code displays symmetric error bars on a scatter plot −

trace = go.Scatter(
   x = [0, 1, 2], y = [6, 10, 2],
   error_y = dict(
   type = 'data', # value of error bar given in data coordinates
   array = [1, 2, 3], visible = True)
)
data = [trace]
layout = go.Layout(title = 'Symmetric Error Bar')
fig = go.Figure(data = data, layout = layout)
iplot(fig)

下面给出上述代码的输出。

Given below is the output of the above stated code.

error bar plot

通过以下脚本渲染非对称错误图 −

Asymmetric error plot is rendered by following script −

trace = go.Scatter(
   x = [1, 2, 3, 4],
   y =[ 2, 1, 3, 4],
   error_y = dict(
      type = 'data',
      symmetric = False,
      array = [0.1, 0.2, 0.1, 0.1],
      arrayminus = [0.2, 0.4, 1, 0.2]
   )
)
data = [trace]
layout = go.Layout(title = 'Asymmetric Error Bar')
fig = go.Figure(data = data, layout = layout)
iplot(fig)

输出如下所示 −

The output of the same is as given below −

asymmeric error bar