Can't populate matplotlib animation frame one point at a time - pandas

I'm currently trying to build an N-body simulation but I'm having a little trouble with plotting the results the way I'd like.
In the code below (with some example data for a few points in an orbit) I'm importing the position and time data and organizing it into a pandas dataframe. To create the 3D animation I use matplotlib's animation class, which works perfectly.
However, the usual way to set up an animation is limited in that you can't customize the points in each frame individually (please let me know if I'm wrong here :p). Since my animation is showing orbiting bodies I would like to vary their sizes and colors. To do that I essentially create a graph for each body and set it's color etc. When it gets to the update_graph function, I iterate over the n bodies, retrieve their individual (x,y,z) coordinates, and update their graphs.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d.axes3d import get_test_data
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import pandas as pd
nbodies = 2
x = np.array([[1.50000000e-10, 0.00000000e+00, 0.00000000e+00],
[9.99950000e-01, 1.00000000e-02, 0.00000000e+00],
[4.28093585e-06, 3.22964816e-06, 0.00000000e+00],
[-4.16142210e-01, 9.09335149e-01, 0.00000000e+00],
[5.10376489e-06, 1.42204430e-05, 0.00000000e+00],
[-6.53770813e-01, -7.56722445e-01, 0.00000000e+00]])
t = np.array([0.01, 0.01, 2.0, 2.0, 4.0, 4.0])
tt = np.array([0.01, 2.0, 4.0])
x = x.reshape((len(tt), nbodies, 3))
x_coords = x[:, :, 0].flatten()
y_coords = x[:, :, 1].flatten()
z_coords = x[:, :, 2].flatten()
df = pd.DataFrame({"time": t[:] ,"x" : x_coords, "y" : y_coords, "z" : z_coords})
print(df)
def update_graph(num):
data=df[df['time']==tt[num]] # x,y,z of all bodies at current time
for n in range(nbodies): # update graphs
data_n = data[data['x']==x_coords[int(num * nbodies) + n]] # x,y,z of body n
graph = graphs[n]
graph.set_data(data_n.x, data_n.y)
graph.set_3d_properties(data_n.z)
graphs[n] = graph
return graphs
plt.style.use('dark_background')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('x (AU)')
ax.set_ylabel('y (AU)')
ax.set_zlabel('z (AU)')
plt.xlim(-1.5,1.5)
plt.ylim(-1.5,1.5)
# initialize
data=df[df['time']==0]
ms_list = [5, 1]
c_list = ['yellow', 'blue']
graphs = []
for n in range(nbodies):
graphs.append(ax.plot([], [], [], linestyle="", marker=".",
markersize=ms_list[n], color=c_list[n])[0])
ani = animation.FuncAnimation(fig, update_graph, len(tt),
interval=400, blit=True, repeat=True)
plt.show()
However, doing this gives me the following error:
Traceback (most recent call last):
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 1194, in _on_timer
ret = func(*args, **kwargs)
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1447, in _step
still_going = Animation._step(self, *args)
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1173, in _step
self._draw_next_frame(framedata, self._blit)
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1193, in _draw_next_frame
self._post_draw(framedata, blit)
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1216, in _post_draw
self._blit_draw(self._drawn_artists, self._blit_cache)
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1231, in _blit_draw
a.axes.draw_artist(a)
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2661, in draw_artist
a.draw(self.figure._cachedRenderer)
File "/home/kris/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/kris/anaconda3/lib/python3.7/site-packages/mpl_toolkits/mplot3d/art3d.py", line 202, in draw
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
File "/home/kris/anaconda3/lib/python3.7/site-packages/mpl_toolkits/mplot3d/proj3d.py", line 201, in proj_transform
vec = _vec_pad_ones(xs, ys, zs)
File "/home/kris/anaconda3/lib/python3.7/site-packages/mpl_toolkits/mplot3d/proj3d.py", line 189, in _vec_pad_ones
return np.array([xs, ys, zs, np.ones_like(xs)])
File "/home/kris/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 871, in __getitem__
result = self.index.get_value(self, key)
File "/home/kris/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 4405, in get_value
return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
File "pandas/_libs/index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 997, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1004, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0
Aborted (core dumped)
I'm not sure what this really means, but I do know the problem is something to do with updating the graphs with only one row of coordinates rather than all three. Because if I instead have
def update_graph(num):
data=df[df['time']==tt[num]] # x,y,z of all bodies at current time
for n in range(nbodies): # update graphs
#data_n = data[data['x']==x_coords[int(num * nbodies) + n]] # x,y,z of body n
graph = graphs[n]
graph.set_data(data.x, data.y) # using data rather than data_n here now
graph.set_3d_properties(data.z)
graphs[n] = graph
return graphs
it actually works, and plots three copies of the bodies with varying colors and sizes on top of each other as you would expect.
Any help would be much appreciated. Thanks!

I don't understand why you are going through a pandas DataFrame, when you seem to already have all the data you need in your numpy array. I couldn't reproduce the initial problem, by I propose this solution that uses pure numpy arrays, which may fix the problem:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d.axes3d import get_test_data
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import pandas as pd
nbodies = 2
x = np.array([[1.50000000e-10, 0.00000000e+00, 0.00000000e+00],
[9.99950000e-01, 1.00000000e-02, 0.00000000e+00],
[4.28093585e-06, 3.22964816e-06, 0.00000000e+00],
[-4.16142210e-01, 9.09335149e-01, 0.00000000e+00],
[5.10376489e-06, 1.42204430e-05, 0.00000000e+00],
[-6.53770813e-01, -7.56722445e-01, 0.00000000e+00]])
t = np.array([0.01, 0.01, 2.0, 2.0, 4.0, 4.0])
tt = np.array([0.01, 2.0, 4.0])
x = x.reshape((len(tt), nbodies, 3))
def update_graph(i):
data = x[i, :, :] # x,y,z of all bodies at current time
for body, graph in zip(data, graphs): # update graphs
graph.set_data(body[0], body[1])
graph.set_3d_properties(body[2])
return graphs
plt.style.use('dark_background')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('x (AU)')
ax.set_ylabel('y (AU)')
ax.set_zlabel('z (AU)')
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
# initialize
ms_list = [50, 10]
c_list = ['yellow', 'blue']
graphs = []
for n in range(nbodies):
graphs.append(ax.plot([], [], [], linestyle="", marker=".",
markersize=ms_list[n], color=c_list[n])[0])
ani = animation.FuncAnimation(fig, func=update_graph, frames=len(tt),
interval=400, blit=True, repeat=True)
plt.show()

Related

Cannot use custom non linear colormap in combination with imshow

I am trying to use a custom colormap to display a ConfusionMatrixDisplay object to have a finer range between 0 and 50 than between 50 and 100 using this answer.
from sklearn.datasets import make_classification
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = (15, 15)
font = {'family' : 'DejaVu Sans',
'weight' : 'bold',
'size' : 22}
plt.rc('font', **font)
class nlcmap(LinearSegmentedColormap):
def __init__(self, cmap, levels):
self.cmap = cmap
self.N = cmap.N
self.monochrome = self.cmap.monochrome
self.levels = np.asarray(levels, dtype='float64')
self._x = self.levels
self.levmax = self.levels.max()
self.transformed_levels = np.linspace(0.0, self.levmax, len(self.levels))
def __call__(self, xi, alpha=1.0, **kw):
yi = np.interp(xi, self._x, self.transformed_levels)
return self.cmap(yi / self.levmax, alpha)
levels = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 100]
cmap_nonlin = nlcmap(plt.cm.viridis, levels)
X, y = make_classification(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y,
random_state=0)
clf = SVC(random_state=0)
clf.fit(X_train, y_train)
SVC(random_state=0)
predictions = clf.predict(X_test)
cm = confusion_matrix(y_test, predictions, labels=clf.classes_)
disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=clf.classes_)
lin_cmap = plt.cm.viridis
levels = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 100]
cmap_nonlin = nlcmap(plt.cm.viridis, levels)
fig, ax = plt.subplots()
im = disp.plot(cmap=cmap_nonlin, colorbar=False)
disp.ax_.get_images()[0].set_clim(0, 100)
disp.figure_.colorbar(disp.im_, orientation="horizontal", pad=0.1)
plt.savefig("test.png")
Produces the following error:
Traceback (most recent call last):
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 61, in _draw
self.figure.draw(renderer)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/figure.py", line 1864, in draw
renderer, self, artists, self.suppressComposite)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper
return func(*inner_args, **inner_kwargs)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2747, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/me/anaconda3/envs/myenv/lib/python3.6/site-packages/matplotlib/image.py", line 646, in draw
renderer.draw_image(gc, l, b, im)
TypeError: Cannot cast array data from dtype('float64') to dtype('uint8') according to the rule 'safe'
It seems the error is related to imshow in conjunction with custom colormap since I can reproduce without sklearn with:
fig, ax = plt.subplots()
ax.imshow(np.array([[10, 15], [20, 30]]), cmap=cmap_nonlin)
Any idea ? I wish to modify the colormap not the data itself if possible.
According to matplotlib's doc on LinearSegmentedColormaps one can do the following to vary the contrast between segments with fast varying segment and slow varying segments.
In this case to answer my question let's have a finer range between 0 and 50 than between 50 and 100 but my solution can be extended to an arbitrary number of different paced segments by changing the levels:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors
# A dict with {percentage_of_max_value: percentage_of_variation}. The keys are thus all < 1. and should be in ascending order alongside associated values in the colormap (also ordered and < 1.).
# In this example we have 90% of the variation of the colormap in its first half (until 0.5) and the remaining 10% in its right half
levels = {0.5: 0.9}
# We are not limited to one segment and we can provide for instance the following dict
# levels = {0.4:0.8, 0.5:0.9} to have 80% of variations between 0 and 40% of the colormap max then 10% between 40 and 50% and then the remaining 10% for the rest
cdict = {"red": None, "green": None, "blue": None}
num_values_per_segment = 50
for k, v in cdict.items():
cdict[k] = []
# We start the first segment by 0. both for value and cmap_value
left_val = 0.
left_cmap_val = 0.
for val, cmap_val in levels.items():
values = np.linspace(left_val, val, num_values_per_segment).tolist()
dynamic_range = np.linspace(left_cmap_val, cmap_val, num_values_per_segment).tolist()
for i, (v, r) in enumerate(zip(values, dynamic_range)):
cdict[k].append((v, r, r))
left_val = val
left_cmap_val = cmap_val
# Last segment towards 1.
values = np.linspace(val, 1., num_values_per_segment).tolist()
dynamic_range = np.linspace(cmap_val, 1., num_values_per_segment).tolist()
for i, (v, r) in enumerate(zip(values, dynamic_range)):
cdict[k].append((v, r, r))
# Mapping levels to colormap
cmap = plt.cm.viridis
for k, v in cdict.items():
if k == "red":
for i in range(len(v)):
cdict[k][i] = (v[i][0], cmap(v[i][1])[0], cmap(v[i][2])[0])
elif k == "green":
for j in range(len(v)):
cdict[k][j] = (v[j][0], cmap(v[j][1])[1], cmap(v[j][2])[1])
elif k == "blue":
for l in range(len(v)):
cdict[k][l] = (v[l][0], cmap(v[l][1])[2], cmap(v[l][2])[2])
else:
raise ValueError("Color not recognized")
cdict[k] = tuple(cdict[k])
cmap_nonlin = colors.LinearSegmentedColormap('MyCustomCMap', cdict)
fig, ax = plt.subplots()
my_image = np.array([[30, 45], [25, 10]])
confusion = ax.imshow(my_image, cmap=cmap_nonlin, vmin=0, vmax=100)
plt.colorbar(confusion, ax=ax)
plt.waitforbuttonpress()
And the resulting cmap_nonlin object can be used in conjunction with imshow without any issue:

Animate a point moving along path between two points

I want to animate a point moving along a path from one location to another on the map.
For example, I drawn a path from New York to New Delhi, using Geodetic transform. Eg. taken from docs Adding data to the map
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='blue', linewidth=2, marker='o',
transform=ccrs.Geodetic(),
)
Now i want to move a point along this path.
My idea was to somehow get some (say 50) points, along the path and plot a marker on each point for each frame. But I am not able to find a way to get the points on the path.
I found a function transform_points under classCRS, but I am unable to use this, as this gives me the same number of points i have, not the points in between.
Thanks in advance!
There are a couple of approaches to this.
The matplotlib approach
I'll start with perhaps the most basic if you are familiar with matplotlib, but this approach suffers from indirectly using cartopy's functionality, and is therefore harder to configure/extend.
There is a private _get_transformed_path method on a Line2D object (the thing that is returned from plt.plot). The resulting TransformedPath object has a get_transformed_path_and_affine method, which basically will give us the projected line (in the coordinate system of the Axes being drawn).
In [1]: import cartopy.crs as ccrs
In [3]: import matplotlib.pyplot as plt
In [4]: ax = plt.axes(projection=ccrs.Robinson())
In [6]: ny_lon, ny_lat = -75, 43
In [7]: delhi_lon, delhi_lat = 77.23, 28.61
In [8]: [line] = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
...: color='blue', linewidth=2, marker='o',
...: transform=ccrs.Geodetic(),
...: )
In [9]: t_path = line._get_transformed_path()
In [10]: path_in_data_coords, _ = t_path.get_transformed_path_and_affine()
In [11]: path_in_data_coords.vertices
Out[11]:
array([[-6425061.82215208, 4594257.92617961],
[-5808923.84969279, 5250795.00604155],
[-5206753.88613758, 5777772.51828996],
[-4554622.94040482, 6244967.03723341],
[-3887558.58343227, 6627927.97123701],
[-3200922.19194864, 6932398.19937816],
[-2480001.76507805, 7165675.95095855],
[-1702269.5101901 , 7332885.72276795],
[ -859899.12295981, 7431215.78426759],
[ 23837.23431173, 7453455.61302756],
[ 889905.10635756, 7397128.77301289],
[ 1695586.66856764, 7268519.87627204],
[ 2434052.81300274, 7073912.54130764],
[ 3122221.22299409, 6812894.40443648],
[ 3782033.80448001, 6478364.28561403],
[ 4425266.18173684, 6062312.15662039],
[ 5049148.25986903, 5563097.6328901 ],
[ 5616318.74912886, 5008293.21452795],
[ 6213232.98764984, 4307186.23400115],
[ 6720608.93929235, 3584542.06839575],
[ 7034261.06659143, 3059873.62740856]])
We can pull this together with matplotlib's animation functionality to do as requested:
import cartopy.crs as ccrs
import matplotlib.animation as animation
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.Robinson())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
[line] = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='blue', linewidth=2, marker='o',
transform=ccrs.Geodetic(),
)
t_path = line._get_transformed_path()
path_in_data_coords, _ = t_path.get_transformed_path_and_affine()
# Draw the point that we want to animate.
[point] = plt.plot(ny_lon, ny_lat, marker='o', transform=ax.projection)
def animate_point(i):
verts = path_in_data_coords.vertices
i = i % verts.shape[0]
# Set the coordinates of the line to the coordinate of the path.
point.set_data(verts[i, 0], verts[i, 1])
ani = animation.FuncAnimation(
ax.figure, animate_point,
frames= path_in_data_coords.vertices.shape[0],
interval=125, repeat=True)
ani.save('point_ani.gif', writer='imagemagick')
plt.show()
The cartopy approach
Under the hood, cartopy's matplotlib implementation (as used above), is calling the project_geometry method. We may as well make use of this directly as it is often more convenient to be using Shapely geometries than it is matplotlib Paths.
With this approach, we simply define a shapely geometry, and then construct the source and target coordinate reference systems that we want to convert the geometry from/to:
target_cs.project_geometry(geometry, source_cs)
The only thing we have to watch out for is that the result can be a MultiLineString (or more generally, any Multi- geometry type). However, in our simple case, we don't need to deal with that (incidentally, the same was true of the simple Path returned in the first example).
The code to produce a similar plot to above:
import cartopy.crs as ccrs
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import shapely.geometry as sgeom
ax = plt.axes(projection=ccrs.Robinson())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
line = sgeom.LineString([[ny_lon, ny_lat], [delhi_lon, delhi_lat]])
projected_line = ccrs.PlateCarree().project_geometry(line, ccrs.Geodetic())
# We only animate along one of the projected lines.
if isinstance(projected_line, sgeom.MultiLineString):
projected_line = projected_line.geoms[0]
ax.add_geometries(
[projected_line], ccrs.PlateCarree(),
edgecolor='blue', facecolor='none')
[point] = plt.plot(ny_lon, ny_lat, marker='o', transform=ccrs.PlateCarree())
def animate_point(i):
verts = np.array(projected_line.coords)
i = i % verts.shape[0]
# Set the coordinates of the line to the coordinate of the path.
point.set_data(verts[i, 0], verts[i, 1])
ani = animation.FuncAnimation(
ax.figure, animate_point,
frames=len(projected_line.coords),
interval=125, repeat=True)
ani.save('projected_line_ani.gif', writer='imagemagick')
plt.show()
Final remaaaaarrrrrrks....
The approach naturally generalises to animating any type of matplotlib Arrrrtist.... in this case, I took a bit more control over the great circle resolution, and I animated an image along the great circle:
import cartopy.crs as ccrs
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import shapely.geometry as sgeom
ax = plt.axes(projection=ccrs.Mercator())
ax.stock_img()
line = sgeom.LineString([[-5.9845, 37.3891], [-82.3666, 23.1136]])
# Higher resolution version of Mercator. Same workaround as found in
# https://github.com/SciTools/cartopy/issues/8#issuecomment-326987465.
class HighRes(ax.projection.__class__):
#property
def threshold(self):
return super(HighRes, self).threshold / 100
projected_line = HighRes().project_geometry(line, ccrs.Geodetic())
# We only animate along one of the projected lines.
if isinstance(projected_line, sgeom.MultiLineString):
projected_line = projected_line.geoms[0]
# Add the projected line to the map.
ax.add_geometries(
[projected_line], ax.projection,
edgecolor='blue', facecolor='none')
def ll_to_extent(x, y, ax_size=(4000000, 4000000)):
"""
Return an image extent in centered on the given
point with the given width and height.
"""
return [x - ax_size[0] / 2, x + ax_size[0] / 2,
y - ax_size[1] / 2, y + ax_size[1] / 2]
# Image from https://pixabay.com/en/sailing-ship-boat-sail-pirate-28930/.
pirate = plt.imread('pirates.png')
img = ax.imshow(pirate, extent=ll_to_extent(0, 0), transform=ax.projection, origin='upper')
ax.set_global()
def animate_ship(i):
verts = np.array(projected_line.coords)
i = i % verts.shape[0]
# Set the extent of the image to the coordinate of the path.
img.set_extent(ll_to_extent(verts[i, 0], verts[i, 1]))
ani = animation.FuncAnimation(
ax.figure, animate_ship,
frames=len(projected_line.coords),
interval=125, repeat=False)
ani.save('arrrr.gif', writer='imagemagick')
plt.show()
All code and images for this answer can be found at https://gist.github.com/pelson/618a5f4ca003e56f06d43815b21848f6.

Pandas Timeseries Subplots with Rectangle

I would like to create a plot using a pandas timeseries in one subplot and a rectangle in another subplot.
If I don't include the subplots, I can achieve this pretty easily:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.patches as mpatches
N = 100
np.random.seed(N)
dates = pd.date_range(start='2018-01-01', periods=N, freq='D')
one_third_delta = (dates[-1] - dates[0])/3
one_third_stamp = dates[0] + one_third_delta
ts = pd.Series(index=dates, data=np.random.randn(N))
def add_rectangle(ax, x, y, width, height, **kwargs):
ax.add_patch(mpatches.Rectangle(
(x, y),
width,
height,
**kwargs
))
args = [one_third_stamp, -1, one_third_delta, 2]
kwargs = {
'facecolor': 'orange',
'edgecolor': 'None',
'alpha': 0.5,
}
# Plot 1: 1 subplot with ts plotted first (Working)
fig, ax = plt.subplots()
ts.plot(ax=ax)
add_rectangle(ax, *args, **kwargs)
plt.savefig('plot1.png')
plt.close(fig)
Plot 1
However, things already start to get weird when I try adding the rectangle first:
# Plot 2: 1 subplot with ts plotted second (Not Working)
fig, ax = plt.subplots()
add_rectangle(ax, *args, **kwargs)
ts.plot(ax=ax)
plt.savefig('plot2.png')
plt.close(fig)
Plot 2
If I try splitting out the two plots, neither approach works:
# Plot 3: 2 subplots with ts plotted first (Not Working)
fig, axes = plt.subplots(2, sharex=True)
ts.plot(ax=axes[1])
add_rectangle(axes[0], *args, **kwargs)
plt.savefig('plot3.png')
plt.close(fig)
# Plot 4: 2 subplots with ts plotted second (Not Working)
fig, axes = plt.subplots(2, sharex=True)
add_rectangle(axes[0], *args, **kwargs)
ts.plot(ax=axes[1])
plt.savefig('plot4.png')
plt.close(fig)
Plot 3
Plot 4
I've found two work-arounds.
The first involves casting everything to a float with matplotlib.dates.date2num:
# Plot 5: 2 subplots with date2num (Working)
two_thirds_stamp = one_third_stamp + one_third_delta
args_date2num = [
mdates.date2num(one_third_stamp),
-1,
mdates.date2num(two_thirds_stamp) - mdates.date2num(one_third_stamp),
2,
]
df = ts.to_frame().reset_index()
df.columns = ['date', 'value']
df['num'] = df.date.apply(mdates.date2num)
fig, axes = plt.subplots(2, sharex=True)
add_rectangle(axes[0], *args_date2num, **kwargs)
axes[1].plot_date(df.num, df.value, ls='-', marker=None)
axes[0].set_ylim(axes[1].get_ylim())
plt.savefig('plot5.png')
plt.close(fig)
Plot 5
This isn't great for two reasons:
I lose the nice ticklabel formatting that pandas uses.
As far as I can tell, date2num is incompatible with how pandas internally represents datetimes as floats.
So if I use date2num at all, all other datetimes must be converted too.
The other work around involves a dummy plot:
# Plot 6: 2 subplots with alpha=0 dummy (Working)
fig, axes = plt.subplots(2, sharex=True)
dummy_ts = ts[::(len(ts)-1)] + 10 # make it out of sight
dummy_ts.plot(ax=axes[0], alpha=0) # and invisible for good measure
add_rectangle(axes[0], *args, **kwargs)
ts.plot(ax=axes[1])
axes[0].set_ylim(axes[1].get_ylim())
plt.savefig('plot6.png')
plt.close(fig)
Plot 6
My question (finally) is why is this necessary?
What changes between doing this on a single subplot vs. multiple?
Is there a better, more canonical way?
Python version:
Python 3.6.3 (v3.6.3:2c5fed86e0)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Pip freeze:
cycler==0.10.0
kiwisolver==1.0.1
matplotlib==2.2.0
numpy==1.14.2
pandas==0.22.0
pyparsing==2.2.0
python-dateutil==2.7.0
pytz==2018.3
six==1.11.0
I think you found the reason yourself: Pandas datetime representation for the matplotlib axes (may) be completely different from the matplotlib date units (this is not always the case and depends on the span of the data).
Since I don't know of any way to convert the rectangle's coordinates to the pandas units, the only option is to plot the pandas plot in matplotlib units.
The problem
But let's start at the beginning. Case 1 and 2 work fine for me.
For the third case, the rectangle is added to the other axes, which does have a different scale. This can be seen by printing the transform.
def add_rectangle(ax, x, y, width, height, **kwargs):
rect = mpatches.Rectangle( (x, y), width, height, **kwargs )
ax.add_patch(rect)
return rect
# Case 1 - working
fig, ax = plt.subplots()
ts.plot(ax=ax)
r = add_rectangle(ax, *args, **kwargs)
print r.get_transform()
# This prints
# BboxTransformTo(
# Bbox(x0=17565.0, y0=-1.0, x1=17598.0, y1=1.0)),
# Case 3 - non-working
fig, axes = plt.subplots(2, sharex=True)
ts.plot(ax=axes[1], x_compat=True)
r = add_rectangle(axes[0], *args, **kwargs)
print r.get_transform()
# BboxTransformTo(
# Bbox(x0=736728.0, y0=-1.0, x1=736761.0, y1=1.0)),
In the second case, the units are the matplotlib date units, because pandas did not change the transform for the axes in which it did not plot anything.
The solution
The easiest option is probably to tell pandas not to change the scale. This would be done using
x_compat=True
This has essentially the same effect as plotting everything in matplotlib units.
# Plot 3: 2 subplots with ts plotted first
fig, axes = plt.subplots(2, sharex=True)
ts.plot(ax=axes[1], x_compat=True)
r = add_rectangle(axes[0], *args, **kwargs)
# Plot 4: 2 subplots with ts plotted second
fig, axes = plt.subplots(2, sharex=True)
add_rectangle(axes[0], *args, **kwargs)
ts.plot(ax=axes[1], x_compat=True)
So indeed the nice pandas formatting is gone. But you may replicate it with the matplotlib.dates formatters. E.g. in this post. an easy solution to add the days is presented. Here, you would maybe rather use a FuncFormatter as follows:
fig, axes = plt.subplots(2, sharex=True)
ts.plot(ax=axes[1], x_compat=True)
r = add_rectangle(axes[0], *args, **kwargs)
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
def f(val, _):
d = mdates.num2date(val)
if d.month == 1:
return d.strftime("%b\n%Y")
else:
return d.strftime("%b")
axes[1].xaxis.set_major_locator(mdates.MonthLocator())
axes[1].xaxis.set_minor_locator(mdates.WeekdayLocator())
axes[1].xaxis.set_major_formatter(mticker.FuncFormatter(f))
fig.autofmt_xdate(rotation=0,ha="center")
producing

Cannot crop lakes with Cartopy

I am trying to draw lakes on a map using Cartopy 0.14 and Shapely 1.5.12. With my custom projection, saving or showing the figure sometimes fails with a stack trace ending with
File "/usr/local/lib/python2.7/dist-packages/Cartopy-0.14.dev0-py2.7-linux-x86_64.egg/cartopy/crs.py", line 291, in _project_multipolygon
r = self._project_polygon(geom, src_crs)
File "/usr/local/lib/python2.7/dist-packages/Cartopy-0.14.dev0-py2.7-linux-x86_64.egg/cartopy/crs.py", line 330, in _project_polygon
return self._rings_to_multi_polygon(rings, is_ccw)
File "/usr/local/lib/python2.7/dist-packages/Cartopy-0.14.dev0-py2.7-linux-x86_64.egg/cartopy/crs.py", line 589, in _rings_to_multi_polygon
multi_poly = sgeom.MultiPolygon(polygon_bits)
File "/usr/local/lib/python2.7/dist-packages/Shapely-1.5.12-py2.7-linux-x86_64.egg/shapely/geometry/multipolygon.py", line 62, in __init__
self._geom, self._ndim = geos_multipolygon_from_polygons(polygons)
File "/usr/local/lib/python2.7/dist-packages/Shapely-1.5.12-py2.7-linux-x86_64.egg/shapely/geometry/multipolygon.py", line 178, in geos_multipolygon_from_polygons
geom, ndims = polygon.geos_polygon_from_py(shell, holes)
File "/usr/local/lib/python2.7/dist-packages/Shapely-1.5.12-py2.7-linux-x86_64.egg/shapely/geometry/polygon.py", line 503, in geos_polygon_from_py
geos_shell, ndim = geos_linearring_from_py(shell)
File "shapely/speedups/_speedups.pyx", line 214, in shapely.speedups._speedups.geos_linearring_from_py (shapely/speedups/_speedups.c:3679)
ValueError: A LinearRing must have at least 3 coordinate tuples
This happens when the boundary of a lake intersects the boundary of the projection. I was unable to reproduce the behaviour with built-in Cartopy projections. Here is the minimal test case I could come up with:
from cartopy import crs as ccrs
from cartopy import feature as cfeature
from matplotlib import pyplot as plt
import numpy as np
from shapely import geometry as sgeom
class Polyconic(ccrs.Projection):
NUM_BOUNDARY_SEGMENTS = 30
def __init__(self, central_longitude, globe=None):
proj4_params = [
('proj', 'poly'),
('lon_0', central_longitude)]
super(Polyconic, self).__init__(proj4_params, globe=globe)
bounds = self.ToPolygon(self.GetLimits(central_longitude)).bounds
self._x_limits = bounds[0], bounds[2]
self._y_limits = bounds[1], bounds[3]
self._boundary = self.ToPolygon(self.GetDomain(central_longitude)).exterior
if not self._boundary.is_ccw:
self._boundary.coords = list(self._boundary.coords)[::-1]
#staticmethod
def GetDomain(central_longitude):
lats = np.linspace(0, +90, Polyconic.NUM_BOUNDARY_SEGMENTS + 1)
lons = np.linspace(
central_longitude - 15., central_longitude + 15.,
Polyconic.NUM_BOUNDARY_SEGMENTS + 1)
domain = []
for lat in lats:
domain.append((central_longitude - 15., lat))
for lat in reversed(lats):
domain.append((central_longitude + 15., lat))
return domain
#staticmethod
def GetLimits(central_longitude):
return [
(central_longitude - 15., 0.),
(central_longitude + 15., 0.),
(central_longitude + 15., +90.),
(central_longitude - 15., +90.)]
def ToPolygon(self, polygon):
return sgeom.Polygon(self.transform_points(
ccrs.PlateCarree(),
np.array([p[0] for p in polygon]),
np.array([p[1] for p in polygon])))
#property
def threshold(self):
return 1e3
#property
def boundary(self):
return self._boundary
#property
def x_limits(self):
return self._x_limits
#property
def y_limits(self):
return self._y_limits
plt.figure()
# ax = plt.axes(projection=Polyconic(180)) works.
ax = plt.axes(projection=Polyconic(0))
lakes = cfeature.NaturalEarthFeature('physical', 'lakes', '50m')
ax.add_feature(lakes)
plt.show()
I was trying to fix the bug for some time, to no avail. I think it stems from the incorrect assumption that type(polygon) is sgeom.Polygon here. In fact, the variable is sometimes of type sgeom.MultiPolygon or sgeom.GeometryCollection.
While we are at it, it seems to me that line 544 of crs.py might use prep_polygon and lines 562–577 could be simplified as follows:
y4 += by
box = sgeom.box(x3, y3, x4, y4)
for ring in interior_rings:
polygon = sgeom.Polygon(ring)
if polygon.is_valid:
# Invert the polygon
polygon = box.difference(polygon)
My question is: is the bug in my code or in Cartopy?
Whoa! I have finally figured this out. Everything works when I change this fragment:
if not self._boundary.is_ccw:
self._boundary.coords = list(self._boundary.coords)[::-1]
to
if self._boundary.is_ccw:
self._boundary.coords = list(self._boundary.coords)[::-1]
which means that boundaries of projections should be clockwise. With hindsight, I could have inferred it from lines 123–149 of crs.py.

How to plot annotated heatmap on plotly?

I'm trying to make a annotated heatmap on plotly.
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('masterc.csv')
locations = {}
anno = []
for i in range(df.shape[0]):
locations.setdefault((df.iat[i,2],df.iat[i,6]),0)
locations[(df.iat[i,2],df.iat[i,6])]+=df.iat[i,8]
x1 = []
y1 = []
z1 = []
z1_text = []
for key in locations.keys():
if key[0] not in x1:
x1 += [key[0],]
if key[1] not in y1:
y1 += [key[1],]
for y in y1:
dummy = []
for x in x1:
if (x,y) in locations.keys():
dummy += [locations[(x,y)],]
else:
dummy += [0,]
z1 += [dummy,]
data = z1
arr = np.array(data)
fig, ax = plt.subplots()
ax.imshow(data, cmap='seismic')
for (i, j), z in np.ndenumerate(data):
ax.text(j, i, '{:f}'.format(z), ha='center', va='center')
ax.set_xticklabels(x1, rotation=90)
ax.set_yticklabels(y1)
#plt.show()
py.plot_mpl(fig)
I'm getting the following warning
Warning (from warnings module):
File "C:\Python27\lib\site-packages\plotly\matplotlylib\renderer.py", line 394
warnings.warn("Aw. Snap! You're gonna have to hold off on "
UserWarning: Aw. Snap! You're gonna have to hold off on the selfies for now. Plotly can't import images from matplotlib yet!
and finally the following error
Traceback (most recent call last):
File "E:\Project Kumbh\heatmap with annotations.py", line 58, in <module>
py.plot_mpl(fig)
File "C:\Python27\lib\site-packages\plotly\plotly\plotly.py", line 261, in plot_mpl
return plot(fig, **plot_options)
File "C:\Python27\lib\site-packages\plotly\plotly\plotly.py", line 155, in plot
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
File "C:\Python27\lib\site-packages\plotly\tools.py", line 1409, in return_figure_from_figure_or_data
if not figure['data']:
KeyError: 'data'
Is there anyway to get around this error? Or is there any simple way to make an annotated heatmap on plotly?
Edit
It's now possible to do it easily with plotly.figure_factory:
https://plot.ly/python/annotated_heatmap/
As far as I know, it is still not possible to convert Matplotlib's heatmaps into Plotly's though.
Aug 2015 Answer
Here's an example of making an annotated heatmap with the python api:
import plotly.plotly as py
import plotly.graph_objs as go
x = ['A', 'B', 'C', 'D', 'E']
y = ['W', 'X', 'Y', 'Z']
# x0 x1 x2 x3 x4
z = [[0.00, 0.00, 0.75, 0.75, 0.00], # y0
[0.00, 0.00, 0.75, 0.75, 0.00], # y1
[0.75, 0.75, 0.75, 0.75, 0.75], # y2
[0.00, 0.00, 0.00, 0.75, 0.00]] # y3
annotations = go.Annotations()
for n, row in enumerate(z):
for m, val in enumerate(row):
annotations.append(go.Annotation(text=str(z[n][m]), x=x[m], y=y[n],
xref='x1', yref='y1', showarrow=False))
colorscale = [[0, '#3D9970'], [1, '#001f3f']] # custom colorscale
trace = go.Heatmap(x=x, y=y, z=z, colorscale=colorscale, showscale=False)
fig = go.Figure(data=go.Data([trace]))
fig['layout'].update(
title="Annotated Heatmap",
annotations=annotations,
xaxis=go.XAxis(ticks='', side='top'),
yaxis=go.YAxis(ticks='', ticksuffix=' '), # ticksuffix is a workaround to add a bit of padding
width=700,
height=700,
autosize=False
)
print py.plot(fig, filename='Stack Overflow 31756636', auto_open=False) # https://plot.ly/~theengineear/5179
With the result at https://plot.ly/~theengineear/5179
Linking a related GitHub issue: https://github.com/plotly/python-api/issues/273
You have to use Plotly's declarative syntax, instead of converting from matplotlib to Python. Plotly only supports the matplotlib figure objects that it can reverse engineer, and unfortunately heatmaps aren't one of them. Here are the Plotly Python heatmap docs:
https://plot.ly/python/heatmaps/
And here are the Plotly Python annotation docs:
https://plot.ly/python/text-and-annotations/
Make sure to set the annotations to be referenced to the data rather than the page.
You could also overlay a scatter plot with a hover text field on the heatmap, but set the mode of the scatter plot to text. This would make only the text show and not the scatter plot points. Docs:
https://plot.ly/python/text-and-annotations/