Plotting Tips and Tricks - nstarman/utilipy GitHub Wiki
https://python-graph-gallery.com
See the starkplot documentation.
Answer from stack overflow.
You can do this easily with a matplotlib AxisDivider.
The example from the linked page also works without using subplots:
import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable import numpy as np plt.figure() ax = plt.gca() im = ax.imshow(np.arange(100).reshape((10,10))) # create an axes on the right side of ax. The width of cax will be 5% # of ax and the padding between cax and ax will be fixed at 0.05 inch. divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) plt.colorbar(im, cax=cax)
If you have a collection of points and want to find a closed curve containing all the points, a simple solution is called a Convex Hull. Wikipedia has lots to say on this and scipy has a code to solve this problem!
As example from scipy,
from scipy.spatial import ConvexHull, convex_hull_plot_2d points = np.random.rand(30, 2) # 30 random points in 2-D hull = ConvexHull(points) # # plot # import matplotlib.pyplot as plt # plt.plot(points[:,0], points[:,1], 'o') # for simplex in hull.simplices: # plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
If the data is "convex" then this works well. If the data has any "concavities", then a convex hull will skip over them. For instance, the upper right corner of the above plot doesn't really capture the shape. For this we move onto Concave Hulls...
Unfortunately, there is no unique solution to the concave hull problem.
Some solution codes:
import alphashape import matplotlib.pyplot as plt from descartes import PolygonPatch points = [(17, 158),(15, 135),(38, 183),(43, 19),(93, 88),(96, 140),(149, 163),(128, 248),(216, 265),(248, 210),(223, 167),(256, 151),(331, 214),(340, 187),(316, 53),(298, 35),(182, 0),(121, 42)] alpha = 0.95 * alphashape.optimizealpha(points) hull = alphashape.alphashape(points, alpha) hull_pts = hull.exterior.coords.xy fig, ax = plt.subplots() ax.scatter(hull_pts[0], hull_pts[1], color='red') ax.add_patch(PolygonPatch(hull, fill=False, color='green'))
# Libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import kde
# Create data: 200 points
data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)
x, y = data.T
# Create a figure with 6 plot areas
fig, axes = plt.subplots(ncols=6, nrows=1, figsize=(21, 5))
# Everything sarts with a Scatterplot
axes[0].set_title('Scatterplot')
axes[0].plot(x, y, 'ko')
# As you can see there is a lot of overplottin here!
# Thus we can cut the plotting window in several hexbins
nbins = 20
axes[1].set_title('Hexbin')
axes[1].hexbin(x, y, gridsize=nbins, cmap=plt.cm.BuGn_r)
# 2D Histogram
axes[2].set_title('2D Histogram')
axes[2].hist2d(x, y, bins=nbins, cmap=plt.cm.BuGn_r)
# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
k = kde.gaussian_kde(data.T)
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
# plot a density
axes[3].set_title('Calculate Gaussian KDE')
axes[3].pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.BuGn_r)
# add shading
axes[4].set_title('2D Density with shading')
axes[4].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
# contour
axes[5].set_title('Contour')
axes[5].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
axes[5].contour(xi, yi, zi.reshape(xi.shape) )