A request for an example with Legend tweaks

Hi,

I have a bar chart with numerous values and as a result, the legend is come out large enough to obscure the data. I’d love to tweak the legend to be smaller text, and maybe multiple columns, but I have no idea how to do it. I used the Bar code in my previous post. And the result is below (I messed with the palette).

bp = Bar(df_stations, values='count', label='hod', stack='stations', tools=["hover", "crosshair", "pan",
             "box_zoom", "wheel_zoom", "reset", "resize", "save"], legend=True,
         plot_height=400, plot_width=700, color=PiYG11)

   I've included the image, but I had to anonymize the data on the legend to share. The size of the font might be off because of it, but everything else is the exact captured image.


Lya,

As of the latest release, all of the configurable legend properties are described here:

  http://bokeh.pydata.org/en/0.11.1/docs/user_guide/styling.html#location

We are working towards a freeze for 0.12 which will have the ability to put legends outside the central plot region, as well as array legends horizontally (no support yet for multiple columns but you could split up and create two legends "by hand" if necessary). It's possible you might be able to use one of the "dev builds" to get this capability earlier:

  http://bokeh.pydata.org/en/0.11.1/docs/installation.html#developer-builds

But please know that the dev builds are not tested to the same standards are regular releases (but it may work perfectly well for your specific use case).

Bryan

···

On Jun 13, 2016, at 1:44 PM, Lya Batlle <[email protected]> wrote:

Hi,

   I have a bar chart with numerous values and as a result, the legend is come out large enough to obscure the data. I'd love to tweak the legend to be smaller text, and maybe multiple columns, but I have no idea how to do it. I used the Bar code in my previous post. And the result is below (I messed with the palette).

bp = Bar(df_stations, values='count', label='hod', stack='stations', tools=["hover", "crosshair", "pan",
             "box_zoom", "wheel_zoom", "reset", "resize", "save"], legend=True,
         plot_height=400, plot_width=700, color=PiYG11)
   
   I've included the image, but I had to anonymize the data on the legend to share. The size of the font might be off because of it, but everything else is the exact captured image.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/484f850f-3587-4c02-8349-9fe08cd9dbe9%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
<Despecified.png>

Bryan,

I have looked at that before. But the issue is that then I have to use the bokeh.plotting way of creating a stacked bar chart, and I’ve been searching all over for an example of that. I am having trouble finding even a basic bar chart using figure(), let alone a stacked one.

Maybe if you could point me to an example like that, then I could do the rest with the legend documentation.

Thanks,

Lya

···

On Mon, Jun 13, 2016 at 2:53 PM Bryan Van de Ven [email protected] wrote:

Lya,

As of the latest release, all of the configurable legend properties are described here:

    [http://bokeh.pydata.org/en/0.11.1/docs/user_guide/styling.html#location](http://bokeh.pydata.org/en/0.11.1/docs/user_guide/styling.html#location)

We are working towards a freeze for 0.12 which will have the ability to put legends outside the central plot region, as well as array legends horizontally (no support yet for multiple columns but you could split up and create two legends “by hand” if necessary). It’s possible you might be able to use one of the “dev builds” to get this capability earlier:

    [http://bokeh.pydata.org/en/0.11.1/docs/installation.html#developer-builds](http://bokeh.pydata.org/en/0.11.1/docs/installation.html#developer-builds)

But please know that the dev builds are not tested to the same standards are regular releases (but it may work perfectly well for your specific use case).

Bryan

On Jun 13, 2016, at 1:44 PM, Lya Batlle [email protected] wrote:

Hi,

I have a bar chart with numerous values and as a result, the legend is come out large enough to obscure the data. I’d love to tweak the legend to be smaller text, and maybe multiple columns, but I have no idea how to do it. I used the Bar code in my previous post. And the result is below (I messed with the palette).

bp = Bar(df_stations, values=‘count’, label=‘hod’, stack=‘stations’, tools=[“hover”, “crosshair”, “pan”,

         "box_zoom", "wheel_zoom", "reset", "resize", "save"], legend=True,
     plot_height=400, plot_width=700, color=PiYG11)

I’ve included the image, but I had to anonymize the data on the legend to share. The size of the font might be off because of it, but everything else is the exact captured image.

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/484f850f-3587-4c02-8349-9fe08cd9dbe9%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

<Despecified.png>

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/BD7F7E42-441B-4081-99D7-34979B752805%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Lya,

Sorry I am trying to do about ten things at once today, I focused on the legend question. With Bar, perhaps you could set the range to be larger, to accommodate a legend that does not over lap the plotted data? Alternatively, if you try a "dev build", you could add legends off the side of a plot (not in the central plotting area) like this example:

  https://github.com/bokeh/bokeh/blob/master/examples/models/legends.py#L56-L64

But again, that requires using a 0.12 pre-release.

Finally, it's a bit more pick with bokeh.plotting, but definitely possible. Here is an example of plotting a histogram:

  https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/histogram.py#L8-L27

Basically you have the option of using "rect" or "quad" glyphs to draw bars "by hand". There is a plan to also add "vbar" and "hbar" glyphs that would make this even simpler, but it's not been worked on yet.

Thanks,

Bryan

···

On Jun 13, 2016, at 2:06 PM, Lya Batlle-Rafferty <[email protected]> wrote:

Bryan,

   I have looked at that before. But the issue is that then I have to use the bokeh.plotting way of creating a stacked bar chart, and I've been searching all over for an example of that. I am having trouble finding even a basic bar chart using figure(), let alone a stacked one.

   Maybe if you could point me to an example like that, then I could do the rest with the legend documentation.

Thanks,
Lya

On Mon, Jun 13, 2016 at 2:53 PM Bryan Van de Ven <[email protected]> wrote:
Lya,

As of the latest release, all of the configurable legend properties are described here:

        http://bokeh.pydata.org/en/0.11.1/docs/user_guide/styling.html#location

We are working towards a freeze for 0.12 which will have the ability to put legends outside the central plot region, as well as array legends horizontally (no support yet for multiple columns but you could split up and create two legends "by hand" if necessary). It's possible you might be able to use one of the "dev builds" to get this capability earlier:

        http://bokeh.pydata.org/en/0.11.1/docs/installation.html#developer-builds

But please know that the dev builds are not tested to the same standards are regular releases (but it may work perfectly well for your specific use case).

Bryan

> On Jun 13, 2016, at 1:44 PM, Lya Batlle <[email protected]> wrote:
>
> Hi,
>
> I have a bar chart with numerous values and as a result, the legend is come out large enough to obscure the data. I'd love to tweak the legend to be smaller text, and maybe multiple columns, but I have no idea how to do it. I used the Bar code in my previous post. And the result is below (I messed with the palette).
>
> bp = Bar(df_stations, values='count', label='hod', stack='stations', tools=["hover", "crosshair", "pan",
> "box_zoom", "wheel_zoom", "reset", "resize", "save"], legend=True,
> plot_height=400, plot_width=700, color=PiYG11)
>
> I've included the image, but I had to anonymize the data on the legend to share. The size of the font might be off because of it, but everything else is the exact captured image.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/484f850f-3587-4c02-8349-9fe08cd9dbe9%40continuum.io.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout.
> <Despecified.png>

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/BD7F7E42-441B-4081-99D7-34979B752805%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/CAPLE76MZUOatSDcftQfQCzZ0T0M-evmoYjoU2%3D1jdUo2O2-ffQ%40mail.gmail.com.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Bryan,

This is how I created a stacked bar chart with the plotting library. I’m hoping the example helps since the few that I saw online were either very simplistic or didn’t really work. BTW: Why does the y have to be scaled by 1/2? It was in every example.

station_data = df_stations.groupby('stations').agg({'count': lambda x: x.tolist()})['count'].to_dict()
station_data['X'] = np.unique(df_stations['hod'].values)

bp = figure(title="Stuff", plot_height=400, plot_width=700, tools=["hover",
                                                                                                     "crosshair",
                                                                                                     "pan",
                                                                                                     "box_zoom",
                                                                                                     "wheel_zoom",
                                                                                                     "reset",
                                                                                                     "resize",
                                                                                                     "save"],
            y_range=[0, max * 5], x_range=[0,23])
bar_height = []
col_count = 0
for key, value in station_data.iteritems():
    if key != 'X':
        # For height, we have to make the y the total values of all the values.
        bar_height.append(value)
        cur_height = np.sum(bar_height, axis=0, dtype=np.int)
        bp.rect(x=station_data['X'], y=cur_height/2, legend=key, width=.8, height=value, color=BrBG11[col_count], alpha=.6)
        if col_count <= 9:
            col_count += 1
        else:
            col_count = 1
bp.legend.location = "top_right"
bp.legend.label_text_font_size = '8pt'
bp.legend.glyph_width = 9
bp.legend.glyph_height = 5
bp.legend.label_standoff= 10
bp.legend.label_height=10
···

On Monday, June 13, 2016 at 3:19:32 PM UTC-4, Bryan Van de ven wrote:

Lya,

Sorry I am trying to do about ten things at once today, I focused on the legend question. With Bar, perhaps you could set the range to be larger, to accommodate a legend that does not over lap the plotted data? Alternatively, if you try a “dev build”, you could add legends off the side of a plot (not in the central plotting area) like this example:

    [https://github.com/bokeh/bokeh/blob/master/examples/models/legends.py#L56-L64](https://github.com/bokeh/bokeh/blob/master/examples/models/legends.py#L56-L64)

But again, that requires using a 0.12 pre-release.

Finally, it’s a bit more pick with bokeh.plotting, but definitely possible. Here is an example of plotting a histogram:

    [https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/histogram.py#L8-L27](https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/histogram.py#L8-L27)

Basically you have the option of using “rect” or “quad” glyphs to draw bars “by hand”. There is a plan to also add “vbar” and “hbar” glyphs that would make this even simpler, but it’s not been worked on yet.

Thanks,

Bryan

On Jun 13, 2016, at 2:06 PM, Lya Batlle-Rafferty [email protected] wrote:

Bryan,

I have looked at that before. But the issue is that then I have to use the bokeh.plotting way of creating a stacked bar chart, and I’ve been searching all over for an example of that. I am having trouble finding even a basic bar chart using figure(), let alone a stacked one.

Maybe if you could point me to an example like that, then I could do the rest with the legend documentation.

Thanks,

Lya

On Mon, Jun 13, 2016 at 2:53 PM Bryan Van de Ven [email protected] wrote:

Lya,

As of the latest release, all of the configurable legend properties are described here:

    [http://bokeh.pydata.org/en/0.11.1/docs/user_guide/styling.html#location](http://bokeh.pydata.org/en/0.11.1/docs/user_guide/styling.html#location)

We are working towards a freeze for 0.12 which will have the ability to put legends outside the central plot region, as well as array legends horizontally (no support yet for multiple columns but you could split up and create two legends “by hand” if necessary). It’s possible you might be able to use one of the “dev builds” to get this capability earlier:

    [http://bokeh.pydata.org/en/0.11.1/docs/installation.html#developer-builds](http://bokeh.pydata.org/en/0.11.1/docs/installation.html#developer-builds)

But please know that the dev builds are not tested to the same standards are regular releases (but it may work perfectly well for your specific use case).

Bryan

On Jun 13, 2016, at 1:44 PM, Lya Batlle [email protected] wrote:

Hi,

I have a bar chart with numerous values and as a result, the legend is come out large enough to obscure the data. I’d love to tweak the legend to be smaller text, and maybe multiple columns, but I have no idea how to do it. I used the Bar code in my previous post. And the result is below (I messed with the palette).

bp = Bar(df_stations, values=‘count’, label=‘hod’, stack=‘stations’, tools=[“hover”, “crosshair”, “pan”,

         "box_zoom", "wheel_zoom", "reset", "resize", "save"], legend=True,
     plot_height=400, plot_width=700, color=PiYG11)

I’ve included the image, but I had to anonymize the data on the legend to share. The size of the font might be off because of it, but everything else is the exact captured image.

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/484f850f-3587-4c02-8349-9fe08cd9dbe9%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

<Despecified.png>

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/BD7F7E42-441B-4081-99D7-34979B752805%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/CAPLE76MZUOatSDcftQfQCzZ0T0M-evmoYjoU2%3D1jdUo2O2-ffQ%40mail.gmail.com.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Hello Lya,

If you use “rect”, you should specify the center of the rectangles:

http://bokeh.pydata.org/en/0.11.1/docs/reference/plotting.html#bokeh.plotting.figure.Figure.rect

(That’s why you use the total height /2)

Have you tried using a grid plot?

I am thinking of using one “plot” for the legend (for example a line with no data) and the second to show the actual plot.

Best!

FYI in upcoming 0.12 legends can be positioned out of the central plot area (like an axis, without needing a grid plot)

Bryan

···

On Jun 15, 2016, at 10:46 AM, Oscar Delgado <[email protected]> wrote:

Hello Lya,

If you use "rect", you should specify the center of the rectangles:

http://bokeh.pydata.org/en/0.11.1/docs/reference/plotting.html#bokeh.plotting.figure.Figure.rect
(That's why you use the total height /2)

Have you tried using a grid plot?
I am thinking of using one "plot" for the legend (for example a line with no data) and the second to show the actual plot.

Best!

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/ac86e03c-924e-489a-92c6-4f9d9b6efad2%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Soo…the value/2 on the y axis was not working. I was still getting spacing and overlap issues.

We ended up (and by we I mean an awesome coworker brainstormed this on the board with me) with: y = previous_y + .5(height_previous_bar) + .5(height_of_current_bar)

Below is what actually worked:

prev_y=[]
prev_value=[]
#bar_height[:] = [0 for x in range(24)]
col_count = 0
prev_y[:] = [0 for x in range(24)]
prev_value[:] = [0 for x in range(24)]
for key, value in station_data.iteritems():
    if key != 'X':
        middle=[x + (y/2) for x,y in zip(prev_y, prev_value)]
        prev_y=[x + (y/2) for x,y in zip(middle, value)]
        bp.rect(x=station_data['X'], y=prev_y, legend=key, width=.8, height=value, color=bpalette[col_count], alpha=.8)
        if col_count <= 19:
            col_count += 1
        else:
            col_count = 1
        prev_value = value

And Bryan, that is awesome news.:slight_smile:

Oscar, I might end up having to go with that if I have too many values. I made the legend a lot smaller on my current plot, so it might not come to that.

Regards,

Lya

···

On Wednesday, June 15, 2016 at 12:06:00 PM UTC-4, Bryan Van de ven wrote:

FYI in upcoming 0.12 legends can be positioned out of the central plot area (like an axis, without needing a grid plot)

Bryan

On Jun 15, 2016, at 10:46 AM, Oscar Delgado [email protected] wrote:

Hello Lya,

If you use “rect”, you should specify the center of the rectangles:

http://bokeh.pydata.org/en/0.11.1/docs/reference/plotting.html#bokeh.plotting.figure.Figure.rect

(That’s why you use the total height /2)

Have you tried using a grid plot?

I am thinking of using one “plot” for the legend (for example a line with no data) and the second to show the actual plot.

Best!


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/ac86e03c-924e-489a-92c6-4f9d9b6efad2%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.