박민준
박민준

Reputation: 1

How do I get the pixel position of a value in an altair chart?

I'm making a dataset to finetune my AI model. I need to render a chart and figure out the pixel position of a specific value.

For example, I want to know the pixel position of the value "(B,3)" in the rendered image from the chart below.

import altair as alt
import pandas as pd

data = pd.DataFrame({'x': ['A', 'B', 'C', 'D', 'E'],
                     'y': [5, 3, 6, 7, 2]})
alt.Chart(data).mark_bar().encode(
    x='x',
    y='y',
)

In matplotlib, there is a transform function to retrieve the pixel value from the data. I tried to find similar function in altair but I couldn't.

I need to plot a chart from vega-lite specs, so I have to use altair.

Upvotes: 0

Views: 38

Answers (1)

无名大侠
无名大侠

Reputation: 11

import altair as alt
import pandas as pd

data = pd.DataFrame({'t': range(101)})
chart = alt.Chart(data).mark_line().encode(
x='x:Q',
y='y:Q',
order='t:Q'
).transform_calculate(
x='cos(datum.t * PI / 50)',
y='sin(datum.t * PI / 25)'
)

In Altair, there isn't a direct equivalent to the transform function in Matplotlib for retrieving pixel values of specific data points in the rendered image. Altair is a declarative visualization library based on Vega-Lite, which focuses on describing the final visual outcome rather than pixel-level operations. Therefore, directly obtaining the pixel position of specific data values in the rendered image is not part of Altair's design.

However, if you need to obtain the pixel position of specific values for fine-tuning your AI model, you might consider the following alternatives:

Using JavaScript and Vega-Lite: Since Altair generates a Vega-Lite specification JSON object, you can render this JSON object into an environment that supports JavaScript (like Jupyter Notebook) and then use JavaScript to get the pixel position. This involves rendering the Vega-Lite chart in a browser and using DOM API to query pixel values.enter code here

Upvotes: 1

Related Questions