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.

Plot legend shows value instead of text specified

See original GitHub issue

On a Plot legend (bokeh 0.12.3), if the name for the legend is the same as the name for the column in the data source, the value will be displayed int he legend instead of the name. Is this expected behavior?

Example:

from bokeh import plotting
from bokeh.models import ColumnDataSource, Range1d
from bokeh.io import curdoc, show
import pandas as pd

data = [{"right1": 10, "right2": 14, "right3": 17,
         "right4": 22, "right5": 37, "right6": 44,
         "right7": 49, "right8": 59}]

source = pd.DataFrame(data)
source = ColumnDataSource(source)

fig = plotting.figure(width=1200, height=900)

fig.hbar(y=1, right="right1", left=0, height=0.5, fill_color="blue", source=source, legend="right1")
fig.hbar(y=1, left="right1", right="right2", height=0.5, fill_color="green",
         line_color="black", source=source, legend="right2")
fig.hbar(y=1, left="right2", right="right3", height=0.5, fill_color="yellow",
         line_color="black", source=source, legend="right3")
fig.hbar(y=1, left="right3", right="right4", height=0.5, fill_color="orange",
         line_color="black", source=source, legend="right4")
fig.hbar(
    y=1, left="right4", right="right5", height=0.5, fill_color="red", line_color="black", source=source,
    legend="right 5")
fig.hbar(
    y=1, left="right5", right="right6", height=0.5, fill_color="purple", line_color="black", source=source,
    legend="right 6")
fig.hbar(y=1, left="right6", right="right7", height=0.5,
         fill_color="grey", line_color="black", source=source, legend="7.")
fig.hbar(
    y=1, left="right7", right="right8", height=0.5, color="black", line_color="black", source=source, legend="*8.",
    fill_alpha=.12, line_alpha=.12)

curdoc().add_root(fig)

show(fig)

As you can see, in the legend for the first 4 hbar(s), the value for that hbar is displayed instead of the text specified in legend=

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:17 (14 by maintainers)

github_iconTop GitHub Comments

7reactions
bryevdvcommented, Oct 17, 2016

@Rubyj Yes, this is a new feature that allows legends to be automatically generated by grouping values in a column data source for a single glyph. In conjunction with a categorical color mapper this can often greatly reduce code needed. If you don’t want this behaviour, there are two options:

  • change the column names to something else, so as not to match the legend string value
  • be explicit that you don’t want the legend to look at field names: i.e passlegend=dict(value="foo") to the glyph function

ping @birdsarah this is the second time today this question has come up. Perhaps we need better documentation support

5reactions
foobarbecuecommented, Jan 11, 2018

The way to make this work is now:

from bokeh.core.properties import value
legend=value("Sunset")

because bokeh.properties has become bokeh.core.properties .

I have to say, I’m a new bokeh user and this legend thing was a major speedbump that cost me about an hour. Couldn’t find the answer until I came across it here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How do you just show the text label in plot legend ...
With a few extra lines the labels can have the same color as their line. just use .set_color() via legend.get_texts() . # Now...
Read more >
Customizing Plot Legends | Python Data Science Handbook
Plot legends give meaning to a visualization, assigning meaning to the various plot ... (alpha value) of the frame, or change the padding...
Read more >
Add a legend to a chart - Microsoft Support
Click the chart. next to the chart, and click Select Data. Select an entry in the Legend Entries (Series) list, and click Edit....
Read more >
Legend guide — Matplotlib 3.6.2 documentation
For example, if you want your axes legend located at the figure's top right-hand corner instead of the axes' corner, simply specify the...
Read more >
LEGEND Statement - SAS Help Center
If both are present, SAS/GRAPH uses the last one specified. ... output to display a long label text, the variable name is displayed...
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