PBL
PBL

Reputation: 115

When plotting histograms using the python package bokeh, how can I control the colors used in the plot?

Using the code:

from bokeh.charts import Histogram, show, output_notebook

p = Histogram(df, values='score', color = 'month',
          title="Histograms for two different months",
          legend='top_right', bins=10)
show(p)

I provide a pandas dataframe (df) with a column called score and a column called month. Histograms are created using score grouped by month by assigning to the color parameter, color = 'month'.

This code succeeds in plotting the two histograms, but assigns default colors of red and green to them. How can I override the default coloring scheme, given that I have already assigned month to the color parameter?

Upvotes: 1

Views: 2820

Answers (1)

user3412205
user3412205

Reputation: 1455

In the current version (0.11.1) you can pass palette=['color1', 'color2', ...] to Histogram to assign colors.

I would assume you need at least as many colors in the palette as you have levels of your color column (in your case it sounds like two), otherwise things might repeat? (Haven't tested it).

From the docs, colors may be specified as:

  • any of the 147 named CSS colors, e.g 'green', 'indigo'
  • an RGB(A) hex value, e.g., '#FF0000', '#44444444'
  • a 3-tuple of integers (r,g,b) between 0 and 255
  • a 4-tuple of (r,g,b,a) where r, g, b are integers between 0 and 255 and a is a floating point value between 0 and 1

For your specific example, say we wanted the two colors to be blue and orange instead...

from bokeh.charts import Histogram, show, output_notebook

p = Histogram(df, values='score', color = 'month',
      title="Histograms for two different months",
      legend='top_right', bins=10, palette=['blue', 'orange'])
show(p)

Upvotes: 1

Related Questions