Histogram

摘要

Histogram 类可创建直方图。 通过测量某些值在数据集中显示的频数,直方图直观地概述了一个或多个连续数值变量的分布。

了解有关 ArcGIS Pro 中的直方图的详细信息

说明

创建此对象时,必须使用参数名指定类构造函数的可选参数;这些可选参数不能通过参数位置指定。 有关如何使用关键字指定参数的示例,请参阅代码示例部分。

语法

 Histogram (x, {splitCategory}, {miniChartsPerRow}, {showPreviewChart}, {binCount}, {showMean}, {showMedian}, {showStandardDeviation}, {showComparisonDistribution}, {dataTransformationType}, {dataTransformationParameters}, {title}, {description}, {xTitle}, {yTitle}, {dataSource}, {displaySize}, {theme})
参数说明数据类型
x

The field names for the x-axis variables. The field must be a single numeric field name or a list of numeric field names. For a raster layer, specify a band name.

String
splitCategory

A categorical field that adds a separate series for each unique value in the field.

String
miniChartsPerRow

每行显示的迷你图表的数量。

Integer
showPreviewChart

指定是否在格网图表中显示预览图表。

  • TrueThe preview chart will be displayed.
  • FalseThe preview chart will not be displayed.

(默认值为 False)

Boolean
binCount

The number of bins that will be displayed in the histogram.

Integer
showMean

Specifies whether the mean statistical line will be visible in the histogram.

  • TrueThe mean line will be visible in the histogram.
  • FalseThe mean line will not be visible in the histogram.
Boolean
showMedian

Specifies whether the median statistical line will be visible in the histogram.

  • TrueThe median line will be visible in the histogram.
  • FalseThe median line will not be visible in the histogram.
Boolean
showStandardDeviation

Specifies whether the standard deviation lines will be visible in the histogram.

  • TrueThe standard deviation lines will be visible in the histogram.
  • FalseThe standard deviation lines will not be visible in the histogram.
Boolean
showComparisonDistribution

Specifies whether the normal distribution comparison line will be visible in the histogram.

  • TrueThe comparison line will be visible in the histogram.
  • FalseThe comparison line will not be visible in the histogram.
Boolean
dataTransformationType

Specifies the data transformation type that will be applied. When multiple x-axis fields are defined, use a list of strings—in the same order as the x parameter—to specify a transformation for each field.

  • noneNo transformation will be applied.
  • logarithmicA logarithmic transformation will be applied.
  • squareRootA square root transformation will be applied.
  • inverseAn inverse transformation will be applied.
  • boxCoxA box-cox transformation will be applied.
String
dataTransformationParameters

The data transformation parameters for the boxCox transformation. This parameter accepts a two-item list in which the first item is the power parameter and the second item is the shift parameter. When multiple x-axis fields are defined, use a two-dimensional list—in the same order as the x parameter—in which each inner list corresponds to the transformation parameters for one field. In this case, specify None for list items that correspond to transformations that are not boxCox.


chart = arcpy.charts.Histogram(
    x=['POPULATION', 'POPULATION'],    
    dataTransformationType=['inverse', 'boxCox'],
    dataTransformationParameters=[None, [2, 5000]]    
)

List
title

图表的标题。 标题文本显示在图表视图的顶部,并在内容窗格中用作图表的标注。

String
description

图表的描述。 该描述文本会显示于图表视图底部。

String
xTitle

图表 x 轴的标题。

String
yTitle

图表 y 轴的标题。

String
dataSource

图表的数据源。 当通过 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebooks 中时,将读取数据源并将其呈现在图表上。 其中包括以下有效数据源:

  • 数据集的路径(包括本地数据集、UNC 路径和服务 URL)
  • Apache Arrow 表对象
  • LayerTable 对象

Object
displaySize
[displaySize,...]

当使用 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebooks 中时,图表的大小。 该值必须指定为两个项目的列表,其中第一个项目是图表的宽度,第二个项目是图表的高度。

List
theme

指定将应用于图表的主题。

  • LightThe Light theme will be applied.
  • MediumThe Medium theme will be applied.
  • DarkThe Dark theme will be applied.
String

属性

属性说明数据类型
binCount
(可读写)

将在直方图中显示的立方图格数量

Integer
color
(可读写)

将应用于每个图表系列的颜色。 该值应为十六进制字符串值的列表。 如果未设置此属性,则系列颜色将匹配符号系统颜色(如果适用)。否则,将应用标准调色板。

List
dataSource
(可读写)

图表的数据源。 当通过 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebook 中时,将读取数据源并将其呈现在图表上。 其中包括以下有效数据源:

  • 数据集的路径(包括本地数据集、UNC 路径和服务 URL)
  • Apache Arrow 表对象
  • LayerTable 对象

Object
dataTransformationParameters
(可读写)

Box-Cox 变换的数据变换参数。 此参数接受两个项目的列表,其中第一个项目是幂参数,第二个项目是偏移参数。 在定义多个 x 轴字段时,使用两维列表,顺序与 x 参数相同,其中每个内部列表对应一个字段的变换参数。 在此情况下,为对应非 boxCox 的变换的列表项目指定 None


chart = arcpy.charts.Histogram(
    x=['POPULATION', 'POPULATION'],    
    dataTransformationType=['inverse', 'boxCox'],
    dataTransformationParameters=[None, [2, 5000]]    
)

List
dataTransformationType
(可读写)

指定将应用的数据变换类型。 在定义多个 x 轴字段时,使用字符串列表,顺序与 x 参数相同,来指定每个字段的变换。

  • none将不会应用任何变换。
  • logarithmic将应用对数变换。
  • squareRoot将应用平方根变换。
  • inverse将应用逆变换。
  • boxCox将应用 Box-Cox 变换。
String
description
(可读写)

图表的描述。 该描述文本会显示于图表视图底部。

String
displaySize
(可读写)

当使用 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebook 中时,图表的大小。 该值必须指定为两个项目的列表,其中第一个项目是图表的宽度,第二个项目是图表的高度。

List
enableServerSideProcessing
(可读写)

指定当数据来自企业级数据库或云数据仓库时,是否将使用服务器端处理

  • True将启用服务器端处理。
  • False将禁用服务器端处理。
Boolean
legend
(可读写)

图表图例的属性。

  • alignment - 指定图例的对齐。 对齐选项为 leftbottomtopright
  • title - 图例的标题。
  • visible - 指定是否在图表视图中显示图例。 True 显示图例,False 隐藏图例。
Object
miniChartsPerRow
(可读写)

每行显示的迷你图表的数量。

Integer
showComparisonDistribution
(可读写)

指定是否将在直方图中显示正态分布比较线。

  • True将在直方图中显示比较线。
  • False将不会在直方图中显示比较线。
Boolean
showMean
(可读写)

指定是否将在直方图中显示平均统计线。

  • True将在直方图中显示平均线。
  • False将不会在直方图中显示平均线。
Boolean
showMedian
(可读写)

指定是否将在直方图中显示中值统计线。

  • True将在直方图中显示中值线。
  • False将不会在直方图中显示中值线。
Boolean
showPreviewChart
(可读写)

指定是否在格网图表中显示预览图表。

  • True将显示预览图表。
  • False不会显示预览图表。
Boolean
showStandardDeviation
(可读写)

指定是否将在直方图中显示标准差线。

  • True将在直方图中显示标准差线。
  • False将不会在直方图中显示标准差线。
Boolean
splitCategory
(可读写)

类别字段,将为字段中每一个唯一值添加单独的系列。

String
theme
(可读写)

指定将应用于图表的主题。

  • Light将应用浅色主题。
  • Medium将应用中等主题。
  • Dark将应用深色主题。
String
title
(可读写)

图表的标题。 标题文本显示于图表视图顶部,并用作内容窗格中按绘制顺序列出选项卡 按绘制顺序列出 的标注。

String
type
(只读)

用于指示图表类型的字符串值。

String
x
(可读写)

x 轴变量的字段名称。 字段必须为单一数值字段名称或数字字段名称列表。 对于栅格图层,指定波段名称。

String
xAxis
(可读写)

指定 x 轴的属性。

  • addGuide - 使用 Guide 对象向轴添加一条参考线。
  • field - x 轴变量的字段名称。 该字段必须为数值字段。
  • listGuides - 轴的 Guide 对象的列表。
  • logarithmic - Histogram 类不支持此属性。
  • maximum - 最大轴边界。
  • minimum - 最小轴边界。
  • removeGuide - 移除 Guide 对象参数指定的参考线。
  • sort - Histogram 类不支持此属性。
  • title - 图表中所显示的轴的标注。
  • useAdaptiveBounds - 指定格网图表是带自适应还是固定轴边界显示。 True 显示带自适应边界的轴。 False 显示带固定边界的轴。
Object
yAxis
(可读写)

指定 y 轴的属性。

  • addGuide - 使用 Guide 对象向轴添加一条参考线。
  • field - Histogram 类不支持此属性。
  • listGuides - 轴的 Guide 对象的列表。
  • logarithmic - Histogram 类不支持此属性。
  • maximum - 最大轴边界。
  • minimum - 最小轴边界。
  • removeGuide - 移除 Guide 对象参数指定的参考线。
  • sort - Histogram 类不支持此属性。
  • title - 图表中所显示的轴的标注。
  • useAdaptiveBounds - 指定格网图表是带自适应还是固定轴边界显示。 True 显示带自适应边界的轴。 False 显示带固定边界的轴。
Object

方法概述

方法说明
addToLayer (layer_or_layerfile)

addToLayer 方法可将图表对象添加到图层或表视图。

exportToJPEG (path, width, height)

exportToJPEG 方法用于将图表导出为 JPEG 格式。

exportToPNG (path, width, height)

exportToPNG 方法用于将图表导出为 PNG 格式。

exportToSVG (path, width, height)

exportToSVG 方法可将图表导出为 SVG 格式。

updateChart ()

updateChart 方法可更新图表属性,以同步对象与之前添加到图层的图表之间的更改。

方法

addToLayer (layer_or_layerfile)
参数说明数据类型
layer_or_layerfile

The chart will be added to the target object. This argument can be a Layer or Table object.

Object

使用 addToLayer 方法将图表对象添加到图层或表通常是定义图表属性之后的最终步骤。

将图表添加到现有图层。

import arcpy

# Insert creation of chart object here
...

aprx = arcpy.mp.ArcGISProject("current")
map = aprx.listMaps()[0]
censusLayer = map.listLayers('Census Block Groups')[0]

# Add chart object to a layer
chart.addToLayer(censusLayer)
exportToJPEG (path, width, height)
参数说明数据类型
path

The path where the chart will be exported in JPEG format.

String
width

The width of the output graphic.

Integer
height

The height of the output graphic.

Integer

图表将导出为 .jpg 文件,以在 ArcGIS Pro 外部进行共享和查看。 JPEG 是使用有损压缩以减小文件大小的栅格图像格式。

将具有要素服务数据源的图表导出到 .jpeg 文件中。

import arcpy

# Insert creation of chart object here
...

featureServiceURL = r'https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/CrimesChiTheft/FeatureServer/0'

# Set data source of chart object to a feature service URL
chart.dataSource = featureServiceURL

# Save the chart to file with dimensions width=800, height=600
chart.exportToJPEG('theftsPerBeat.jpg', 800, 600)
exportToPNG (path, width, height)
参数说明数据类型
path

The path where the chart will be exported in PNG format.

String
width

The width of the output graphic.

Integer
height

The height of the output graphic.

Integer

图表将导出为 .png 文件,以在 ArcGIS Pro 外部进行共享和查看。 PNG 是一种栅格图像格式,使用无损压缩保持图像质量。

从图层访问现有图表,更新标题,然后将其导出为 .png 文件。

import arcpy

lyr = arcpy.mp.ArcGISProject('current').listLayers()[0]

# Retrieve the first chart belonging to the layer
chart = lyr.listCharts()[0]

# Update chart title
chart.title = 'Voter Turnout by State'

# Save the chart to PNG file with dimensions width=800, height=600
chart.exportToPNG('populationByState.png', 800, 600)
exportToSVG (path, width, height)
参数说明数据类型
path

将图表以 SVG 格式导出时的路径。

String
width

输出图形的宽度。

Integer
height

输出图形的高度。

Integer

图表将导出为 .svg 文件,以在 ArcGIS Pro 外部进行共享和查看。 SVG 是一种矢量图像格式,支持编辑和无限缩放图形且不会导致质量下降。

将具有工程图层数据源的图表导出到 .svg 文件中。

import arcpy

# Insert creation of chart object here
# ...

aprx = arcpy.mp.ArcGISProject('current')
censusLayer = aprx.listMaps()[0].listLayers('Census Block Groups')[0]

# Set data source of chart object to a layer in current project
chart.dataSource = censusLayer

# Save the chart to file with dimensions width=500, height=500
chart.exportToSVG('populationByState.svg', 500, 500)
updateChart ()

使用 addToLayer 方法将图表对象添加到图层通常是定义图表属性之后的最终步骤。

您也可以修改原始图表的属性,而不是从头开始创建新图表。 然后,您可以使用 updateChart 方法将所有更改同步到已添加到该图层的图表中。 由此可在图表属性窗格和图表视图中显示所做的更改。

可以使用 updateChart 方法将图表属性更改同步到图层中。

import arcpy

# Insert creation of chart object here
...

chart.addToLayer(myLayer)

# Further modification is necessary
chart.description = "Data from the U.S. Census Bureau"
chart.updateChart()

代码示例

可以使用图层名称作为数据源来创建直方图,然后将该直方图导出至 .svg 文件。

import arcpy

chart = arcpy.charts.Histogram("price", binCount=20, showMedian=True)																											
chart.dataSource = "airbnb_listings"
chart.title = "Distribution of AirBnB Listing Price"
chart.exportToSVG("histogram.svg", width=1000, height=800)