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.

Seaborn Objects: Horizontal line

See original GitHub issue

Hello.

Firstly, thanks for the addition of seaborn objects. It seems really powerful and easy to use. Awesome!

Secondly, I’d like to ask, whether a functionality is available in the new API. I’d like to draw a horizontal line together with a line derived from data.

For example:

import seaborn.objects as so
from datetime import datetime
import pandas as pd

data = pd.DataFrame({
    "date": [datetime(2018, 1, 1), datetime(2018, 2, 1), datetime(2018, 3, 1)],
    "count": [250, 130, 300]
})

(
    so.Plot(data=data, x="date", y="count")
    .add(so.Line())
    .add(so.Line(color="grey"), so.Agg())
)

I’d like the second so.Line() to draw a horizontal line that is made out of mean of all values in “count” column. It however averages over automatic groups from “date” column. This functionality (if it isn’t possible already) would be very useful in time-related data, where you would e.g. like to display a yearly average.

It would generally be useful to draw any constant horizontal (or vertical line). Currently the best option that occurs to me is to switch to matplotlib’s axhline.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mwaskomcommented, Sep 11, 2022

I do plan to add a Rule mark but it hasn’t happened yet. In the meantime, a little clumsy, but something like this might do?

(
    so.Plot(data=data, x="date", y="count")
    .add(so.Line(), x=[data["date"].min(), data["date"].max()], y=150, data={})
    .add(so.Line(color="grey"), so.Agg())
)

image

0reactions
mwaskomcommented, Sep 11, 2022

The problem with by is that the operation you want is “drop {orient} from the groupers”, otherwise to drop the grouping variable from a more complex plot, you’d have to do

so.Plot(data, x=..., y=..., color=..., linewidth=...)
.add(so.Rule(), so.Agg(by=["color", "linewidth"]))

which is kind of annoying. And it doesn’t really make sense to otherwise choose whether to group by the semantic variable (color, etc.) since you can already drop them by passing None in the layer definition.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding a horizontal line in a Seaborn plot in Python
A barplot will be used in this tutorial and we will put a horizontal line on this bar plot using the axhline() function....
Read more >
Draw a line at specific position/annotate a Facetgrid in seaborn
I now want to draw to distinct horizontal lines in each of the plot. For instance one horizontal line (with coordinated (0,10)) only...
Read more >
How to Add Vertical/Horizontal Lines to Subplots with Seaborn
To add a horizontal and vertical line we can use Seaborn's refline() function with x and y y co-ordinates for the locations of...
Read more >
seaborn.lineplot — seaborn 0.12.1 documentation - PyData |
Object determining how to draw the lines for different levels of the style variable. ... Use the orient parameter to aggregate and sort...
Read more >
Plot a Horizontal line in Matplotlib - GeeksforGeeks
The axhline() function in pyplot module of matplotlib library is used to add a horizontal line across the axis. ... Parameters: y: Position...
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