将Altair中的值进行缩放以适应图层?

huangapple go评论56阅读模式
英文:

Scaling values in Altair to fit on layer?

问题

我有一个股票价格图和一个包含预期收益和实际收益的层次。

问题是股价可以是50美元或100美元,而收益值通常在0.50美元或1.00美元左右。我想将它们叠加在一起,因为日期很好地对齐,但收益值要小得多,所以它们总是在图的底部。

我查看了alt.Scale(),以使收益(绿色和灰色圆圈)不考虑“收盘价”,并将圆圈放得更高。在Scale中是否有可以帮助的属性,或者Altair中的另一个函数?

这是我的收益图部分的代码:

expected_chart = alt.Chart(earnings_df).mark_point(
      size=point_size, 
      strokeWidth=stroke_size, 
      color="gray"
      ).encode(
        x=x_axis,
        y=alt.Y("EstimatedEarning:Q", axis=alt.Axis(labels=True), 
                scale=alt.Scale(domain=[min_earning, max_earning], type="log")),
        tooltip=tooltip,
  ).transform_filter(date_filter).properties(title=f"Earnings beat quarterly {symbol_name}",
               height=height,
               width=500)

将Altair中的值进行缩放以适应图层?

英文:

I have a graph of stock prices and a layer with Earnings Expected and Earnings Actual.

The issue is the stock price can be $50 or $100 where the earnings values are usually within a couple dollars like $0.50 or $1.00. I'd like to layer both since dates line up nicely but earnings values are way smaller so they are always on the bottom of the graph.

将Altair中的值进行缩放以适应图层?

I looked at alt.Scale() to make the earnings (green and gray circles) disregard the "Close price" and scale the circles higher.

Is there an attribute in Scale which can help or another function in Altair?

Here's my code for the Earnings chart portion:

expected_chart = alt.Chart(earnings_df).mark_point(
      size=point_size, 
      strokeWidth=stroke_size, 
      color="gray"
      ).encode(
        x=x_axis,
        y=alt.Y("EstimatedEarning:Q", axis=alt.Axis(labels=True), 
                scale=alt.Scale(domain=[min_earning, max_earning], type="log")),
        tooltip=tooltip,
  ).transform_filter(date_filter).properties(title=f"Earnings beat quarterly {symbol_name}",
               height=height,
               width=500)

答案1

得分: 1

以下是翻译好的内容:

"这听起来你想要一个带有独立y轴刻度的双y轴,就像这个示例中的一样:"

import altair as alt
from vega_datasets import data

source = data.seattle_weather()

base = alt.Chart(source).encode(
    alt.X('month(date):T').axis(title=None)
)

area = base.mark_area(opacity=0.3, color='#57A44C').encode(
    alt.Y('average(temp_max)').title('平均温度 (°C)', titleColor='#57A44C'),
    alt.Y2('average(temp_min)')
)

line = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
    alt.Y('average(precipitation)').title('降水量 (英寸)', titleColor='#5276A7')
)

alt.layer(area, line).resolve_scale(
    y='independent'
)
英文:

It sounds like you want a dual y-axis with independent y-scales as in this example:

import altair as alt
from vega_datasets import data

source = data.seattle_weather()

base = alt.Chart(source).encode(
    alt.X('month(date):T').axis(title=None)
)

area = base.mark_area(opacity=0.3, color='#57A44C').encode(
    alt.Y('average(temp_max)').title('Avg. Temperature (°C)', titleColor='#57A44C'),
    alt.Y2('average(temp_min)')
)

line = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
    alt.Y('average(precipitation)').title('Precipitation (inches)', titleColor='#5276A7')
)

alt.layer(area, line).resolve_scale(
    y='independent'
)

huangapple
  • 本文由 发表于 2023年8月5日 11:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定