[英]Adding the line of identity to a scatter plot using altair
I have created a basic scatter plot to compare two variables using altair.我创建了一个基本的散点图来使用 altair 比较两个变量。 I expect the variables to be strongly correlated and the points should end up on or close to the line of identity.
我希望这些变量具有很强的相关性,并且这些点最终应该位于或接近于同一直线。
How can I add the line of identity to the plot?如何将身份线添加到情节中?
I would like it to be a line similar to those created by mark_rule
, but extending diagonally instead of vertically or horizontally.我希望它是一条类似于由
mark_rule
创建的mark_rule
,但对角线而不是垂直或水平延伸。
Here is as far as I have gotten:这是我得到的:
import altair as alt
import numpy as np
import pandas as pd
norm = np.random.multivariate_normal([0, 0], [[2, 1.8],[1.8, 2]], 100)
df = pd.DataFrame(norm, columns=['var1', 'var2'])
chart = alt.Chart(df, width=500, height=500).mark_circle(size=100).encode(
alt.X('var1'),
alt.Y('var2'),
).interactive()
line = alt.Chart(
pd.DataFrame({'var1': [-4, 4], 'var2': [-4, 4]})).mark_line().encode(
alt.X('var1'),
alt.Y('var2'),
).interactive()
chart + line
The problems with this example is that the line doesn't extend forever when zooming (like a rule mark) and that the plot gets automatically scaled to the line endings instead of only the points.此示例的问题在于,缩放时线条不会永远延伸(如规则标记),并且绘图会自动缩放到线条末端,而不仅仅是点。
It's not perfect but you could make the line longer and set the scale domain.这并不完美,但您可以延长线并设置比例域。
import altair as alt
import numpy as np
import pandas as pd
norm = np.random.multivariate_normal([0, 0], [[2, 1.8],[1.8, 2]], 100)
df = pd.DataFrame(norm, columns=['var1', 'var2'])
chart = alt.Chart(df, width=500, height=500).mark_circle(size=100).encode(
alt.X('var1', scale=alt.Scale(domain=[-4,4])),
alt.Y('var2', scale=alt.Scale(domain=[-4,4])),
).interactive()
line = alt.Chart(
pd.DataFrame({'var1': [-100, 100], 'var2': [-100, 100]})).mark_line().encode(
alt.X('var1'),
alt.Y('var2'),
).interactive()
chart + line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.