Bokeh histogram will not plot - python

My issue seems exactly like this post (albeit column types may be different):
Cannot plot Histogram on Ubuntu 14.04
The code is straight out of the docs http://docs.bokeh.org/en/0.10.0/docs/user_guide/charts.html#histograms
I couldn't comment on that post so needed to ask again if a solution was found...
My system is SUSE. Just trying to plot a simple histogram of datetimes from a pandas df series.
>>>df
ACQ_DATE
0 2017-01-28
1 2017-01-28
... ...
456365 2017-07-25
456366 2017-07-25
>>>hist = Histogram(df['ACQ_DATE'], title="Fire Frequency")
2017-08-22 11:56:15,240 Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x2b6cc2c8f358>: expected an element of either Column(Float) or Column(String), got array(['2017- 01-28T00:00:00.000000000', '2017-01-28T00:00:00.000000000',
'2017-01-28T00:00:00.000000000', ...,
'2017-07-25T00:00:00.000000000', '2017-07-25T00:00:00.000000000',
'2017-07-25T00:00:00.000000000'], dtype='datetime64[ns]')
File "properties.py", line 676, in validate:
raise ValueError("expected an element of either %s, got %r" % (nice_join (self.type_params), value)) Traceback (most recent call last):
File "/home/byed/venv36/lib/python3.6/site- packages/bokeh/application/handlers/code_runner.py", line 81, in run
exec(self._code, module.__dict__)
File "/home/byed/job/fire/report_fire_points.py", line 118, in <module>
hist = Histogram(df['ACQ_DATE'], title="Fire Frequency") #, tools='pan,wheel_zoom,box_select,reset')
File "/home/byed/venv36/lib/python3.6/site- packages/bkcharts/builders/histogram_builder.py", line 107, in Histogram
return create_and_build(HistogramBuilder, data, **kw)
File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/builder.py", line 56, in create_and_build
chart.add_builder(builder)
File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/chart.py", line 155, in add_builder
builder.create(self)
File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/builder.py", line 512, in create
chart.add_renderers(self, renderers)
File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/chart.py", line 150, in add_renderers
self.renderers += renderers
File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/containers.py", line 76, in wrapper
result = func(self, *args, **kwargs)
File "/home/byed/venv36/lib/python3.6/site- packages/bokeh/core/property/containers.py", line 172, in __iadd__
return super(PropertyValueList, self).__iadd__(y)
File "/home/byed/venv36/lib/python3.6/site- packages/bkcharts/builders/bar_builder.py", line 221, in yield_renderers
**group_kwargs)
File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/glyphs.py", line 950, in __init__
super(HistogramGlyph, self).__init__(**kwargs)
File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/glyphs.py", line 490, in __init__
super(AggregateGlyph, self).__init__(**kwargs)
File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/models.py", line 83, in __init__
super(CompositeGlyph, self).__init__(**properties)
File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/has_props.py", line 246, in __init__
setattr(self, name, value)
File "/home/byed/venv36/lib/python3.6/site- packages/bokeh/core/has_props.py", line 274, in __setattr__
super(HasProps, self).__setattr__(name, value)
File "/home/byed/venv36/lib/python3.6/site- packages/bokeh/core/property/descriptors.py", line 495, in __set__
self._internal_set(obj, value, setter)
File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/descriptors.py", line 713, in _internal_set
value = self.property.prepare_value(obj, self.name, value)
File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/bases.py", line 290, in prepare_value
raise e
File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/bases.py", line 283, in prepare_value
self.validate(value)
File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/properties.py", line 676, in validate
raise ValueError("expected an element of either %s, got %r" % (nice_join (self.type_params), value))
ValueError: expected an element of either Column(Float) or Column(String), got array(['2017-01-28T00:00:00.000000000', '2017-01-28T00:00:00.000000000',
'2017-01-28T00:00:00.000000000', ...,
'2017-07-25T00:00:00.000000000', '2017-07-25T00:00:00.000000000',
'2017-07-25T00:00:00.000000000'], dtype='datetime64[ns]')
Any help would be much appreciated.
Cheers n Thanks

Don't use bokeh.charts (now a separate bkcharts project), including Histogram. The bkcharts project is currently abandoned and unmaintained. However, it's pretty trivial to create histograms using bokeh.plotting which is the stable and well-supported core API of Bokeh:
import numpy as np
from bokeh.io import show, output_file
from bokeh.plotting import figure
data = np.random.normal(0, 0.5, 1000)
hist, edges = np.histogram(data, density=True, bins=50)
p = figure()
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color="white")
output_file("hist.html")
show(p)
Alternatively, if you want a very-high level statistical charting API on top of Bokeh, then check out these options:
Holoviews
PandasBokeh
Chartify

Thanks to #bigreddot!
I'm using Google Colab as an online Jupyter notebook environment. And to plot Bokeh histogram in Colab, I had to tweak the sample code snippet above by using output_notebook():
import numpy as np
from bokeh.io import show, output_notebook
from bokeh.plotting import figure
output_notebook()
data = np.random.normal(0, 0.5, 1000)
hist, edges = np.histogram(data, density=True, bins=10)
p = figure()
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color="white")
show(p)

Related

Exception thrown in PyCharm debug mode but not in run mode (seaborn, matplotlib)

I'm using
PyCharm 2021.2.3 Community Edition
Python interpreter 3.10.0
matplotlib 3.5.0
seaborn 0.11.2
numpy 1.21.4
pandas 1.3.4
PySimpleGUI 4.55.1
When I run the following script, it is fine in run mode but in debug mode it throws an exception. Here's the script:
import numpy as np
import pandas as pd
import PySimpleGUI as sg
import seaborn as sns
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
def init_data():
r1 = np.random.rand(5, 4)
columns = [f"var{i}" for i in range(1, 5)]
df = pd.DataFrame(r1, columns=columns)
df.insert(0, 'year', range(2021, 2026))
df.insert(1, 'scenario', 'test1')
ldf = pd.melt(df, id_vars=['year', 'scenario'], value_vars=columns, var_name='percentile', value_name='carbon')
return ldf
def draw_figure(canvas, figure):
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
return figure_canvas_agg
# define the window layout
layout = [[sg.Text('Plot test')],
[sg.Canvas(key='-CANVAS-')],
[sg.Button('Ok')]]
# create the form and show it without the plot
window = sg.Window('Testing seaborn in PySimpleGUI', layout, finalize=True,
element_justification='center', font='Helvetica 18')
figure = Figure()
ax = figure.subplots()
sns.lineplot(x='year', y='carbon', hue='percentile', data=init_data(), ax=ax)
# add the plot to the window
fig_canvas_agg = draw_figure(window['-CANVAS-'].TKCanvas, figure)
event, values = window.read()
window.close()
And here's the stacktrace:
Traceback (most recent call last):
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\getlimits.py", line 384, in __new__
dtype = numeric.dtype(dtype)
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/OneDrive/OneDrive - Louise Pryor & Co Ltd/Actuarial/Carbon/Carbon/seaborntest.py", line 39, in <module>
sns.lineplot(x='year', y='carbon', hue='percentile', data=init_data(), ax=ax)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\_decorators.py", line 46, in inner_f
return f(**kwargs)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\relational.py", line 710, in lineplot
p.plot(ax, kwargs)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\relational.py", line 557, in plot
self._add_axis_labels(ax)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\_core.py", line 1194, in _add_axis_labels
x_visible = any(t.get_visible() for t in ax.get_xticklabels())
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 75, in wrapper
return get_method(self)(*args, **kwargs)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1249, in get_ticklabels
return self.get_majorticklabels()
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1201, in get_majorticklabels
ticks = self.get_major_ticks()
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1371, in get_major_ticks
numticks = len(self.get_majorticklocs())
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1277, in get_majorticklocs
return self.major.locator()
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\ticker.py", line 2113, in __call__
vmin, vmax = self.axis.get_view_interval()
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1987, in getter
return getattr(getattr(self.axes, lim_name), attr_name)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 781, in viewLim
self._unstale_viewLim()
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 776, in _unstale_viewLim
self.autoscale_view(**{f"scale{name}": scale
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 2932, in autoscale_view
handle_single_axis(
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 2895, in handle_single_axis
x0, x1 = locator.nonsingular(x0, x1)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\ticker.py", line 1654, in nonsingular
return mtransforms.nonsingular(v0, v1, expander=.05)
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\transforms.py", line 2880, in nonsingular
if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny:
File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\getlimits.py", line 387, in __new__
dtype = numeric.dtype(type(dtype))
TypeError: 'NoneType' object is not callable
Has anyone seen this before or have any ideas?
I had the same issue until I changed my version of python from 3.10 to 3.9.5.
This did the ugly trick for debugging under pyCharm 2022.1.1, python 3.10.4, scipy=1.8.0
numpy=1.22.3
pandas=1.4.1
AK lame handling of debugg issue with numpy vs pandas/scipy np.core.numeric.dtype
if sys.gettrace():
np.core.numeric.dtype = np.dtype

Issue with tight_layout with matplotlib and cartopy

I recently switched to Matplotlib 3.3.1 and my old script start to complain. I guess it is an issue with cartopy. Here is a minimum reproducible example
import cartopy.crs as ccrs
fig, ax = plt.subplots(2, 2,
subplot_kw=dict(projection=ccrs.PlateCarree()),
figsize=[12,7], sharex=True, sharey=True)
plt.tight_layout()
Any suggestion to fix this issue?
Here I copy the error message:
Traceback (most recent call last):
File "", line 4, in
plt.tight_layout()
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py",
line 451, in wrapper
return func(*args, **kwargs)
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\pyplot.py",
line 1490, in tight_layout
gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py",
line 411, in wrapper
return func(*inner_args, **inner_kwargs)
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\figure.py",
line 2613, in tight_layout
kwargs = get_tight_layout_figure(
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\tight_layout.py",
line 303, in get_tight_layout_figure
kwargs = auto_adjust_subplotpars(fig, renderer,
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\tight_layout.py",
line 84, in auto_adjust_subplotpars
bb += [ax.get_tightbbox(renderer, for_layout_only=True)]
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\axes_base.py",
line 4203, in get_tightbbox
bbox = a.get_tightbbox(renderer)
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\artist.py",
line 278, in get_tightbbox
bbox = self.get_window_extent(renderer)
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\patches.py",
line 598, in get_window_extent
return self.get_path().get_extents(self.get_transform())
File
"C:\Users\Vinod\anaconda3\lib\site-packages\matplotlib\path.py", line
603, in get_extents
return Bbox([xys.min(axis=0), xys.max(axis=0)])
File
"C:\Users\Vinod\anaconda3\lib\site-packages\numpy\core_methods.py",
line 43, in _amin
return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has
no identity
This was a known issue and was addressed in: https://github.com/SciTools/cartopy/issues/1207. (Making sure you have the latest version of cartopy may resolve this issue).
In the meanwhile, as a workaround it was noted that you could make a call to fig.canvas.draw() before your call to plt.tight_layout().

Python Matplotlib show the cursor when hovering on graph

I want to plot the close price of INS as below and it works. Then I want to add the cursor when hovering on the graph. I follow the demo from https://matplotlib.org/3.1.0/gallery/misc/cursor_demo_sgskip.html and that is what I want.
But when I added these lines into the code, it shows value error. Initially I use epoch time as x-axis and I thought that is the problem, so I convert epoch time to datetime but it is still not working and plot nothing.
snap_cursor = SnaptoCursor(ax, secs, df['close'])
fig.canvas.mpl_connect('motion_notify_event', snap_cursor.mouse_move)
Traceback (most recent call last): File
"c:\Users\Sam.vscode\extensions\ms-python.python-2020.6.89148\pythonFiles\ptvsd_launcher.py",
line 48, in
main(ptvsdArgs) File "c:\Users\Sam.vscode\extensions\ms-python.python-2020.6.89148\pythonFiles\lib\python\old_ptvsd\ptvsd_main_.py",
line 432, in main
run() File "c:\Users\Sam.vscode\extensions\ms-python.python-2020.6.89148\pythonFiles\lib\python\old_ptvsd\ptvsd_main_.py",
line 316, in run_file
runpy.run_path(target, run_name='main') File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\runpy.py",
line 263, in run_path
pkg_name=pkg_name, script_name=fname) File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\runpy.py",
line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name) File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\runpy.py",
line 85, in _run_code
exec(code, run_globals) File "c:\Users\Sam\OneDrive\Project\stock\test.py", line 89, in
plt.gcf().autofmt_xdate() File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\figure.py",
line 632, in autofmt_xdate
for label in self.axes[0].get_xticklabels(which=which): File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes_base.py",
line 3355, in get_xticklabels
return self.xaxis.get_ticklabels(minor=minor, which=which) File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axis.py",
line 1320, in get_ticklabels
return self.get_majorticklabels() File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axis.py",
line 1276, in get_majorticklabels File
"C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axis.py",
line 1431, in get_major_ticks
numticks = len(self.get_majorticklocs()) File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axis.py",
line 1348, in get_majorticklocs
return self.major.locator() File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\dates.py",
line 1338, in call
self.refresh() File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\dates.py",
line 1364, in refresh
dmin, dmax = self.viewlim_to_dt() File "C:\Users\Sam\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\dates.py",
line 1098, in viewlim_to_dt
.format(vmin))
ValueError: view limit minimum -36879.777083333334 is less than 1 and
is an invalid Matplotlib date value. This often happens if you pass a
non-datetime value to an axis that has datetime units
class SnaptoCursor(object):
"""
Like Cursor but the crosshair snaps to the nearest x, y point.
For simplicity, this assumes that *x* is sorted.
"""
def __init__(self, ax, x, y):
self.ax = ax
self.lx = ax.axhline(color='k') # the horiz line
self.ly = ax.axvline(color='k') # the vert line
self.x = x
self.y = y
# text location in axes coords
self.txt = ax.text(0.7, 0.9, '', transform=ax.transAxes)
def mouse_move(self, event):
if not event.inaxes:
return
x, y = event.xdata, event.ydata
indx = min(np.searchsorted(self.x, x), len(self.x) - 1)
x = self.x[indx]
y = self.y[indx]
# update the line positions
self.lx.set_ydata(y)
self.ly.set_xdata(x)
self.txt.set_text('x=%1.2f, y=%1.2f' % (x, y))
print('x=%1.2f, y=%1.2f' % (x, y))
self.ax.figure.canvas.draw()
data = td.pricehistory("INS")
df = pd.DataFrame(data['candles'])
df['datetime'] = df.apply(lambda x: datetime.datetime.fromtimestamp(x['datetime']/1000),axis=1)
secs = df['datetime']
fig, ax = plt.subplots(1, 1)
ax.plot(secs,df['close'])
snap_cursor = SnaptoCursor(ax, secs, df['close'])
fig.canvas.mpl_connect('motion_notify_event', snap_cursor.mouse_move)
plt.gcf().autofmt_xdate()
myFmt = mdates.DateFormatter('%d-%m-%y %H:%M:%S')
plt.gca().xaxis.set_major_formatter(myFmt)
plt.show()
I'm not familiar with SnaptoCursor, but have you considered using plotly instead?
Saves many lines of code and is very user-friendly:
# install plotly library
!pip install plotly
# import express module
import plotly.express as px
# plot interactive line chart
fig = px.line(data=df, x="datetime", y="close")
fig.show()
It's very flexible too. Here is the documentation: https://plotly.com/python/line-charts/
You can interact with the plots using the cursor too, for example:
Hope this helps, though of course it's different to what you were trying to do!

Intermittent error while plotting graph using matplotlib "RuntimeError: main thread is not in main loop"

I am trying to pull data from different end points using my python code and feeding the same data to plot a graph using matplotlib. I dont have any problem in reading the data but when i invoke the methods to plot graph by feeding the data i see intermittent error caused by matplot lib. below are the error details.
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\slackbot\dispatcher.py", line 55, in _dispatch_msg_handler
func(Message(self._client, msg), *args)
File "C:\PycharmProjects\SlackBot\src\plugins\bot_response.py", line 248, in checkmarx
draw_chart.riskscore_bar(top_riskscore, project_name, "output_files", "riskscore_bar.png")
File "C:\PycharmProjects\SlackBot\src\drawchart.py", line 111, in riskscore_bar
fig, ax = plt.subplots()
File "C:\Python35\lib\site-packages\matplotlib\pyplot.py", line 1202, in subplots
fig = figure(**fig_kw)
File "C:\Python35\lib\site-packages\matplotlib\pyplot.py", line 535, in figure
**kwargs)
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 81, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 98, in new_figure_manager_given_figure
icon_img = Tk.PhotoImage(file=icon_fname)
File "C:\Python35\lib\tkinter\__init__.py", line 3403, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python35\lib\tkinter\__init__.py", line 3359, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
RuntimeError: main thread is not in main loop
I have tried looking into other cases from stackoverflow with same error messages, but it didnt help me fix this. Below is my code snippet that invokes an error.
def riskscore_bar(self, top_riskscore, project_id, output_folder, output_filename):
logger.debug("Inside method plotgraph in drawchart.py.")
y_pos = np.arange(len(project_id))
width = .4
fig, ax = plt.subplots()
graph = ax.bar(y_pos+1, top_riskscore, width, color='#feb308')
ax.set_ylabel('Risk Score')
ax.set_title('Summary')
ax.set_xticks(y_pos + 1)
ax.set_xticklabels(project_id,fontsize=5, rotation=45 )
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.001*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(graph)
pylab.savefig(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', output_folder,output_filename))
The error seems to occur at "fig, ax = plt.subplots()". any ideas on how this can be fixed?
this can be solved by adding plt.switch_backend('agg') below right after you import matplotlib.pyplot as plt. As shown below
import matplotlib.pyplot as plt
plt.switch_backend('agg')

python cluster animation using matplotlib and different colors

I'm trying to create an animation of my clustering where each cluster is a different color on a scatter plot and there are several clusters per plot. The next plot to show has the same clusters and colors but a different location.
I've searched around and so far have this (timePeriodData refers to a list of cluster objects. Each list contains the clusters for that time/plot and the cluster object has the x, y, and color data.):
plt.ion()
fig,ax = subplots(1,1)
ax.set_aspect('equal')
fig.canvas.draw()
background = fig.canvas.copy_from_bbox(ax.bbox)
for timeData in timePeriodData:
plt.clf()
#print timeperiod
for cluster in timeData:
#ax.scatter(cluster.dataX,cluster.dataY, color = cluster.color)
# restore background
plt.scatter(cluster.dataX,cluster.dataY, color = cluster.color)
#plt.show()
#fig.canvas.restore_region(background)
# redraw just the points
#ax.draw_artist()
# fill in the axes rectangle
#fig.canvas.blit(ax.bbox)
plt.draw()
time.sleep(1)
But the plot window eventually hangs and doesn't continue plotting after about 5 plots. You can see in the comments I also tried to save the background and update that, but I am doing something wrong there. What exactly needs to be updated confuses me and I can't find good documentation on what needs to be passed to draw_artist.
I've also tried to use the animation feature of matplotlib, but can't seem to get it working with what I am trying to do.
timeperiods = 4
fig, ax = plt.subplots()
#fig = plt.figure()
def update(timePeriod, *args):
clusterNum = 0
for cluster in args[timePeriod]:
scat = ax.scatter(cluster.dataX,cluster.dataY, color = cluster.color)
clusterNum +=1
#scat = ax.scatter(dataX, dataY, c = scalarMap.to_rgba(values[clusterNum]))
return scat
ani = animation.FuncAnimation(fig, update, frames=xrange(timeperiods),
fargs=(timePeriodData), blit = True)
plt.show()
It says something is not iterable, but I'm sure what part it is referring to. I'm also not sure if I can continually assign scat to ax.scatter and it will add the points or not. but I assume so. Error message is:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 276, in resize
self.show()
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 348, in draw
FigureCanvasAgg.draw(self)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1040, in draw
self.canvas.draw_event(renderer)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 1693, in draw_event
self.callbacks.process(s, event)
File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 527, in process
proxy(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 405, in __call__
return mtd(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 832, in _end_redraw
self._post_draw(None, self._blit)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 778, in _post_draw
self._blit_draw(self._drawn_artists, self._blit_cache)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 787, in _blit_draw
for a in artists:
TypeError: 'PathCollection' object is not iterable
Any help getting a working system would be great!

Categories