question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an horizontal line to an existent chart

See original GitHub issue

Hello, I am using chime project to simulate some numbers to a local hospital and I would like to add a line with the maximum hospital capacity that will work as a threshold. For that I need to add a horizontal line, i.e, y=100 to this chart. I am having an hard time doing it, though.

What would be the best approach to do this?

Kind regards

def build_census_chart(
    *,
    alt,
    census_df: pd.DataFrame,
    max_y_axis: Optional[int] = None,
    hospital_maximum_capacity: Optional[int] = None,
) -> Chart:
    """Build census chart."""
    y_scale = alt.Scale()
    if max_y_axis:
        y_scale.domain = (0, max_y_axis)

    x = dict(shorthand="date:T", title="Date", axis=alt.Axis(format=(DATE_FORMAT)))
    y = dict(shorthand="value:Q", title="Census", scale=y_scale)
    color = "key:N"
    tooltip = ["date:T", alt.Tooltip("value:Q", format=".0f", title="Census"), "key:N"]

    # TODO fix the fold to allow any number of dispositions
    points = (
        alt.Chart()
        .transform_fold(fold=["hospitalized", "icu", "ventilated"])
        .encode(x=alt.X(**x), y=alt.Y(**y), color=color, tooltip=tooltip)
        .mark_line(point=True)
    )
    bar = (
        alt.Chart()
        .encode(x=alt.X(**x))
        .transform_filter(alt.datum.day == 0)
        .mark_rule(color="black", opacity=0.35, size=2)
    )
    '''bar = (
        alt.Chart()
        .encode(y=alt.Y(**y))
        .transform_filter(alt.datum.day == 0)
        .mark_rule(color="red", opacity=0.35, size=2)
    )'''
   
    return alt.layer(points, bar, data=census_df)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
jakevdpcommented, Apr 2, 2020

The best way to do this is with a rule mark:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'x': np.random.randn(100),
    'y': np.random.randn(100)
})

chart = alt.Chart(df).mark_point().encode(x='x', y='y')
line = alt.Chart(pd.DataFrame({'y': [1]})).mark_rule().encode(y='y')

chart + line

visualization - 2020-04-02T065631 323

Note that in the next version of Altair (4.2) this will be slightly easier using the datum encoding; the horizontal line will look something like this:

alt.Chart().mark_rule().encode(y=alt.datum(1))

(The final API has not been decided yet)

5reactions
jakevdpcommented, Apr 2, 2020

Yes, for example, use

mark_rule(strokeDash=[10, 10])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a Horizontal Line to an Excel Chart - Peltier Tech
Right click on the added series, and change its chart type to XY Scatter With Straight ...
Read more >
How to Add a Horizontal Line in a Chart in Excel
Add a Horizontal Target Line in Column Chart · Go to → Fill & Line → Line. · Change line style to “No...
Read more >
How to add horizontal line to chart - Get Digital Help
Select the chart. Go to tab "Design"; Add the "Primary Vertical" axis; Double press with left mouse button on axis values to open...
Read more >
How to add a line in Excel graph: average line, benchmark, etc.
Insert a new column beside your source data. · Right-click the existing graph, and choose Select Data… from the context menu: · In...
Read more >
How to add a horizontal line to the chart - Microsoft Excel 2016
Under Chart Tools, on the Design tab, in the Data group, choose Select Data: Select Data in Excel 2016 · Right-click in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found