I'm using matplotlib and PyQt5 in a GUI application. To plot my data I use the "FigureCanvasQTAgg" and add the "NavigationToolbar2QT" to be able to modify and save my plots. It works, but I was wondering if there are more advanved Toolbars that for example allow changing the font size of the titel and/or label? Here is what I use atm:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
self.figure = plt.figure()
self.ax = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas)
The available "Figure options" look like this:
Options I'm looking for are:
font size of the title
font size of axis-label
options for the legend like position, font size, style
Probably I'm not the first one looking for these options, so I guess that somebody coded such an advanced toolbar already, but I couldn't find anything and thought it's worth asking here before I try to code it on my own and (probably) waste a lot of time.
The figure options qt dialog is defined in
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/qt_editor/figureoptions.py
You may copy that code to a new file, say myfigureoptions.py and make the changes you want to. Then monkey-patch it into the original.
The following would add a title fontsize field.
# Copyright © 2009 Pierre Raybaut
# Licensed under the terms of the MIT License
# see the mpl licenses directory for a copy of the license
# Modified to add a title fontsize
"""Module that provides a GUI-based editor for matplotlib's figure options."""
import os.path
import re
import matplotlib
from matplotlib import cm, colors as mcolors, markers, image as mimage
import matplotlib.backends.qt_editor.formlayout as formlayout
from matplotlib.backends.qt_compat import QtGui
def get_icon(name):
basedir = os.path.join(matplotlib.rcParams['datapath'], 'images')
return QtGui.QIcon(os.path.join(basedir, name))
LINESTYLES = {'-': 'Solid',
'--': 'Dashed',
'-.': 'DashDot',
':': 'Dotted',
'None': 'None',
}
DRAWSTYLES = {
'default': 'Default',
'steps-pre': 'Steps (Pre)', 'steps': 'Steps (Pre)',
'steps-mid': 'Steps (Mid)',
'steps-post': 'Steps (Post)'}
MARKERS = markers.MarkerStyle.markers
def figure_edit(axes, parent=None):
"""Edit matplotlib figure options"""
sep = (None, None) # separator
# Get / General
# Cast to builtin floats as they have nicer reprs.
xmin, xmax = map(float, axes.get_xlim())
ymin, ymax = map(float, axes.get_ylim())
general = [('Title', axes.get_title()),
('Title Fontsize', axes.title.get_fontsize()), # <------------- HERE
sep,
(None, "<b>X-Axis</b>"),
('Left', xmin), ('Right', xmax),
('Label', axes.get_xlabel()),
('Scale', [axes.get_xscale(), 'linear', 'log', 'logit']),
sep,
(None, "<b>Y-Axis</b>"),
('Bottom', ymin), ('Top', ymax),
('Label', axes.get_ylabel()),
('Scale', [axes.get_yscale(), 'linear', 'log', 'logit']),
sep,
('(Re-)Generate automatic legend', False),
]
# Save the unit data
xconverter = axes.xaxis.converter
yconverter = axes.yaxis.converter
xunits = axes.xaxis.get_units()
yunits = axes.yaxis.get_units()
# Sorting for default labels (_lineXXX, _imageXXX).
def cmp_key(label):
match = re.match(r"(_line|_image)(\d+)", label)
if match:
return match.group(1), int(match.group(2))
else:
return label, 0
# Get / Curves
linedict = {}
for line in axes.get_lines():
label = line.get_label()
if label == '_nolegend_':
continue
linedict[label] = line
curves = []
def prepare_data(d, init):
"""Prepare entry for FormLayout.
`d` is a mapping of shorthands to style names (a single style may
have multiple shorthands, in particular the shorthands `None`,
`"None"`, `"none"` and `""` are synonyms); `init` is one shorthand
of the initial style.
This function returns an list suitable for initializing a
FormLayout combobox, namely `[initial_name, (shorthand,
style_name), (shorthand, style_name), ...]`.
"""
if init not in d:
d = {**d, init: str(init)}
# Drop duplicate shorthands from dict (by overwriting them during
# the dict comprehension).
name2short = {name: short for short, name in d.items()}
# Convert back to {shorthand: name}.
short2name = {short: name for name, short in name2short.items()}
# Find the kept shorthand for the style specified by init.
canonical_init = name2short[d[init]]
# Sort by representation and prepend the initial value.
return ([canonical_init] +
sorted(short2name.items(),
key=lambda short_and_name: short_and_name[1]))
curvelabels = sorted(linedict, key=cmp_key)
for label in curvelabels:
line = linedict[label]
color = mcolors.to_hex(
mcolors.to_rgba(line.get_color(), line.get_alpha()),
keep_alpha=True)
ec = mcolors.to_hex(
mcolors.to_rgba(line.get_markeredgecolor(), line.get_alpha()),
keep_alpha=True)
fc = mcolors.to_hex(
mcolors.to_rgba(line.get_markerfacecolor(), line.get_alpha()),
keep_alpha=True)
curvedata = [
('Label', label),
sep,
(None, '<b>Line</b>'),
('Line style', prepare_data(LINESTYLES, line.get_linestyle())),
('Draw style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
('Width', line.get_linewidth()),
('Color (RGBA)', color),
sep,
(None, '<b>Marker</b>'),
('Style', prepare_data(MARKERS, line.get_marker())),
('Size', line.get_markersize()),
('Face color (RGBA)', fc),
('Edge color (RGBA)', ec)]
curves.append([curvedata, label, ""])
# Is there a curve displayed?
has_curve = bool(curves)
# Get / Images
imagedict = {}
for image in axes.get_images():
label = image.get_label()
if label == '_nolegend_':
continue
imagedict[label] = image
imagelabels = sorted(imagedict, key=cmp_key)
images = []
cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())]
for label in imagelabels:
image = imagedict[label]
cmap = image.get_cmap()
if cmap not in cm.cmap_d.values():
cmaps = [(cmap, cmap.name)] + cmaps
low, high = image.get_clim()
imagedata = [
('Label', label),
('Colormap', [cmap.name] + cmaps),
('Min. value', low),
('Max. value', high),
('Interpolation',
[image.get_interpolation()]
+ [(name, name) for name in sorted(mimage.interpolations_names)])]
images.append([imagedata, label, ""])
# Is there an image displayed?
has_image = bool(images)
datalist = [(general, "Axes", "")]
if curves:
datalist.append((curves, "Curves", ""))
if images:
datalist.append((images, "Images", ""))
def apply_callback(data):
"""This function will be called to apply changes"""
orig_xlim = axes.get_xlim()
orig_ylim = axes.get_ylim()
general = data.pop(0)
curves = data.pop(0) if has_curve else []
images = data.pop(0) if has_image else []
if data:
raise ValueError("Unexpected field")
# Set / General
(title, titlefontsize, xmin, xmax, xlabel, xscale, # <------------- HERE
ymin, ymax, ylabel, yscale, generate_legend) = general
if axes.get_xscale() != xscale:
axes.set_xscale(xscale)
if axes.get_yscale() != yscale:
axes.set_yscale(yscale)
axes.set_title(title)
axes.title.set_fontsize(titlefontsize) # <------------- HERE
axes.set_xlim(xmin, xmax)
axes.set_xlabel(xlabel)
axes.set_ylim(ymin, ymax)
axes.set_ylabel(ylabel)
# Restore the unit data
axes.xaxis.converter = xconverter
axes.yaxis.converter = yconverter
axes.xaxis.set_units(xunits)
axes.yaxis.set_units(yunits)
axes.xaxis._update_axisinfo()
axes.yaxis._update_axisinfo()
# Set / Curves
for index, curve in enumerate(curves):
line = linedict[curvelabels[index]]
(label, linestyle, drawstyle, linewidth, color, marker, markersize,
markerfacecolor, markeredgecolor) = curve
line.set_label(label)
line.set_linestyle(linestyle)
line.set_drawstyle(drawstyle)
line.set_linewidth(linewidth)
rgba = mcolors.to_rgba(color)
line.set_alpha(None)
line.set_color(rgba)
if marker is not 'none':
line.set_marker(marker)
line.set_markersize(markersize)
line.set_markerfacecolor(markerfacecolor)
line.set_markeredgecolor(markeredgecolor)
# Set / Images
for index, image_settings in enumerate(images):
image = imagedict[imagelabels[index]]
label, cmap, low, high, interpolation = image_settings
image.set_label(label)
image.set_cmap(cm.get_cmap(cmap))
image.set_clim(*sorted([low, high]))
image.set_interpolation(interpolation)
# re-generate legend, if checkbox is checked
if generate_legend:
draggable = None
ncol = 1
if axes.legend_ is not None:
old_legend = axes.get_legend()
draggable = old_legend._draggable is not None
ncol = old_legend._ncol
new_legend = axes.legend(ncol=ncol)
if new_legend:
new_legend.set_draggable(draggable)
# Redraw
figure = axes.get_figure()
figure.canvas.draw()
if not (axes.get_xlim() == orig_xlim and axes.get_ylim() == orig_ylim):
figure.canvas.toolbar.push_current()
data = formlayout.fedit(datalist, title="Figure options", parent=parent,
icon=get_icon('qt4_editor_options.svg'),
apply=apply_callback)
if data is not None:
apply_callback(data)
# Monkey-patch original figureoptions
from matplotlib.backends.qt_editor import figureoptions # <------------- HERE
figureoptions.figure_edit = figure_edit
Use it as
import matplotlib.pyplot as plt
import myfigureoptions
fig, ax = plt.subplots()
ax.plot([1,2])
ax.set_title("My Title")
plt.show()
When clicking the figure options dialog you now have a title font size field.
Based on the answer of ImportanceOfBeingErnes I started to modify the matplotlib figure options in the way I need them for my app. I removed the left/right and up/down limits for the X-Axis and Y-Axis because I already got these options implemented in my GUI, but I added another tab for various legend options. This his how the figure options look now:
And here is the code of the current version (I had to access some private variables because I couldn't find the corresponding get-functions and also the variable naming might not be the best. Please feel free to revise my code):
# Copyright © 2009 Pierre Raybaut
# Licensed under the terms of the MIT License
# see the mpl licenses directory for a copy of the license
# Modified to add a title fontsize
"""Module that provides a GUI-based editor for matplotlib's figure options."""
import os.path
import re
import matplotlib
from matplotlib import cm, colors as mcolors, markers, image as mimage
import matplotlib.backends.qt_editor.formlayout as formlayout
from matplotlib.backends.qt_compat import QtGui
def get_icon(name):
basedir = os.path.join(matplotlib.rcParams['datapath'], 'images')
return QtGui.QIcon(os.path.join(basedir, name))
LINESTYLES = {'-': 'Solid',
'--': 'Dashed',
'-.': 'DashDot',
':': 'Dotted',
'None': 'None',
}
DRAWSTYLES = {
'default': 'Default',
'steps-pre': 'Steps (Pre)', 'steps': 'Steps (Pre)',
'steps-mid': 'Steps (Mid)',
'steps-post': 'Steps (Post)'}
MARKERS = markers.MarkerStyle.markers
def figure_edit(axes, parent=None):
"""Edit matplotlib figure options"""
sep = (None, None) # separator
# Get / General
# Cast to builtin floats as they have nicer reprs.
xmin, xmax = map(float, axes.get_xlim())
ymin, ymax = map(float, axes.get_ylim())
if 'labelsize' in axes.xaxis._major_tick_kw:
_ticksize = int(axes.xaxis._major_tick_kw['labelsize'])
else:
_ticksize = 15
general = [(None, "<b>Figure Title</b>"),
('Title', axes.get_title()),
('Font Size', int(axes.title.get_fontsize())),
sep,
(None, "<b>Axes settings</b>"),
('Label Size', int(axes.xaxis.label.get_fontsize())),
('Tick Size', _ticksize),
('Show grid', axes.xaxis._gridOnMajor),
sep,
(None, "<b>X-Axis</b>"),
('Label', axes.get_xlabel()),
('Scale', [axes.get_xscale(), 'linear', 'log', 'logit']),
sep,
(None, "<b>Y-Axis</b>"),
('Label', axes.get_ylabel()),
('Scale', [axes.get_yscale(), 'linear', 'log', 'logit'])
]
if axes.legend_ is not None:
old_legend = axes.get_legend()
_draggable = old_legend._draggable is not None
_ncol = old_legend._ncol
_fontsize = int(old_legend._fontsize)
_frameon = old_legend._drawFrame
_shadow = old_legend.shadow
_fancybox = type(old_legend.legendPatch.get_boxstyle()) == matplotlib.patches.BoxStyle.Round
_framealpha = old_legend.get_frame().get_alpha()
else:
_draggable = False
_ncol = 1
_fontsize = 15
_frameon = True
_shadow = True
_fancybox = True
_framealpha = 0.5
legend = [('Draggable', _draggable),
('columns', _ncol),
('Font Size', _fontsize),
('Frame', _frameon),
('Shadow', _shadow),
('FancyBox', _fancybox),
('Alpha', _framealpha)
]
# Save the unit data
xconverter = axes.xaxis.converter
yconverter = axes.yaxis.converter
xunits = axes.xaxis.get_units()
yunits = axes.yaxis.get_units()
# Sorting for default labels (_lineXXX, _imageXXX).
def cmp_key(label):
match = re.match(r"(_line|_image)(\d+)", label)
if match:
return match.group(1), int(match.group(2))
else:
return label, 0
# Get / Curves
linedict = {}
for line in axes.get_lines():
label = line.get_label()
if label == '_nolegend_':
continue
linedict[label] = line
curves = []
def prepare_data(d, init):
"""Prepare entry for FormLayout.
`d` is a mapping of shorthands to style names (a single style may
have multiple shorthands, in particular the shorthands `None`,
`"None"`, `"none"` and `""` are synonyms); `init` is one shorthand
of the initial style.
This function returns an list suitable for initializing a
FormLayout combobox, namely `[initial_name, (shorthand,
style_name), (shorthand, style_name), ...]`.
"""
if init not in d:
d = {**d, init: str(init)}
# Drop duplicate shorthands from dict (by overwriting them during
# the dict comprehension).
name2short = {name: short for short, name in d.items()}
# Convert back to {shorthand: name}.
short2name = {short: name for name, short in name2short.items()}
# Find the kept shorthand for the style specified by init.
canonical_init = name2short[d[init]]
# Sort by representation and prepend the initial value.
return ([canonical_init] +
sorted(short2name.items(),
key=lambda short_and_name: short_and_name[1]))
curvelabels = sorted(linedict, key=cmp_key)
for label in curvelabels:
line = linedict[label]
color = mcolors.to_hex(
mcolors.to_rgba(line.get_color(), line.get_alpha()),
keep_alpha=True)
ec = mcolors.to_hex(
mcolors.to_rgba(line.get_markeredgecolor(), line.get_alpha()),
keep_alpha=True)
fc = mcolors.to_hex(
mcolors.to_rgba(line.get_markerfacecolor(), line.get_alpha()),
keep_alpha=True)
curvedata = [
('Label', label),
sep,
(None, '<b>Line</b>'),
('Line style', prepare_data(LINESTYLES, line.get_linestyle())),
('Draw style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
('Width', line.get_linewidth()),
('Color (RGBA)', color),
sep,
(None, '<b>Marker</b>'),
('Style', prepare_data(MARKERS, line.get_marker())),
('Size', line.get_markersize()),
('Face color (RGBA)', fc),
('Edge color (RGBA)', ec)]
curves.append([curvedata, label, ""])
# Is there a curve displayed?
has_curve = bool(curves)
# Get / Images
imagedict = {}
for image in axes.get_images():
label = image.get_label()
if label == '_nolegend_':
continue
imagedict[label] = image
imagelabels = sorted(imagedict, key=cmp_key)
images = []
cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())]
for label in imagelabels:
image = imagedict[label]
cmap = image.get_cmap()
if cmap not in cm.cmap_d.values():
cmaps = [(cmap, cmap.name)] + cmaps
low, high = image.get_clim()
imagedata = [
('Label', label),
('Colormap', [cmap.name] + cmaps),
('Min. value', low),
('Max. value', high),
('Interpolation',
[image.get_interpolation()]
+ [(name, name) for name in sorted(mimage.interpolations_names)])]
images.append([imagedata, label, ""])
# Is there an image displayed?
has_image = bool(images)
datalist = [(general, "Axes", ""), (legend, "Legend", "")]
if curves:
datalist.append((curves, "Curves", ""))
if images:
datalist.append((images, "Images", ""))
def apply_callback(data):
"""This function will be called to apply changes"""
general = data.pop(0)
legend = data.pop(0)
curves = data.pop(0) if has_curve else []
images = data.pop(0) if has_image else []
if data:
raise ValueError("Unexpected field")
# Set / General
(title, titlesize, labelsize, ticksize, grid, xlabel, xscale,
ylabel, yscale) = general
if axes.get_xscale() != xscale:
axes.set_xscale(xscale)
if axes.get_yscale() != yscale:
axes.set_yscale(yscale)
axes.set_title(title)
axes.title.set_fontsize(titlesize)
axes.set_xlabel(xlabel)
axes.xaxis.label.set_size(labelsize)
axes.xaxis.set_tick_params(labelsize=ticksize)
axes.set_ylabel(ylabel)
axes.yaxis.label.set_size(labelsize)
axes.yaxis.set_tick_params(labelsize=ticksize)
axes.grid(grid)
# Restore the unit data
axes.xaxis.converter = xconverter
axes.yaxis.converter = yconverter
axes.xaxis.set_units(xunits)
axes.yaxis.set_units(yunits)
axes.xaxis._update_axisinfo()
axes.yaxis._update_axisinfo()
# Set / Legend
(leg_draggable, leg_ncol, leg_fontsize, leg_frameon, leg_shadow,
leg_fancybox, leg_framealpha, ) = legend
new_legend = axes.legend(ncol=leg_ncol,
fontsize=float(leg_fontsize),
frameon=leg_frameon,
shadow=leg_shadow,
framealpha=leg_framealpha,
fancybox=leg_fancybox)
new_legend.set_draggable(leg_draggable)
# Set / Curves
for index, curve in enumerate(curves):
line = linedict[curvelabels[index]]
(label, linestyle, drawstyle, linewidth, color, marker, markersize,
markerfacecolor, markeredgecolor) = curve
line.set_label(label)
line.set_linestyle(linestyle)
line.set_drawstyle(drawstyle)
line.set_linewidth(linewidth)
rgba = mcolors.to_rgba(color)
line.set_alpha(None)
line.set_color(rgba)
if marker is not 'none':
line.set_marker(marker)
line.set_markersize(markersize)
line.set_markerfacecolor(markerfacecolor)
line.set_markeredgecolor(markeredgecolor)
# Set / Images
for index, image_settings in enumerate(images):
image = imagedict[imagelabels[index]]
label, cmap, low, high, interpolation = image_settings
image.set_label(label)
image.set_cmap(cm.get_cmap(cmap))
image.set_clim(*sorted([low, high]))
image.set_interpolation(interpolation)
# Redraw
figure = axes.get_figure()
figure.canvas.draw()
data = formlayout.fedit(datalist, title="Figure options", parent=parent,
icon=get_icon('qt4_editor_options.svg'),
apply=apply_callback)
if data is not None:
apply_callback(data)
# Monkey-patch original figureoptions
from matplotlib.backends.qt_editor import figureoptions
figureoptions.figure_edit = figure_edit
Related
I want to keep the labels when you hover, but hide the labels from just appearing over the Sankey as text.
Here is my code:
labels = df_mapping['Name'].to_numpy().tolist() + labels
count_dict = {}
source = []
target = []
value = df_subset['Stuff'].to_numpy().tolist()
index = 0
for x in unique_broad:
count_dict[x] = len(df_mapping.loc[df_mapping['Stuff'] == x])
for key in count_dict:
for i in range(count_dict[key]):
source.append(index)
index += 1
for key in count_dict:
for i in range(count_dict[key]):
target.append(index)
index += 1
number_of_colors = len(source)
color_link = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])
for i in range(number_of_colors)]
link = dict(source=source, target=target, value=value, color=color_link)
node = dict(label=labels, pad=35, thickness=10)
data = go.Sankey(link=link, node=node)
fig = go.Figure(data)
fig.update_layout(
hovermode = 'x',
title="Sankey for Stuff",
font=dict(size=8, color='white'),
paper_bgcolor='#51504f'
)
return fig
You can make the labels invisible by setting the color of the labels to rgba(0,0,0,0). This ensures that the label will remain in the hovertemplate, but not show up on the nodes.
To do this you can pass textfont=dict(color="rgba(0,0,0,0)", size=1) to go.Sankey such as in the example you used from the Plotly sankey diagram documentation:
import plotly.graph_objects as go
import urllib.request, json
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
response = urllib.request.urlopen(url)
data = json.loads(response.read())
# override gray link colors with 'source' colors
opacity = 0.4
# change 'magenta' to its 'rgba' value to add opacity
data['data'][0]['node']['color'] = ['rgba(255,0,255, 0.8)' if color == "magenta" else color for color in data['data'][0]['node']['color']]
data['data'][0]['link']['color'] = [data['data'][0]['node']['color'][src].replace("0.8", str(opacity))
for src in data['data'][0]['link']['source']]
fig = go.Figure(data=[go.Sankey(
textfont=dict(color="rgba(0,0,0,0)", size=1),
valueformat = ".0f",
valuesuffix = "TWh",
# Define nodes
node = dict(
pad = 15,
thickness = 15,
line = dict(color = "black", width = 0.5),
label = data['data'][0]['node']['label'],
color = data['data'][0]['node']['color']
),
# Add links
link = dict(
source = data['data'][0]['link']['source'],
target = data['data'][0]['link']['target'],
value = data['data'][0]['link']['value'],
label = data['data'][0]['link']['label'],
color = data['data'][0]['link']['color']
))])
fig.update_layout(title_text="Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>",
font_size=10)
fig.show()
You get the following:
I'm having problems plotting groupings of countries on a world map using Bokeh in combination with the geopandas package. What I want to do is colour each country in a group with a certain colour on the map. The groupings are saved in the dataframe "dataset", which is merged with the geopandas geographical info, converted to json and fed to the mapping functions. (Code below.)
The interesting thing is that no error is generated, Bokeh even reports that "BokehJS 1.4.0 successfully loaded", but no chart is shown.
I am quite convinced that the problem is with my implementation of the CategoricalColorMapper. This is evident since if I change the color mapper to to linear color mapper, the code works perfectly.
This code does not work:
from bokeh.palettes import viridis
from bokeh.models import FactorRange
dataset = gdf.merge(dataset, left_on = 'country', right_on = 'location', how = 'left')
#gdf is geopandas geo info dataframe
#Read data to json
dataset_json = json.loads(dataset.to_json())
#Convert to str like object
dataset_json_data = json.dumps(dataset_json)
#Input GeoJSON source that contains features for plotting.
geosource = GeoJSONDataSource(geojson = dataset_json_data)
catValues=list(dataset["val"].dropna().unique().astype("str"))
palette=viridis(len(catValues))
print("Palette len:", len(palette))
print("Factors:", len(catValues))
print(dataset)
color_mapper = CategoricalColorMapper(palette = palette , factors=catValues)
#Create figure object.
p = figure(title = title_string, plot_height = 600 , plot_width = 950, toolbar_location = None)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
#Add patch renderer to figure.
p.patches('xs','ys', source = geosource, fill_color = {'field' :'val', 'transform' : color_mapper})
#Display figure inline in Jupyter Notebook.
output_notebook()
#Display figure.
show(p)
Calling the function prints the following, but non map is shown. The number of colors and categories seems fine to me?
Palette len: 118
Factors: 118
BokehJS 1.4.0 successfully loaded.
Replacing only the color mapper works perfectly. This code works:
def plot_map(dataset, title_string = ""):
dataset = gdf.merge(dataset, left_on = 'country', right_on = 'location', how = 'left')
#Read data to json
dataset_json = json.loads(dataset.to_json())
#Convert to str like object
dataset_json_data = json.dumps(dataset_json)
#Input GeoJSON source that contains features for plotting.
geosource = GeoJSONDataSource(geojson = dataset_json_data)
#Define a sequential multi-hue color palette.
palette = brewer['OrRd'][7]
#Reverse color order so that dark blue is highest obesity.
palette = palette[::-1]
#Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors.
color_mapper = LinearColorMapper(palette = palette, low = dataset.val.min(), high = dataset.val.max())
#Define custom tick labels for color bar.
#tick_labels = {'0': '0', '1': '1', '2':'2', '3':'3', '4':'4', '5':'5', '6':'6','7':'7'}
#Create color bar.
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=7,width = 500, height = 20,
border_line_color=None,location = (0,0), orientation = 'horizontal', major_label_overrides = tick_labels)
#Create figure object.
p = figure(title = title_string, plot_height = 600 , plot_width = 950, toolbar_location = None)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
#Add patch renderer to figure.
p.patches('xs','ys', source = geosource, fill_color = {'field' :'val', 'transform' : color_mapper},
line_color = 'black', line_width = 0.25, fill_alpha = 1)
#Specify figure layout.
p.add_layout(color_bar, 'below')
#Display figure inline in Jupyter Notebook.
output_notebook()
#Display figure.
show(p)
I'm using reportlab to generate reports. I can define the creation process in four steps: 1) get the data via API, 2) filter the data, 3) generate the graphics with matplotlib and 4) insert information in PDF with reportlab.
I found in this (thanks Patrick Maupin!) and in this (thanks Larry Meyn!) answer a flowable matplotlib for ReportLab. I made some changes and I copy below the part of the code that interests:
import os
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from reportlab.platypus import Paragraph, SimpleDocTemplate, Flowable
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen import canvas
from pdfrw import PdfReader, PdfDict
from pdfrw.buildxobj import pagexobj
from pdfrw.toreportlab import makerl
try:
from cStringIO import StringIO as BytesIO
except ImportError:
from io import BytesIO
styles = getSampleStyleSheet()
style = styles['Normal']
#Info if you want to run the script
div = ['may/18', 'jun/18', 'jul/18', 'aug/18', 'sep/18', 'oct/18', 'nov/18', \
'dec/18', 'jan/19', 'feb/19', 'mar/19', 'apr/19']
mes = [0, 15, 149, 0, 59, 0, 0, 101, 45, 25, 751.07, 5358.3]
acc = [0, 15, 164, 164, 223, 223, 223, 324, 369, 394, 1145.07, 6503.37]
class PdfImage(Flowable):
"""
Generates a reportlab image flowable for matplotlib figures. It is initialized
with either a matplotlib figure or a pointer to a list of pagexobj objects and
an index for the pagexobj to be used.
"""
def __init__(self, fig=None, width=200, height=200, cache=None, cacheindex=0):
self.img_width = width
self.img_height = height
if fig is None and cache is None:
raise ValueError("Either 'fig' or 'cache' must be provided")
if fig is not None:
imgdata = BytesIO()
fig.savefig(imgdata, format='pdf')
imgdata.seek(0)
page, = PdfReader(imgdata).pages
image = pagexobj(page)
self.img_data = image
else:
self.img_data = None
self.cache = cache
self.cacheindex = cacheindex
def wrap(self, width, height):
return self.img_width, self.img_height
def drawOn(self, canv, x, y, _sW=0):
if _sW > 0 and hasattr(self, 'hAlign'):
a = self.hAlign
if a in ('CENTER', 'CENTRE', TA_CENTER):
x += 0.5*_sW
elif a in ('RIGHT', TA_RIGHT):
x += _sW
elif a not in ('LEFT', TA_LEFT):
raise ValueError("Bad hAlign value " + str(a))
canv.saveState()
if self.img_data is not None:
img = self.img_data
else:
img = self.cache[self.cacheindex]
if isinstance(img, PdfDict):
xscale = self.img_width / img.BBox[2]
yscale = self.img_height / img.BBox[3]
canv.translate(x, y)
canv.scale(xscale, yscale)
canv.doForm(makerl(canv, img))
else:
canv.drawImage(img, x, y, self.img_width, self.img_height)
canv.restoreState()
class PdfImageCache(object):
"""
Saves matplotlib figures to a temporary multi-page PDF file using the 'savefig'
method. When closed the images are extracted and saved to the attribute 'cache'.
The temporary PDF file is then deleted. The 'savefig' returns a PdfImage object
with a pointer to the 'cache' list and an index for the figure. Use of this
cache reduces duplicated resources in the reportlab generated PDF file.
Use is similar to matplotlib's PdfPages object. When not used as a context
manager, the 'close()' method must be explictly called before the reportlab
document is built.
"""
def __init__(self):
self.pdftempfile = '_temporary_pdf_image_cache_.pdf'
self.pdf = PdfPages(self.pdftempfile)
self.cache = []
self.count = 0
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
def close(self, *args):
self.pdf.close()
pages = PdfReader(self.pdftempfile).pages
pages = [pagexobj(x) for x in pages]
self.cache.extend(pages)
os.remove(self.pdftempfile)
def savefig(self, fig, width=200, height=200):
self.pdf.savefig(fig)
index = self.count
self.count += 1
return PdfImage(width=width, height=height, cache=self.cache, cacheindex=index)
def make_report_cached_figs(outfn):
"""
Makes a dummy report with nfig matplotlib plots using PdfImageCache
to reduce PDF file size.
"""
doc = SimpleDocTemplate(outfn)
style = styles["Normal"]
story = []
with PdfImageCache() as pdfcache:
fig = plt.figure(figsize=(5, 5))
plt.bar(div, acc, width = 0.8, color = 'silver', label = 'Year')
plt.bar(div, mes, width = 0.4, color = 'k', label = 'Month', alpha = 0.5)
plt.legend()
plt.xticks(rotation=60)
plt.close()
img0 = pdfcache.savefig(fig, width=185, height=135)
img0.drawOn(doc, 0, 100)
story.append(img0)
doc.build(story)
make_report_cached_figs("Test.pdf")
My problem is in the drawOn method, more specifically in the first argument. In the above code I get the following error:
AttributeError: 'SimpleDocTemplate' object has no attribute 'saveState'
If I replace doc = SimpleDocTemplate(outfn) with doc = canvas.Canvas(outfn) (I know that if I'm going to use canvas I should make some other minor changes) I get another error:
img = self.cache[self.cacheindex]
IndexError: list index out of range
I have searched for information in the official documentation and in the source code but have not been successful. So, how do I set the position of the image? (using the drawOn method or not)
today I developed a simple class in python in order to get a plot style suitable for my purpose ...
here the class :
this is the base class in which I define the colors ... in function colors , my particular interest is for the colors cycle named 'soft'
from abc import ABCMeta, abstractmethod
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
from matplotlib.axes import Axes
import matplotlib.pylab as pylab
import matplotlib
from cycler import cycler
class PlotBase(metaclass=ABCMeta):
def __init__(self, title , filename : str = ' '):
self.title = title
self.filename = filename
#----------------------------------------------------------------------------------------------
def findLimits(self):
'''
found the vectors limits in order to set the figure limits x,y
'''
_minX = 10E20
_minY = 10E20
_maxX = 0.000
_maxY = 0.000
for i in range(0,len(self.variable)-1,3):
if _minX >= min(self.variable[i]):
_minX = min(self.variable[i])
if _maxX <= max(self.variable[i]):
_maxX = max(self.variable[i])
if _minY >= min(self.variable[i+1]):
_minY = min(self.variable[i+1])
if _maxY <= max(self.variable[i+1]):
_maxY = max(self.variable[i+1])
return [round(_minX,2), round(_maxX,2) , round(_minY,2) , round(_maxY,2)]
#------------------------------------------------------------------------------------------------
def save(self, filename : 'str -> name of the file with extension'):
plt.savefig(filename)
#------------------------------------------------------------------------------------------------
def colors(self, style : 'str (name of the pallette of line colors)'):
if style == 'vega':
style = "cycler('color', ['1F77B4', 'FF7F0E', '2CA02C', 'D62728', '9467BD', '8C564B', 'E377C2', '7F7F7F', 'BCBD22', '17BECF'] )"
elif style == 'gg':
style = "cycler('color', ['E24A33', '348ABD', '988ED5', '777777', 'FBC15E', '8EBA42', 'FFB5B8'])"
elif style == 'brewer':
style = "cycler('color', ['66C2A5', 'FC8D62', '8DA0CB', 'E78AC3', 'A6D854', 'FFD92F', 'E5C494', 'B3B3B3'] )"
elif style == 'soft1':
style = "cycler('color', ['8DA0CB', 'E78AC3', 'A6D854', 'FFD92F', 'E5C494', 'B3B3B3', '66C2A5', 'FC8D62'] )"
elif style == 'tthmod':
style = "cycler('color', ['30a2da', 'fc4f30', 'e5ae38', '6d904f', '8b8b8b'])"
elif style == 'grayscale':
style = "cycler('color', ['252525', '525252', '737373', '969696', 'BDBDBD', 'D9D9D9', 'F0F0F0', 'F0F0FF' ])"
elif style == 'grayscale2':
style = "cycler('color', ['525252', '969696', 'BDBDBD' ,'D9D9D9', 'F0F0F0', 'F0F0FF' ])"
return style
#------------------------------------------------------------------------------------------------
def linestyle(self , linestyle : str):
if linestyle == 'linetype1':
linestyle = "cycler('linestyle', ['-', '--', ':', '-.'])"
if linestyle == 'linetype2':
linestyle = "cycler('linestyle', ['-', ':', '-.', '--'])"
return linestyle
#------------------------------------------------------------------------------------------------
#abstractmethod
def plot(self,*args,**kwargs):
"""
Abstract method!
the derived class must implement its self method
"""
pass
then I define the usable callable class :
class TimesPlot(PlotBase):
'''
mathpazo style (LaTeX-class) plot suitable for:
- palatino fonts template / beamer
- classic thesis style
- mathpazo package
'''
def __init__(self,title,filename: str= ' '):
super().__init__(title,filename)
#------------------------------------------------------------------------------------------------------------
def plot(self, *args,**kwargs): #
self.variable = [*args]
if len(self.variable) % 3 != 0:
print('Error variable must be coupled (even number)')
raise AttributeError('you must give 2 array x,y followed by string label for each plot')
'''
plot method, define all the parameter for the plot
the rendering of the figure is setting to beh "light"
--> TODO : define parameter in order to set the size of figure/font/linewidth
'''
#plt.rc('text', usetex=True )
#plt.rcParams['text.latex.preamble']=[r"\usepackage{times}"]
#plt.rcParams['text.latex.preamble']=[r"\usepackage{mathpazo}"]
plt.rcParams['font.family'] = 'serif' #'serif'
#plt.rcParams['font.sans-serif'] = ''#'DejaVu Sans' #'Tahoma' #, , 'Lucida Grande', 'Verdana']
plt.rcParams['font.size'] = 14
#plt.rcParams['font.name'] = 'Helvetica'
plt.rcParams['font.style'] = 'italic'
#plt.rc('font',family='' ,size=16, weight='normal')
plt.rc_context({'axes.edgecolor':'#999999' }) # BOX colors
plt.rc_context({'axes.linewidth':'1' }) # BOX width
plt.rc_context({'axes.xmargin':'0' })
plt.rc_context({'axes.ymargin':'0' })
plt.rc_context({'axes.labelcolor':'#555555' })
plt.rc_context({'axes.edgecolor':'999999' })
plt.rc_context({'axes.axisbelow':'True' })
plt.rc_context({'xtick.color':'#555555' }) # doesn't affect the text
plt.rc_context({'ytick.color':'#555555' }) # doesn't affect the text
plt.rc_context({ 'axes.prop_cycle': self.colors('soft1')})
#plt.rc('lines', linewidth=3)
fig,ax = plt.subplots(1,figsize=(10,6))
plt.title(self.title,color='#555555',fontsize=18)
plt.xlabel('time [s]',fontsize=16)
plt.ylabel('y(t)',fontsize=16)
#plt.grid(linestyle='dotted')
plt.grid(linestyle='--')
#plt.figure(1)
#ax.edgecolor('gray')
for i in range(0,len(self.variable)-1,3):
plt.plot(self.variable[i],self.variable[i+1], linewidth=3, label= self.variable[i+2])
ax.set_xlim( self.findLimits()[0] , self.findLimits()[1] )
ax.set_ylim( self.findLimits()[2] , self.findLimits()[3] + 0.02 )
majorLocator = MultipleLocator(20)
majorFormatter = FormatStrFormatter('%f')
minorXLocator = MultipleLocator(0.05)
minorYLocator = MultipleLocator(0.05)
ax.xaxis.set_minor_locator(minorXLocator)
ax.yaxis.set_minor_locator(minorYLocator)
ax.yaxis.set_ticks_position('both')
ax.xaxis.set_ticks_position('both')
#axes.xmargin: 0
#axes.ymargin: 0
#plt.legend(fontsize=10)
#handles, labels = ax.get_legend_handles_labels()
#ax.legend(handles, labels)
#ax.legend(frameon=True, fontsize=12)
#text.set_color('gray')
#leg = plt.legend(framealpha = 0, loc = 'best')
#ax.legend(borderpad=1)
legend = leg = plt.legend(framealpha = 1, loc = 'best', fontsize=14,fancybox=False, borderpad =0.4)
leg.get_frame().set_edgecolor('#dddddd')
leg.get_frame().set_linewidth(1.2)
plt.setp(legend.get_texts(), color='#555555')
plt.tight_layout(0.5)
if self.filename != ' ':
super().save(self.filename)
plt.show()
The falls happens because in my university pc this class give me a plot using the soft1 scheme of colors ... at home with same version of python It use the default set of colors (corresponding to the set that I defined named 'vega') ... not only the line colors change ... also the box of the plot have different colors contrast ... could somebody help me ??
I call this class simply passing them 6 vector (in order to obtain 3 curve) as follow :
fig1 = makeplot.TimesPlot('Solution of Differential Equations','p1.pdf')
fig1.plot(fet,feu,'Explicit Euler',bet,beu,'Implicit Euler',x,y,'Analytical')
May you please help me to understand the reason why this happens ? I've tried in my home from archlinux and gentoo .... while in my desktop pc in which the class works correctly I use the last debian
I'd like to create a barplot in matplotlib:
fig, ax = plt.subplots()
oldbar = ax.bar(x=ind, height=y, width=width)
I'd then like to pickle this barplot to file (either the dictionary or the axes - I'm not sure which is correct):
pickle.dump(oldbar, file('oldbar.pkl', 'w'))
I'd then like to reload this file, and then plot the old bar onto alongside a new bar plot, so I can compare them on a single axes:
fig, ax = plt.subplots()
newbar = ax.bar(x=ind, height=y, width=width)
oldbar = pickle.load(file('oldbar.pkl'))
# I realise the line below doesn't work
ax.bar(oldbar)
plt.show()
Ideally, I'd then like to present them as below. Any suggestions of how I might go about this?
You would pickle the figure instead the artists in it.
import matplotlib.pyplot as plt
import numpy as np
import pickle
ind = np.linspace(1,5,5)
y = np.linspace(9,1,5)
width = 0.3
fig, ax = plt.subplots()
ax.bar(x=ind, height=y, width=width)
ax.set_xlabel("x label")
pickle.dump(fig, file('oldbar.pkl', 'w'))
plt.close("all")
ind2 = np.linspace(1,5,5)
y2 = np.linspace(8,2,5)
width2 = 0.3
fig2 = pickle.load(file('oldbar.pkl'))
ax2 = plt.gca()
ax2.bar(x=ind2+width, height=y2, width=width2, color="C1")
plt.show()
However pickling the data itself may make more sense here.
import matplotlib.pyplot as plt
import numpy as np
import pickle
ind = np.linspace(1,5,5)
y = np.linspace(9,1,5)
width = 0.3
dic = {"ind":ind, "y":y, "width":width}
pickle.dump(dic, file('olddata.pkl', 'w'))
### new data
ind2 = np.linspace(1,5,5)
y2 = np.linspace(8,2,5)
width2 = 0.3
olddic = pickle.load(file('olddata.pkl'))
fig, ax = plt.subplots()
ax.bar(x=olddic["ind"], height=olddic["y"], width=olddic["width"])
ax.bar(x=ind2+olddic["width"], height=y2, width=width2)
ax.set_xlabel("x label")
plt.show()
Maybe this will help:
import pickle as pkl
import matplotlib.pyplot as plt
import numpy as np
class Data_set(object):
def __init__(self, x=[], y=[], name='data', pklfile=None,
figure=None, axes=None):
"""
"""
if pklfile is None:
self.x = np.asarray(x)
self.y = np.asarray(y)
self.name = str(name)
else:
self.unpickle(pklfile)
self.fig = figure
self.ax = axes
self.bar = None
def plot(self, width=0, offset=0, figure=None, axes=None):
if self.fig is None:
if figure is None:
self.fig = plt.figure()
self.ax = self.fig.subplots(1, 1)
else:
self.fig = figure
if axes is None:
self.ax = self.fig.subplots(1, 1)
else:
self.ax = axes
# maybe there's no need to keep track of self.fig, .ax and .bar,
# but just in case...
if figure is not None:
fig_to_use = figure
if axes is not None:
ax_to_use = axes
else:
ax_to_use = fig_to_use.subplots(1, 1)
else:
fig_to_use = self.fig
ax_to_use = self.ax
if not width:
width = (self.x[1]-self.x[0]) / 2.
self.bar = ax_to_use.bar(x=self.x+offset, height=self.y, width=width)
return fig_to_use, ax_to_use, self.bar
def pickle(self, filename='', ext='.pkl'):
if filename == '':
filename = self.name
with open(filename+ext, 'w') as output_file:
pkl.dump((self.name, self.x, self.y), output_file)
def unpickle(self, filename='', ext='.pkl'):
if filename == '':
filename = self.name
with open(filename + ext, 'r') as input_file:
# the name should really come from the filename, but then the
# above would be confusing?
self.name, self.x, self.y = pkl.load(input_file)
class Data_set_manager(object):
def __init__(self, datasets={}):
self.datasets = datasets
def add_dataset(self, data_set):
self.datasets[data_set.name] = data_set
def add_dataset_from_file(self, filename, ext='.pkl'):
self.datasets[filename] = Data_set(name=filename)
self.datasets[filename].unpickle(filename=filename, ext=ext)
def compare(self, width=0, offset=0, *args):
self.fig = plt.figure()
self.ax = self.fig.subplots(1, 1)
if len(args) == 0:
args = self.datasets.keys()
args.sort()
n = len(args)
if n == 0:
return None, None
if width == 0:
min_dx = None
for dataset in self.datasets.values():
sorted_x = dataset.x.copy()
sorted_x.sort()
try:
new_min_dx = np.min(dataset.x[1:] - dataset.x[:-1])
except ValueError:
# zero-size array to reduction operation minimum which
# has no identity (empty array)
new_min_dx = None
if new_min_dx < min_dx or min_dx is None:
min_dx = new_min_dx
if min_dx is None:
min_dx = 1.
width = float(min_dx) / (n + 1)
offset = float(min_dx) / (n + 1)
offsets = offset*np.arange(n)
if n % 2 == 0:
offsets -= offsets[n/2] - offset/2.
else:
offsets -= offsets[n/2]
i = 0
for name in args:
self.datasets.get(name, Data_set()).plot(width=width,
offset=offsets[i],
figure=self.fig,
axes=self.ax)
i += 1
self.ax.legend(args)
return self.fig, self.ax
if __name__ == "__main__":
# test saving/loading
name = 'test'
to_pickle = Data_set(x=np.arange(10),
y=np.random.rand(10),
name=name)
to_pickle.pickle()
unpickled = Data_set(pklfile=name)
print unpickled.name == to_pickle.name
# test comparison
blorg = Data_set_manager({})
x_step = 1.
n_bars = 4 # also try an odd number
for n in range(n_bars):
blorg.add_dataset(Data_set(x=x_step * np.arange(n_bars),
y=np.random.rand(n_bars),
name='teste' + str(n)))
fig, ax = blorg.compare()
fig.show()
It should work with both even and odd number of bars:
And as long as you keep a record of the names you've used (tip:look in the folder where you are saving them) you can reload the data and compare it with the new one.
More checks could be made (to make sure the file exists, that the x axis is something that can be subtracted before trying to do so, etc.), and it could also use some documentation and proper testing - but this should do in a hurry.