Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow string formatting for labels #1140

Merged
merged 9 commits into from Oct 6, 2023
Merged

Allow string formatting for labels #1140

merged 9 commits into from Oct 6, 2023

Conversation

ahuang11
Copy link
Collaborator

@ahuang11 ahuang11 commented Sep 15, 2023

Closes #1095

I thought about putting this in holoviews, but passing format strings to vdims seems a tad strange...
hv.Labels(kdims=["x", "y"], vdims=["{x} {y}"]), but passing it in hvplot feels natural

import pandas as pd
import hvplot.pandas

df = pd.DataFrame(
    {
        "City": ["Buenos Aires", "Brasilia", "Santiago", "Bogota", "Caracas"],
        "Country": ["Argentina", "Brazil", "Chile", "Colombia", "Venezuela"],
        "Latitude": [-34.58, -15.78, -33.45, 4.60, 10.48],
        "Longitude": [-58.66, -47.91, -70.66, -74.08, -66.86],
    }
)
df.hvplot.points("Longitude", "Latitude", by="City") * df.hvplot.labels(
    "Longitude", "Latitude", "{City} ({Latitude}, {Longitude})", by="City"
)
image

@philippjfr philippjfr added this to the 0.9.0 milestone Sep 18, 2023
hvplot/tests/testcharts.py Outdated Show resolved Hide resolved
hvplot/tests/testcharts.py Outdated Show resolved Hide resolved
@maximlt maximlt self-requested a review October 2, 2023 08:22
Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a useful feature to me (even if I have rarely if ever used labels!).

I would have expected to be able to use formatting options like {Density:.2f} except that it raised an error when I tried. We could merge this feature without support for that, it'd be far less useful though. If you try to implement it, I'd suggest leveraging:

  • '{a} {b}'.format(a=1) raises a KeyError, so you don't need to check whether some columns are wrongly typed/missing, it does it for you
  • '{a} {b}'.format(a=1, b=2, c=3) doesn't raise an error so you can pass all the columns data to .format and it'll happily use only what it needs.

examples/reference/pandas/labels.ipynb Show resolved Hide resolved
hvplot/converter.py Show resolved Hide resolved
@ahuang11
Copy link
Collaborator Author

ahuang11 commented Oct 4, 2023

I added formatting capabilities.

import pandas as pd
import hvplot.pandas


df = pd.DataFrame({
    "Latitude": [-34.58, -15.78, -33.45],
    "Longitude": [-58.66, -47.91, -70.66],
})
plot = df.hvplot.labels("Longitude", "Latitude", text="{Latitude:.5f} {Longitude:.1f}")
assert plot.data["text"]
image

hvplot/converter.py Show resolved Hide resolved
hvplot/converter.py Outdated Show resolved Hide resolved
@maximlt maximlt merged commit 6d0d859 into main Oct 6, 2023
7 checks passed
@maximlt maximlt deleted the format_labels branch October 6, 2023 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow string formatting by column name for labels' text
3 participants