Skip to content

Conversation

ahuang11
Copy link
Collaborator

@ahuang11 ahuang11 commented Sep 29, 2025

If only two columns, use y (this would previously crash because of text = [c for c in data.columns if c not in (x, y)][0]):

import hvplot.pandas
import pandas as pd

df = pd.DataFrame({
    'lat': [-34.58, -15.78, -33.45, 4.60, 10.48],
    'lon': [-58.66, -47.91, -70.66, -74.08, -66.86],
})

df.hvplot.points(x='lon', y='lat') * \
df.hvplot.labels(x='lon', y='lat', hover=False, padding=0.2)
image

@ahuang11 ahuang11 marked this pull request as ready for review September 29, 2025 11:40
@ahuang11 ahuang11 requested review from Azaya89 and maximlt September 29, 2025 11:40
@ahuang11 ahuang11 changed the title Automatically use y as default 'text' kwarg Automatically use y as default 'text' kwarg if only two cols Sep 29, 2025
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.

I'm OK with this change. However the tests are broken.

@ahuang11
Copy link
Collaborator Author

Thanks I think it's because the column name has units. Going to experiment with LLM...

@copilot can you fix the test by dropping the correct column?

@ahuang11
Copy link
Collaborator Author

:'( okay maybe I don't have permissions to this repo to launch

@ahuang11 ahuang11 requested a review from Copilot October 10, 2025 16:10
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a crash in the labels method when working with DataFrames that have only two columns by automatically using the y-axis column as the default text label.

  • Fixed IndexError when no third column is available for text labels
  • Added fallback logic to use y-column as text when only x and y columns exist
  • Added test coverage for the two-column scenario

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
hvplot/converter.py Added conditional logic to handle two-column DataFrames by using y as default text
hvplot/tests/testcharts.py Added test case to verify labels work correctly with only two columns

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +546 to +547
assert plot.kdims == ['Longitude', 'Latitude']
assert plot.vdims == ['Latitude']
Copy link
Member

Choose a reason for hiding this comment

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

Just to double check, is it ok for a holoviews element to have a dimension both in the kdims and vdims?

Copy link
Collaborator

@Azaya89 Azaya89 Oct 14, 2025

Choose a reason for hiding this comment

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

I think it can if you pass them in directly:

import holoviews as hv
import pandas as pd

df = pd.DataFrame({
    'lat': [-34.58, -15.78, -33.45, 4.60, 10.48],
    'lon': [-58.66, -47.91, -70.66, -74.08, -66.86],
})

plot = hv.Points(df, ['lon', 'lat'], 'lon')
plot.dimensions()

[Dimension('lon'), Dimension('lat'), Dimension('lon')]

This is what is now achieved here via next((c for c in data.columns if c not in (x, y)), y), no? @ahuang11

Copy link
Collaborator Author

@ahuang11 ahuang11 Oct 15, 2025

Choose a reason for hiding this comment

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

Almost! Very close:

next((c for c in data.columns if c not in (x, y)), y) should default to y, which is:

import holoviews as hv
import pandas as pd

df = pd.DataFrame({
    'lat': [-34.58, -15.78, -33.45, 4.60, 10.48],
    'lon': [-58.66, -47.91, -70.66, -74.08, -66.86],
})

plot = hv.Points(df, ['lon', 'lat'], 'lat')  # lat not lon
plot.dimensions()

If df has an extra col, it'll use that:

import holoviews as hv
import pandas as pd

df = pd.DataFrame({
    'lat': [-34.58, -15.78, -33.45, 4.60, 10.48],
    'lon': [-58.66, -47.91, -70.66, -74.08, -66.86],
    'col': ['a', 'b', 'c', 'd', 'e']
})

plot = hv.Points(df, ['lon', 'lat'], 'col')
plot.dimensions()

And yes to Maxime's question:
image

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.

3 participants