Python Drawing - Bokeh histogram small scale (Stacked Bar)

background

In the Preliminary Bokeh after learning to use it to make a map

aims

Make a histogram, y supporting a plurality of data sources, that is a histogram of the effect of stacking stacked bar

achieve

Single simple histogram data source

参考 Handling Categorical Data — Bokeh 1.4.0 documentation

from bokeh.io import show, output_file
from bokeh.plotting import figure

output_file("bars.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]

p = figure(x_range=fruits, plot_height=250, title="Fruit Counts", toolbar_location=None, tools="")

p.vbar(x=fruits, top=counts, width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0

show(p)

See the above-referenced renderings

Y increases a data source, the effect of stacking do

In this case, we need to consider:

  • Source: not a single list, you have to be able to accommodate a plurality of sets of data. Dictionary.
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 4, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}
  • Color: distinguish between different data sources

colors = ["green", "#718dbf", "#e84d60","#e84d20","#e84361"]

Color is a problem, believe it will be ugly, it will be mentioned later with a palette palette

  • Paint: The above vbardoes not support stacking
p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data,legend_label=years)

Export to file

Exporting Plots — Bokeh 1.4.0 documentation

  • html

output_file("file.html")

  • png

  • npm install selenium phantomjs
  • npm install -g phantomjs-prebuilt
  • pip install bokeh

then from bokeh.io import export_png

Source: reading data from a file .csv

I tried two ways, now with the second pandas

  • The numpy genfromtxt

But I encountered a lot of problems, including different dtype parameters, names and other parameters, to return different data types of array, feel very convenient (such as sorting, etc.), so then abandoned, of course, because I am not familiar with.

from numpy import genfromtxt
    my_data = genfromtxt("data.csv", delimiter=',', dtype=None, encoding="utf8")
  • pandas

This is easy to read the file:

df = pd.read_csv("data.csv",header=0)

Torimae Line 7 :df = df.head(n=7)

Take a column :df['col1']

Columns summation :df['col1'] + df['col2'] + df['col3']

Sort by :df = df.sort_values(by='col1', ascending=False)

x axis of rotation

Styling Visual Attributes — Bokeh 1.4.0 documentation

For example, the left oblique rotated 45 degrees:

    p.xaxis.major_label_orientation = 360-45

Palette

Earlier we use colors = ["green", "#718dbf", "#e84d60","#e84d20","#e84361"]artificial color, will be ugly unprofessional, bokeh has built-in color palettes, is actually very convenient, but also good-looking.

>>> from bokeh.palettes import brewer
>>> colors = brewer["Blues"][6]
>>> colors
['#08519c', '#3182bd', '#6baed6', '#9ecae1', '#c6dbef', '#eff3ff']

Specific reference list:

Category Data Processing

If only the digital data such as x [1,2,3], the above demo p.figureenough to handle

However, if some of the x or y coordinate is classified as data ["apple","orange"], it is necessary to add x_range, or y_rangethe like

Such as

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
p = figure(x_range=fruits, ... )
p.vbar(x=x, top=y, legend_label="Temp.", width=0.9)

参考 Handling Categorical Data — Bokeh 1.4.0 documentation

References

Guess you like

Origin www.cnblogs.com/learnbydoing/p/12441004.html