Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Altair, how to set the size of the connected points in a line chart?

Tags:

python

altair

I want to plot a line chart (with connected point markers) in Altair. I know how to change the linewidth (by setting the strokeWidth) but I don't know how I can change the size of these point markers. Below is my code:

altair.Chart(ys.reset_index()).mark_line(point=True, strokeWidth=5).encode(
        x="Time:T",
        y="HL:Q",
        color=altair.Color(
            "Time Series Component",
            scale=altair.Scale(scheme="dark2")
        ),
        tooltip=["Time Series Component", "Time", "HL"]
    ).interactive().properties(
        width=1000,
        height=500
    ).configure_axis(
        labelFontSize=20,
        titleFontSize=20
    ).configure_legend(
        orient="right"
    )
like image 945
James Chang Avatar asked Aug 10 '19 18:08

James Chang


People also ask

How do I change my Altair scale?

By default Altair uses a linear mapping between the domain values (MIC) and the range values (pixels). To get a better overview of the data, we can apply a different scale transformation. To change the scale type, we'll set the scale attribute, using the alt. Scale method and type parameter.

How do I change the color of my Altair chart?

Customizing Colors If you don't like the colors chosen by Altair for your scatter plot, you can customize the colors. The default colors can be changed using the scale argument of the Color class, By passing the Scale class to the scale argument.

How do I get rid of gridlines on Altair?

We can remove the grid lines on x or y-axis by specifying the argument grid=False inside alt. X() or alt. Y() method in the encoding channels.


1 Answers

One way to do it is to use configure_point(size=SIZE). For example:

import altair as alt
import pandas as pd
import numpy as np

np.random.seed(0)

ys = pd.DataFrame({
    'Time': pd.date_range('2019-01-01', freq='D', periods=30),
    'HL': np.random.randn(30).cumsum(),
    'Time Series Component': np.random.choice(list('ABC'), 30),
})

alt.Chart(ys.reset_index()).mark_line(point=True, strokeWidth=5).encode(
    x="Time:T",
    y="HL:Q",
    color=alt.Color(
        "Time Series Component",
        scale=alt.Scale(scheme="dark2")
    ),
    tooltip=["Time Series Component", "Time", "HL"]
).interactive().properties(
    width=1000,
    height=500
).configure_axis(
    labelFontSize=20,
    titleFontSize=20
).configure_legend(
    orient="right"
).configure_point(
    size=200
)

enter image description here

See https://altair-viz.github.io/user_guide/customization.html for more information about customizing altair visualizations.

like image 100
jakevdp Avatar answered Sep 29 '22 09:09

jakevdp