Here are the examples of the python api bokeh.transform.dodge taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
10 Examples
3
View Source File : _message_graphs.py
License : MIT License
Project Creator : mnemocron
License : MIT License
Project Creator : mnemocron
def histogram_weekdays(filename, metrics):
bkh.reset_output()
bkh.output_file(filename, title=filename)
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
fig = bkh.figure(x_range=weekdays,
title='Message distribution over weekdays',
width=720, height=480)
fig.vbar(x=dodge('index', 0.35, range=fig.x_range),
top='frequency', width=0.3, source=metrics['A']['frame_weekdays'],
color=colors[0], legend=metrics['A']['name'])
fig.vbar(x=dodge('index', 0.65, range=fig.x_range),
top='frequency', width=0.3, source=metrics['B']['frame_weekdays'],
color=colors[1], legend=metrics['B']['name'])
fig.xaxis.axis_label = 'Weekday'
fig.yaxis.axis_label = 'Message count'
bkh.show(fig)
return
'''
3
View Source File : _message_graphs.py
License : MIT License
Project Creator : mnemocron
License : MIT License
Project Creator : mnemocron
def histogram_hourofday(filename, metrics, key, title_str, ylabel):
bkh.reset_output()
bkh.output_file(filename, title=filename)
hours = ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
fig = bkh.figure(x_range=hours,
title=title_str,
width=1280, height=480)
fig.vbar(x=dodge('index', 0.35, range=fig.x_range),
top='frequency', width=0.3, source=metrics['A'][key],
color=colors[0], legend=metrics['A']['name'])
fig.vbar(x=dodge('index', 0.65, range=fig.x_range),
top='frequency', width=0.3, source=metrics['B'][key],
color=colors[1], legend=metrics['B']['name'])
fig.xaxis.axis_label = 'Time'
fig.yaxis.axis_label = ylabel
bkh.show(fig)
return
3
View Source File : test_transform.py
License : MIT License
Project Creator : rthorst
License : MIT License
Project Creator : rthorst
def test_basic(self):
t = bt.dodge("foo", 0.5)
assert isinstance(t, dict)
assert set(t) == {"field", "transform"}
assert t['field'] == "foo"
assert isinstance(t['transform'], Dodge)
assert t['transform'].value == 0.5
assert t['transform'].range is None
def test_with_range(self):
3
View Source File : test_transform.py
License : MIT License
Project Creator : rthorst
License : MIT License
Project Creator : rthorst
def test_with_range(self):
r = FactorRange("a")
t = bt.dodge("foo", 0.5, range=r)
assert isinstance(t, dict)
assert set(t) == {"field", "transform"}
assert t['field'] == "foo"
assert isinstance(t['transform'], Dodge)
assert t['transform'].value == 0.5
assert t['transform'].range is r
assert t['transform'].range.factors == ["a"]
class Test_factor_cmap(object):
0
View Source File : html_reporting.py
License : Apache License 2.0
Project Creator : allegroai
License : Apache License 2.0
Project Creator : allegroai
def report_html_periodic_table(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting interactive (html) of periodic table to debug samples section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
output_file("periodic.html")
periods = ["I", "II", "III", "IV", "V", "VI", "VII"]
groups = [str(x) for x in range(1, 19)]
autompg_clean = elements.copy()
autompg_clean["atomic mass"] = autompg_clean["atomic mass"].astype(str)
autompg_clean["group"] = autompg_clean["group"].astype(str)
autompg_clean["period"] = [periods[x - 1] for x in autompg_clean.period]
autompg_clean = autompg_clean[autompg_clean.group != "-"]
autompg_clean = autompg_clean[autompg_clean.symbol != "Lr"]
autompg_clean = autompg_clean[autompg_clean.symbol != "Lu"]
cmap = {
"alkali metal": "#a6cee3",
"alkaline earth metal": "#1f78b4",
"metal": "#d93b43",
"halogen": "#999d9a",
"metalloid": "#e08d49",
"noble gas": "#eaeaea",
"nonmetal": "#f1d4Af",
"transition metal": "#599d7A",
}
source = ColumnDataSource(autompg_clean)
p = figure(
plot_width=900,
plot_height=500,
title="Periodic Table (omitting LA and AC Series)",
x_range=groups,
y_range=list(reversed(periods)),
toolbar_location=None,
tools="hover",
)
p.rect(
"group",
"period",
0.95,
0.95,
source=source,
fill_alpha=0.6,
legend_label="metal",
color=factor_cmap(
"metal", palette=list(cmap.values()), factors=list(cmap.keys())
),
)
text_props = {"source": source, "text_align": "left", "text_baseline": "middle"}
x = dodge("group", -0.4, range=p.x_range)
r = p.text(x=x, y="period", text="symbol", **text_props)
r.glyph.text_font_style = "bold"
r = p.text(
x=x, y=dodge("period", 0.3, range=p.y_range), text="atomic number", **text_props
)
r.glyph.text_font_size = "8pt"
r = p.text(
x=x, y=dodge("period", -0.35, range=p.y_range), text="name", **text_props
)
r.glyph.text_font_size = "5pt"
r = p.text(
x=x, y=dodge("period", -0.2, range=p.y_range), text="atomic mass", **text_props
)
r.glyph.text_font_size = "5pt"
p.text(
x=["3", "3"],
y=["VI", "VII"],
text=["LA", "AC"],
text_align="center",
text_baseline="middle",
)
p.hover.tooltips = [
("Name", "@name"),
("Atomic number", "@{atomic number}"),
("Atomic mass", "@{atomic mass}"),
("Type", "@metal"),
("CPK color", "$color[hex, swatch]:CPK"),
("Electronic configuration", "@{electronic configuration}"),
]
p.outline_line_color = None
p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_standoff = 0
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
save(p)
logger.report_media("html", "periodic_html", iteration=iteration, local_path="periodic.html")
def report_html_groupby(logger, iteration=0):
0
View Source File : ui.py
License : Apache License 2.0
Project Creator : IntelLabs
License : Apache License 2.0
Project Creator : IntelLabs
def _create_plot() -> (Figure, ColumnDataSource):
"""Utility function for creating and styling the bar plot."""
global source, aspects, stats
pos_counts, neg_counts = (
[stats.loc[(asp, pol, False), "Quantity"] for asp in aspects] for pol in POLARITIES
)
np.seterr(divide="ignore")
source = ColumnDataSource(
data={
"aspects": aspects,
"POS": pos_counts,
"NEG": neg_counts,
"log-POS": np.log2(pos_counts),
"log-NEG": np.log2(neg_counts),
}
)
np.seterr(divide="warn")
p = figure(
plot_height=145,
sizing_mode="scale_width",
x_range=aspects,
toolbar_location="right",
tools="save, tap",
)
rs = [
p.vbar(
x=dodge("aspects", -0.207, range=p.x_range),
top="log-POS",
width=0.4,
source=source,
color="limegreen",
legend=value("POS"),
name="POS",
),
p.vbar(
x=dodge("aspects", 0.207, range=p.x_range),
top="log-NEG",
width=0.4,
source=source,
color="orangered",
legend=value("NEG"),
name="NEG",
),
]
for r in rs:
p.add_tools(
HoverTool(tooltips=[("Aspect", "@aspects"), (r.name, "@" + r.name)], renderers=[r])
)
p.add_layout(
Title(text=" " * 7 + "Sentiment Count (log scale)", align="left", text_font_size="23px"),
"left",
)
p.yaxis.ticker = []
p.y_range.start = 0
p.xgrid.grid_line_color = None
p.xaxis.major_label_text_font_size = "20pt"
p.legend.label_text_font_size = "20pt"
return p, source
def _update_events(events: DataTable, in_domain: bool) -> None:
0
View Source File : visualize.py
License : Apache License 2.0
Project Creator : IQTLabs
License : Apache License 2.0
Project Creator : IQTLabs
def visualize(
k_mers,
target_freqs,
optimized_freqs,
original_freqs=None,
title="Freqgen Optimization Results",
plot_height=400,
plot_width=1200,
show=True,
filepath="freqgen.html",
codons=False,
):
"""Creates a visualization of the results of a Freqgen optimization.
Note:
Currently, this function does not support the visualization of codon
optimization *and* *k*-mer optimization simultaneously.
Args:
k_mers (list): A list of the *k*-mers to use as the labels for the *x*-axis.
target_freqs (list): A list of the target frequencies in the same order as the `k_mers` argument.
optimized_freqs (list): A list of the resultant frequencies in the same order as the `k_mers` argument.
original_freqs (list, optional): A list of the original frequencies in the same order as the `k_mers` argument.
title (str, optional): A title to use for the graph. Defaults to "Freqgen Optimization Results".
plot_height (int, optional): The height for the graph. Defaults to 400.
plot_width (int, optional): The width for the graph. Defaults to 1200.
show (bool, optional): Whether to show the plot or simply return it. Defaults to True.
filepath (str, optional): The output filepath. Defaults to "freqgen.html".
codons (bool, optional): Whether codons are included in the input vectors. If they are, the *x*-axis legend will updated accordingly.
Note:
Codons must be denoted with a ``*`` in the ``k_mers`` argument. For example, the codon GAG should be passed as ``GAG*``
Returns:
bokeh.plotting.figure.Figure: A Bokeh figure containing the bar graph.
"""
# validate that all the codons that should be there are there
if codons and len([k_mer for k_mer in k_mers if k_mer.endswith("*")]) != 64:
raise ValueError("You appear to be passing an incomplete list of codons.")
codons_only = False
if all([k_mer.endswith("*") for k_mer in k_mers]) and codons:
k_mers = [k_mer[:-1] for k_mer in k_mers]
codons_only = True
output_file(filepath)
categories = ["Original", "Target", "Optimized"]
data = {"k_mers": k_mers, "Target": target_freqs, "Optimized": optimized_freqs}
# adjust spacing depending on if there's three bars or two
if not isinstance(original_freqs, type(None)):
offset = 0.25
data["Original"] = original_freqs
else:
offset = 0.15
# identify the greatest y value for setting bounds
y_max = max((max(target_freqs), max(optimized_freqs)))
if not isinstance(original_freqs, type(None)):
y_max = max((max(original_freqs), y_max))
source = ColumnDataSource(data=data)
p = figure(
x_range=k_mers,
plot_height=plot_height,
plot_width=plot_width,
title=title,
y_range=(0, 1.2 * y_max),
)
if not isinstance(original_freqs, type(None)):
p.vbar(
x=dodge("k_mers", -0.25, range=p.x_range),
top="Original",
width=0.2,
source=source,
color=Set2_3[0],
legend=value("Original"),
)
p.vbar(
x=dodge(
"k_mers",
0.0 if not isinstance(original_freqs, type(None)) else -offset,
range=p.x_range,
),
top="Target",
width=0.2,
source=source,
color=Set2_3[1],
legend=value("Target"),
)
p.vbar(
x=dodge("k_mers", offset, range=p.x_range),
top="Optimized",
width=0.2,
source=source,
color=Set2_3[2],
legend=value("Optimized"),
)
p.x_range.range_padding = 0.05
p.xgrid.grid_line_color = None
p.legend.location = "top_right"
p.legend.orientation = "horizontal"
p.legend.click_policy = "hide"
# decide the x-axis label
if codons_only:
p.xaxis.axis_label = "codon"
elif codons:
p.xaxis.axis_label = "k-mer (* denotes codon)"
else:
p.xaxis.axis_label = "k-mer"
if len(k_mers) >= 32:
p.xaxis.major_label_orientation = math.pi / 2
p.yaxis.axis_label = "frequency"
if show:
_show(p)
else:
save(p, filename=filepath)
return p
0
View Source File : plot.py
License : MIT License
Project Creator : PatrikHlobil
License : MIT License
Project Creator : PatrikHlobil
def plot( # noqa C901
df_in,
x=None,
y=None,
kind="line",
figsize=None,
use_index=True,
title="",
legend="top_right",
logx=False,
logy=False,
xlabel=None,
ylabel=None,
xticks=None,
yticks=None,
xlim=None,
ylim=None,
fontsize_title=None,
fontsize_label=None,
fontsize_ticks=None,
fontsize_legend=None,
color=None,
colormap=None,
category=None,
histogram_type=None,
stacked=False,
weights=None,
bins=None,
normed=False,
cumulative=False,
show_average=False,
plot_data_points=False,
plot_data_points_size=5,
number_format=None,
disable_scientific_axes=None,
show_figure=True,
return_html=False,
panning=True,
zooming=True,
sizing_mode="fixed",
toolbar_location="right",
hovertool=True,
hovertool_string=None,
rangetool=False,
vertical_xlabel=False,
x_axis_location="below",
webgl=True,
reuse_plot=None, # This keyword is not used by Pandas-Bokeh, but pandas plotting API adds it for series object calls
**kwargs,
):
"""Method for creating a interactive with 'Bokeh' as plotting backend. Available
plot kinds are:
* line
* point
* scatter
* bar / barh
* hist
* area
* pie
* map
Examples
--------
>>> df.plot_bokeh.line()
>>> df.plot_bokeh.scatter(x='x',y='y')
These plotting methods can also be accessed by calling the accessor as a
method with the ``kind`` argument (except of "map" plot):
``df.plot_bokeh(kind='line')`` is equivalent to ``df.plot_bokeh.line()``
For more information about the individual plot kind implementations, have a
look at the underlying method accessors (like df.plot_bokeh.line) or visit
https://github.com/PatrikHlobil/Pandas-Bokeh.
If `sizing_mode` is not fixed (default), it will overide the set plot width or height
depending on which axis it is scaled on.
"""
# Make a local copy of the DataFrame:
df = df_in.copy()
if isinstance(df, pd.Series):
df = pd.DataFrame(df)
if kind == "map":
return mapplot(
df,
x=x,
y=y,
figsize=figsize,
title=title,
legend=legend,
xlabel=xlabel,
ylabel=ylabel,
xlim=xlim,
color=color,
colormap=colormap,
category=category,
show_figure=show_figure,
return_html=return_html,
panning=panning,
zooming=zooming,
toolbar_location=toolbar_location,
hovertool=hovertool,
hovertool_string=hovertool_string,
webgl=webgl,
**kwargs,
)
# Check plot kind input:
allowed_kinds = [
"line",
"step",
"point",
"scatter",
"bar",
"barh",
"hist",
"area",
"pie",
"map",
]
rangetool_allowed_kinds = ["line", "step"]
if kind not in allowed_kinds:
allowed_kinds = "', '".join(allowed_kinds)
raise ValueError(f"Allowed plot kinds are '{allowed_kinds}'.")
if rangetool and kind not in rangetool_allowed_kinds:
allowed_rangetool_kinds = "', '".join(rangetool_allowed_kinds)
raise ValueError(
f"For using the rangetool, the allowed plot kinds are '{allowed_rangetool_kinds}'."
)
if rangetool:
x_axis_location = "above"
# Get and check options for base figure:
figure_options = {
"title": title,
"toolbar_location": toolbar_location,
"active_scroll": "wheel_zoom",
"plot_width": 600,
"plot_height": 400,
"output_backend": "webgl",
"sizing_mode": sizing_mode,
"x_axis_location": x_axis_location,
}
# Initializing rangetool plot variable:
p_rangetool = None
if figsize is not None:
width, height = figsize
figure_options["plot_width"] = width
figure_options["plot_height"] = height
if logx:
figure_options["x_axis_type"] = "log"
if logy:
figure_options["y_axis_type"] = "log"
if xlabel is not None:
figure_options["x_axis_label"] = xlabel
if ylabel is not None:
figure_options["y_axis_label"] = ylabel
if xlim is not None:
if not isinstance(xlim, (tuple, list)):
raise ValueError(" < xlim> must be a list/tuple of form (x_min, x_max).")
elif len(xlim) != 2:
raise ValueError(" < xlim> must be a list/tuple of form (x_min, x_max).")
else:
figure_options["x_range"] = xlim
if ylim is not None:
if not isinstance(ylim, (tuple, list)):
raise ValueError(" < ylim> must be a list/tuple of form (y_min, y_max).")
elif len(ylim) != 2:
raise ValueError(" < ylim> must be a list/tuple of form (y_min, y_max).")
else:
figure_options["y_range"] = ylim
if webgl:
figure_options["output_backend"] = "webgl"
if number_format is None:
number_format = ""
else:
number_format = "{%s}" % number_format
# Check hovertool_string and define additional columns to keep in source:
additional_columns = _extract_additional_columns(df, hovertool_string)
# Set standard linewidth:
if "line_width" not in kwargs:
kwargs["line_width"] = 2
# Get x-axis Name and Values:
delete_in_y = None
if x is not None:
if issubclass(x.__class__, pd.Index) or issubclass(x.__class__, pd.Series):
if x.name is not None:
name = str(x.name)
else:
name = ""
x = x.values
elif x in df.columns:
delete_in_y = x
name = str(x)
x = df[x].values
elif isinstance(x, (tuple, list, type(np.array))):
if len(x) == len(df):
x = x
name = ""
else:
raise Exception(
"Length of provided < x> argument does not fit length of DataFrame or Series."
)
else:
raise Exception(
"Please provide for the < x> parameter either a column name of the DataFrame/Series or an array of the same length."
)
else:
if use_index:
x = df.index.values
if df.index.name is not None:
name = str(df.index.name)
else:
name = ""
else:
x = np.linspace(0, len(df) - 1, len(df))
name = ""
# Define name of axis of x-values (for horizontal plots like barh, this corresponds
# to y-axis):
if kind == "barh":
if "y_axis_label" not in figure_options:
figure_options["y_axis_label"] = name
else:
if "x_axis_label" not in figure_options:
figure_options["x_axis_label"] = name
# Check type of x-axis:
if check_type(x) == "datetime":
figure_options["x_axis_type"] = "datetime"
xaxis_type = "datetime"
if xlim is not None:
starttime, endtime = xlim
try:
starttime = pd.to_datetime(starttime)
except ParserError:
raise ValueError("Could not parse x_min input of < xlim> as datetime.")
try:
endtime = pd.to_datetime(endtime)
except ParserError:
raise ValueError("Could not parse x_max input of < xlim> as datetime.")
figure_options["x_range"] = (starttime, endtime)
elif check_type(x) == "numeric":
xaxis_type = "numerical"
else:
xaxis_type = "categorical"
if kind in ["bar", "barh", "pie"]:
xaxis_type = "categorical"
x_old = x
x_labels_dict = None
if xaxis_type == "categorical":
if check_type(x) == "datetime":
x = _times_to_string(x)
else:
x = [str(el) for el in x]
if kind != "hist":
x_labels_dict = dict(zip(range(len(x)), x))
x = list(range(len(x)))
if "x_axis_type" in figure_options:
del figure_options["x_axis_type"]
# Determine data cols to plot (only plot numeric data):
data_cols = _determine_data_columns(y, df)
# Convert y-column names into string representation:
df.rename(columns={col: str(col) for col in data_cols}, inplace=True)
data_cols = [str(col) for col in data_cols]
# Delete x column if it appears in y columns:
if delete_in_y is not None:
if delete_in_y in data_cols:
data_cols.remove(delete_in_y)
N_cols = len(data_cols)
if len(data_cols) == 0:
raise Exception(
f"The only numeric column is the column {delete_in_y} that is already used on the x-axis."
)
# Autodetect y-label if no y-label is provided by user and only one y-column exists:
if N_cols == 1:
if kind == "barh":
if "x_axis_label" not in figure_options:
figure_options["x_axis_label"] = data_cols[0]
else:
if "y_axis_label" not in figure_options:
figure_options["y_axis_label"] = data_cols[0]
# Get Name of x-axis data:
if kind == "barh":
xlabelname = (
figure_options["y_axis_label"]
if figure_options.get("y_axis_label", "") != ""
else "x"
)
else:
xlabelname = (
figure_options["x_axis_label"]
if figure_options.get("x_axis_label", "") != ""
else "x"
)
# Create Figure for plotting:
p = figure(**figure_options)
if "x_axis_type" not in figure_options:
figure_options["x_axis_type"] = None
# For categorical plots, set the xticks:
if x_labels_dict is not None:
p.xaxis.formatter = FuncTickFormatter(
code="""
var labels = %s;
return labels[tick];
"""
% x_labels_dict
)
# Define ColumnDataSource for Plot if kind != "hist":
if kind != "hist":
source = {col: df[col].values for col in data_cols}
source["__x__values"] = x
source["__x__values_original"] = x_old
for kwarg, value in kwargs.items():
if value in df.columns:
source[value] = df[value].values
for add_col in additional_columns:
source[add_col] = df[add_col].values
# Define colormap
if kind not in ["scatter", "pie"]:
colormap = get_colormap(colormap, N_cols)
if color is not None:
colormap = get_colormap([color], N_cols)
# Add Glyphs to Plot:
if kind == "line":
p, p_rangetool = lineplot(
p,
source,
data_cols,
colormap,
hovertool,
xlabelname,
figure_options["x_axis_type"],
plot_data_points,
plot_data_points_size,
hovertool_string,
number_format,
rangetool,
**kwargs,
)
if kind == "step":
p, p_rangetool = stepplot(
p,
source,
data_cols,
colormap,
hovertool,
xlabelname,
figure_options["x_axis_type"],
plot_data_points,
plot_data_points_size,
hovertool_string,
number_format,
rangetool,
**kwargs,
)
if kind == "point":
p = pointplot(
p,
source,
data_cols,
colormap,
hovertool,
hovertool_string,
xlabelname,
figure_options["x_axis_type"],
number_format,
**kwargs,
)
if kind == "scatter":
if N_cols > 2:
raise Exception(
"For scatterplots < x> and < y> values can only be a single column of the DataFrame, not a list of columns. Please specify both < x> and < y> columns for a scatterplot uniquely."
)
# Get and set y-labelname:
y_column = data_cols[0]
if "y_axis_label" not in figure_options:
p.yaxis.axis_label = y_column
# Get values for y-axis:
y = df[y_column].values
# Delete additionally created values by pandas.plotting:
for add_param in ["s", "c"]:
if add_param in kwargs:
del kwargs[add_param]
# Get values for categorical colormap:
category_values = None
if category in df.columns:
category_values = df[category].values
elif category is not None:
raise Exception(
" < category> parameter has to be either None or the name of a single column of the DataFrame"
)
scatterplot(
p,
df,
x,
x_old,
y,
category,
category_values,
colormap,
hovertool,
hovertool_string,
additional_columns,
x_axis_type=figure_options["x_axis_type"],
xlabelname=xlabelname,
ylabelname=y_column,
**kwargs,
)
if kind == "bar" or kind == "barh":
# Define data source for barplot:
data = {col: df[col].values for col in data_cols}
data["__x__values"] = x
data["__x__values_original"] = x_old
source = ColumnDataSource(data)
for kwarg, value in kwargs.items():
if value in df.columns:
source.data[value] = df[value].values
for add_col in additional_columns:
source.data[add_col] = df[add_col].values
# Create Figure (just for categorical barplots):
del figure_options["x_axis_type"]
if "y_axis_label" not in figure_options and kind == "barh":
figure_options["y_axis_label"] = xlabelname
p = figure(**figure_options)
figure_options["x_axis_type"] = None
# Set xticks:
if kind == "bar":
p.xaxis.formatter = FuncTickFormatter(
code="""
var labels = %s;
return labels[tick];
"""
% x_labels_dict
)
elif kind == "barh":
p.yaxis.formatter = FuncTickFormatter(
code="""
var labels = %s;
return labels[tick];
"""
% x_labels_dict
)
if not stacked:
if N_cols >= 3:
base_width = 0.5
else:
base_width = 0.35
width = base_width / (N_cols - 0.5)
if N_cols == 1:
shifts = [0]
else:
delta_shift = base_width / (N_cols - 1)
shifts = [-base_width / 2 + i * delta_shift for i in range(N_cols)]
for i, name, color, shift in zip(
range(N_cols), data_cols, colormap, shifts
):
if kind == "bar":
glyph = p.vbar(
x=dodge("__x__values", shift, range=p.x_range),
top=name,
width=width,
source=source,
color=color,
legend_label=" " + name,
**kwargs,
)
hovermode = "vline"
elif kind == "barh":
glyph = p.hbar(
y=dodge("__x__values", shift, range=p.y_range),
right=name,
height=width,
source=source,
color=color,
legend_label=" " + name,
**kwargs,
)
hovermode = "hline"
if hovertool:
my_hover = HoverTool(mode=hovermode, renderers=[glyph])
if hovertool_string is None:
my_hover.tooltips = [
(xlabelname, "@__x__values_original"),
(name, "@{%s}" % name),
]
else:
my_hover.tooltips = hovertool_string
p.add_tools(my_hover)
if stacked:
if kind == "bar":
glyph = p.vbar_stack(
data_cols,
x="__x__values",
width=0.8,
source=source,
color=colormap,
legend_label=data_cols,
**kwargs,
)
hovermode = "vline"
elif kind == "barh":
glyph = p.hbar_stack(
data_cols,
y="__x__values",
height=0.8,
source=source,
color=colormap,
legend_label=data_cols,
**kwargs,
)
hovermode = "hline"
if hovertool:
my_hover = HoverTool(mode=hovermode, renderers=[glyph[-1]])
if hovertool_string is None:
my_hover.tooltips = [(xlabelname, "@__x__values_original")] + [
(col, "@{%s}" % col) for col in data_cols
]
else:
my_hover.tooltips = hovertool_string
p.add_tools(my_hover)
if kind == "hist":
# Disable line_color (for borders of histogram bins) per default:
if "line_color" not in kwargs:
kwargs["line_color"] = None
elif kwargs["line_color"] is True:
del kwargs["line_color"]
if "by" in kwargs and y is None:
y = kwargs["by"]
del kwargs["by"]
# Check for stacked keyword:
if stacked and histogram_type not in [None, "stacked"]:
warnings.warn(
f" < histogram_type> was set to '{histogram_type}', but was overriden by < stacked>=True parameter."
)
histogram_type = "stacked"
elif stacked and histogram_type is None:
histogram_type = "stacked"
# Set xlabel if only one y-column is given and user does not override this via
# xlabel parameter:
if len(data_cols) == 1 and xlabel is None:
p.xaxis.axis_label = data_cols[0]
# If Histogram should be plotted, calculate bins, aggregates and
# averages:
# Autocalculate bins if bins are not specified:
if bins is None:
values = df[data_cols].values
values = values[~np.isnan(values)]
data, bins = np.histogram(values)
# Calculate bins if number of bins is given:
elif isinstance(bins, int):
if bins < 1:
raise ValueError(
" < bins> can only be an integer>0, a list or a range of numbers."
)
values = df[data_cols].values
values = values[~np.isnan(values)]
v_min, v_max = values.min(), values.max()
bins = np.linspace(v_min, v_max, bins + 1)
if not isinstance(bins, str):
bins = list(bins)
if weights is not None:
if weights not in df.columns:
raise ValueError(
"Columns '%s' for < weights> is not in provided DataFrame."
)
else:
weights = df[weights].values
aggregates = []
averages = []
for col in data_cols:
values = df[col].values
if weights is not None:
not_nan = ~(np.isnan(values) | np.isnan(weights))
values_not_nan = values[not_nan]
weights_not_nan = weights[not_nan]
if sum(not_nan) < len(not_nan):
warnings.warn(
f"There are NaN values in column '{col}' or in the < weights> column. For the histogram, these rows have been neglected.",
Warning,
)
else:
not_nan = ~np.isnan(values)
values_not_nan = values[not_nan]
weights_not_nan = None
if sum(not_nan) < len(not_nan):
warnings.warn(
f"There are NaN values in column '{col}'. For the histogram, these rows have been neglected.",
Warning,
)
average = np.average(values_not_nan, weights=weights_not_nan)
averages.append(average)
data, bins = np.histogram(
values_not_nan, bins=bins, weights=weights_not_nan
)
if normed:
data = data / np.sum(data) * normed
if cumulative:
data = np.cumsum(data)
aggregates.append(data)
p = histogram(
p,
df,
data_cols,
colormap,
aggregates,
bins,
averages,
hovertool,
hovertool_string,
additional_columns,
normed,
cumulative,
show_average,
histogram_type,
logy,
**kwargs,
)
if kind == "area":
p = areaplot(
p,
source,
data_cols,
colormap,
hovertool,
hovertool_string,
xlabelname,
figure_options["x_axis_type"],
stacked,
normed,
**kwargs,
)
if kind == "pie":
source["__x__values"] = x_old
p = pieplot(
source,
data_cols,
colormap,
hovertool,
hovertool_string,
figure_options,
xlabelname,
**kwargs,
)
# Set xticks and yticks:
if xticks is not None:
p.xaxis[0].ticker = list(xticks)
elif x_labels_dict is not None and kind != "barh":
p.xaxis.ticker = x
elif kind == "barh":
p.yaxis.ticker = x
if yticks is not None:
p.yaxis.ticker = list(yticks)
# Format datetime ticks correctly:
if figure_options["x_axis_type"] == "datetime":
p.xaxis.formatter = DatetimeTickFormatter(
milliseconds=["%H:%M:%S.%f"],
seconds=["%H:%M:%S"],
minutes=["%H:%M:%S"],
hours=["%H:%M:%S"],
days=["%d %B %Y"],
months=["%d %B %Y"],
years=["%d %B %Y"],
)
# Rotate xlabel if wanted:
if vertical_xlabel:
p.xaxis.major_label_orientation = np.pi / 2
# Set panning option:
if panning is False:
p.toolbar.active_drag = None
# Set zooming option:
if zooming is False:
p.toolbar.active_scroll = None
# Set click policy for legend:
if not stacked and kind != "pie":
p.legend.click_policy = "hide"
# Hide legend if wanted:
if not legend:
p.legend.visible = False
# Modify legend position:
else:
if legend is True:
p.legend.location = "top_right"
elif legend in [
"top_left",
"top_center",
"top_right",
"center_left",
"center",
"center_right",
"bottom_left",
"bottom_center",
"bottom_right",
]:
p.legend.location = legend
else:
raise ValueError(
"Legend can only be True/False or one of 'top_left', 'top_center', 'top_right', 'center_left', 'center', 'center_right', 'bottom_left', 'bottom_center', 'bottom_right'"
)
# Set fontsizes:
set_fontsizes_of_figure(
figure=p,
fontsize_title=fontsize_title,
fontsize_label=fontsize_label,
fontsize_ticks=fontsize_ticks,
fontsize_legend=fontsize_legend,
)
# Scientific formatting for axes:
if disable_scientific_axes is None:
pass
elif disable_scientific_axes == "x":
p.xaxis[0].formatter.use_scientific = False
elif disable_scientific_axes == "y":
p.yaxis[0].formatter.use_scientific = False
elif disable_scientific_axes in ["xy", True]:
p.xaxis[0].formatter.use_scientific = False
p.yaxis[0].formatter.use_scientific = False
else:
raise ValueError(
"""Keyword parameter < disable_scientific_axes> only accepts "xy", True, "x", "y" or None."""
)
# If rangetool is used, add it to layout:
if p_rangetool is not None:
p = column(p, p_rangetool)
# Display plot if wanted
if show_figure:
show(p)
# Return as (embeddable) HTML if wanted:
if return_html:
return embedded_html(p)
# Return plot:
return p
def _determine_data_columns(
0
View Source File : render.py
License : MIT License
Project Creator : sfu-db
License : MIT License
Project Creator : sfu-db
def bar_viz(
df: List[pd.DataFrame],
ttl_grps: int,
nrows: List[int],
col: str,
yscale: str,
plot_width: int,
plot_height: int,
show_yticks: bool,
orig: List[str],
df_labels: List[str],
baseline: int,
) -> Figure:
"""
Render a bar chart
"""
# pylint: disable=too-many-arguments, too-many-locals
if len(df) > 1:
for i, _ in enumerate(df):
df[i] = df[i].reindex(index=df[baseline].index, fill_value=0).to_frame()
tooltips = [
(col, "@index"),
("Count", f"@{{{col}}}"),
("Percent", "@pct{0.2f}%"),
("Source", "@orig"),
]
if show_yticks:
if len(df[baseline]) > 10:
plot_width = 28 * len(df[baseline])
fig = Figure(
plot_width=plot_width,
plot_height=plot_height,
title=col,
toolbar_location=None,
tooltips=tooltips,
tools="hover",
x_range=list(df[baseline].index),
y_axis_type=yscale,
)
offset = np.linspace(-0.08 * len(df), 0.08 * len(df), len(df)) if len(df) > 1 else [0]
for i, (nrow, data) in enumerate(zip(nrows, df)):
data["pct"] = data[col] / nrow * 100
data.index = [str(val) for val in data.index]
data["orig"] = orig[i]
fig.vbar(
x=dodge("index", offset[i], range=fig.x_range),
width=0.6 / len(df),
top=col,
bottom=0.01,
source=data,
fill_color=CATEGORY10[i],
line_color=CATEGORY10[i],
)
tweak_figure(fig, "bar", show_yticks)
fig.yaxis.axis_label = "Count"
x_axis_label = ""
if ttl_grps > len(df[baseline]):
x_axis_label += f"Top {len(df[baseline])} of {ttl_grps} {col}"
if orig != df_labels:
if x_axis_label:
x_axis_label += f", this vairable is only in {','.join(orig)}"
else:
x_axis_label += f"This vairable is only in {','.join(orig)}"
fig.xaxis.axis_label = x_axis_label
fig.xaxis.axis_label_standoff = 0
if show_yticks and yscale == "linear":
_format_axis(fig, 0, df[baseline].max(), "y")
return fig
def hist_viz(
0
View Source File : bokeh_figures.py
License : MIT License
Project Creator : SolarArbiter
License : MIT License
Project Creator : SolarArbiter
def bar_subdivisions(cds, category, metric):
"""
Create bar graphs comparing a single metric across subdivisions of
time for multiple forecasts. e.g.::
Fx 1 MAE |
|_________________
Fx 2 MAE |
|_________________
Year, Month of the year, etc.
Parameters
----------
cds : bokeh.models.ColumnDataSource
Fields must be kind and the names of the forecasts
category : str
One of the available metrics grouping categories (e.g., total)
Returns
-------
figs : dict of figures
"""
palette = cycle(PALETTE)
tools = 'pan,xwheel_zoom,box_zoom,reset,save'
fig_kwargs = dict(tools=tools, toolbar_location='above')
figs = {}
width = 0.8
human_category = datamodel.ALLOWED_CATEGORIES[category]
metric_name = datamodel.ALLOWED_DETERMINISTIC_METRICS[metric]
fig_kwargs['x_axis_label'] = human_category
fig_kwargs['y_axis_label'] = metric_name
filter_ = ((np.asarray(cds.data['category']) == category) &
(np.asarray(cds.data['metric']) == metric))
# Special handling for x-axis with dates
if category == 'date':
fig_kwargs['x_axis_type'] = 'datetime'
width = width * pd.Timedelta(days=1)
fig_kwargs['x_range'] = DataRange1d()
elif category == 'month':
fig_kwargs['x_range'] = FactorRange(
factors=calendar.month_abbr[1:])
elif category == 'weekday':
fig_kwargs['x_range'] = FactorRange(
factors=calendar.day_abbr[0:])
elif category == 'hour':
fig_kwargs['x_range'] = FactorRange(
factors=[str(i) for i in range(25)])
else:
fig_kwargs['x_range'] = FactorRange(
factors=np.unique(cds.data['index'][filter_]))
y_data = np.asarray(cds.data['value'])[filter_]
if len(y_data) == 0:
start, end = None, None
else:
y_min = np.nanmin(y_data)
y_max = np.nanmax(y_data)
start, end = calc_y_start_end(y_min, y_max)
fig_kwargs['y_range'] = DataRange1d(start=start, end=end)
unique_names = np.unique(np.asarray(cds.data['name'])[filter_])
for name in unique_names:
view = CDSView(source=cds, filters=[
GroupFilter(column_name='metric', group=metric),
GroupFilter(column_name='category', group=category),
GroupFilter(column_name='name', group=name)
])
# Create figure
title = name + ' ' + metric_name
fig = figure(width=800, height=200, title=title,
name=f'{category}_{metric}_{name}',
**fig_kwargs)
# Custom bar alignment
if category == 'hour':
# Center bars between hour ticks
x = dodge('index', 0.5, range=fig.x_range)
else:
x = 'index'
fig.vbar(x=x, top='value', width=width, source=cds,
view=view,
line_color='white', fill_color=next(palette))
# axes parameters
fig.xgrid.grid_line_color = None
fig.xaxis.minor_tick_line_color = None
# Hover tool and format specific changes
if category == 'date':
# Datetime x-axis
formatter = DatetimeTickFormatter(days='%Y-%m-%d')
fig.xaxis.formatter = formatter
tooltips = [
('Forecast', '@name'),
(human_category, '@index{%F}'),
(metric_name, '@value'),
]
hover_kwargs = dict(tooltips=tooltips,
formatters={'index': 'datetime'})
elif category == 'month' or category == 'weekday':
# Categorical x-axis
formatter = CategoricalTickFormatter()
fig.xaxis.formatter = formatter
tooltips = [
('Forecast', '@name'),
(human_category, '@index'),
(metric_name, '@value'),
]
hover_kwargs = dict(tooltips=tooltips)
else:
# Numerical x-axis
tooltips = [
('Forecast', '@name'),
(human_category, '@index'),
(metric_name, '@value'),
]
hover_kwargs = dict(tooltips=tooltips)
hover = HoverTool(mode='vline', **hover_kwargs)
fig.add_tools(hover)
figs[name] = fig
return figs
def nested_bar():