简体   繁体   English

Altair - 在分面图中绘制圆、椭圆、注释

[英]Altair - plot circle, ellipse, annotations in faceted chart

Is it possible to plot lines and geometric shapes inside an Altair chart that follow the units of measure of the chart x and y variables?是否可以按照图表 x 和 y 变量的度量单位在 Altair 图表内绘制线条和几何形状? The chart may be faceted, and the shapes and lines depend on the data in each specific facet.图表可能是多面的,形状和线条取决于每个特定面中的数据。

A reproducible example:一个可重现的例子:

import pandas as pd
import numpy as np
import altair as alt
alt.renderers.enable('notebook')

# make some data to test
N = 1000
df = pd.DataFrame({
    'x1': np.random.normal(0, 1, N),
    'x2': np.random.normal(0, 1, N),
    'facet': np.random.choice(list('ABCDEFGHI'), N),
})

# derived variables 
df['y1'] = np.where(np.sqrt(df['x1']**2 + df['x2']**2) > 2, 'F', 'P')
df['y2'] = 0.5*df['x2'] + 2.0 + np.random.normal(0, .5, N)
df['color'] = np.where(df['y1'].eq('F'), 'red', 'green')

# custom color map
domain = ['F', 'P']
range_ = ['red', 'green']

# create and render the chart
p1 = alt.Chart(df).mark_circle(opacity=1, size=15).encode(
    alt.X('x1', scale=alt.Scale(domain=(-4, 4))),
    alt.Y('x2', scale=alt.Scale(domain=(-4, 4))),
    color=alt.Color('y1', scale=alt.Scale(domain=domain, range=range_)),
    facet='facet'
)

# set some additional properties
p1.properties(width=150, height=150, columns=3).resolve_scale()

Which produces the following output:产生以下输出:

在此处输入图片说明

Q1: Is is possible to draw a circle centered at 0, 0 with radius=2 in each of the charts as illustrated in the 2nd facet? Q1:是否可以在每个图表中绘制一个以 0, 0 为中心且半径 = 2 的圆,如第二个方面所示?

The circle units of measure would be the same as the x and y units of measure.圆的度量单位将与 x 和 y 的度量单位相同。 In this case, x and y may represent a physical linear measurement, where lenght per pixel is uniform in x and yie it could be a dart board.在这种情况下,x 和 y 可能表示物理线性测量,其中每个像素的长度在 x 上是均匀的,yie 它可能是一个飞镖板。

The analogy may be to R lattice xyplot aspect = 'iso'.类比可能是 R 格子 xyplot aspect = 'iso'。 See aspect description here: https://rdrr.io/cran/lattice/man/xyplot.html在此处查看方面描述: https : //rdrr.io/cran/lattice/man/xyplot.html

Q2: Is it possible to add a text annotation in one corner of the plot that counts the number of "F" (red) in each chart? Q2:是否可以在绘图的一个角落添加一个文本注释来计算每个图表中“F”(红色)的数量?

Q3: For cases where the axis are not "iso" and have different units of measure, can an line and ellipse (say, a 95% density ellipse) be plotted as illustrated in the 2nd facet below? Q3:对于轴不是“iso”并且具有不同测量单位的情况,是否可以绘制一条线和椭圆(例如,95% 密度的椭圆),如下面的第二个方面所示? Perhaps with the slope and intercept of the line of fit annotated on the chart?也许在图表上注释了拟合线的斜率和截距?

Example:例子:

# create and render the chart
p1 = alt.Chart(df).mark_point().encode(
    x='x2',
    y='y2',
    facet='facet'
)

# set some additional properties
p1.properties(width=150, height=150, columns=3).resolve_scale()

在此处输入图片说明

In R Lattice, these types of visualizations were accomplished through use of 'aspect', some specific lattice/grid functions and in some cases writing a custom 'panel' (facet) function, that had access to the indexes of the data in each facet and could run linear models in each facet and display the the results.在 R Lattice 中,这些类型的可视化是通过使用“aspect”、一些特定的网格/网格函数以及在某些情况下编写自定义“面板”(facet)函数来完成的,该函数可以访问每个 facet 中的数据索引并且可以在每个方面运行线性模型并显示结果。

There is no mechanism in Altair for this kind of annotation, short of creating data which underlies the desired annotations and drawing them as normal chart layers. Altair 中没有这种注释的机制,除了创建作为所需注释基础的数据并将它们绘制为普通图表层之外。

Support for general chart annotations is an open issue in Vega and Vega-Lite, the rendering libraries that Altair uses.对一般图表注释的支持是 Altair 使用的渲染库 Vega 和 Vega-Lite 中的一个悬而未决的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM