I have a 1D array for some quantity, say T. I have data points (X, Y) to be plotted with errorbars and colorcoded with T.
I am plotting errorbars as:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import matplotlib
X = np.linspace(0, 10, 50)
Y = np.random.normal(0, 1, 50)
E = np.random.normal(0, 0.1, 50)
norm = matplotlib.colors.Normalize(vmin=0, vmax=0.5)
c_m = matplotlib.cm.jet
s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm)
s_m.set_array([])
plt.figure()
for i in range(0, len(Y)):
plt.errorbar(X[i], Y[i], color=s_m.to_rgba(E[i]), yerr=[E[i], E[i]], capsize=3, ls='none')
plt.grid()
plt.show()
This doesn't seem to work. Says :
err must be [ scalar | N, Nx1 or 2xN array-like ]
For plt.plot, if I have say N curves, each with M points, and I have to colorcode each curve by T, (dimensions: X[M], Y[N][M], T[N])
I do the following:
norm = matplotlib.colors.Normalize(
vmin=0,
vmax=32)
# choose a colormap
c_m = matplotlib.cm.jet
# create a ScalarMappable and initialize a data structure
s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm)
s_m.set_array([])
plt.figure()
for i in range(0, N):
plt.plot(X, Y[i], color=s_m.to_rgba(T[i]))
plt.grid()
plt.show()
This scheme works for plot! But does not seem to work with errorbar and 1D arrays.
However, I am not really sure how far the comparison is good since array dimensions are different in plot (2D) and errorbar (1D) case.
EDIT:
Got the solution. It does not relate to colorbar at all. Just that yerr array needs 2XN array.
Hence yerr=[[E[i]], [E[i]]] fixes it.
I still don't see the problem. Replacing plt.plot with plt.errorbar is working just fine:
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.cm
import numpy as np
N=3
x=np.arange(10)
Y = np.random.rand(len(x),N)
a = np.ones_like(x)*0.1
T = np.array([5,12,27])
plt.figure()
norm = matplotlib.colors.Normalize(vmin=0,vmax=32)
# choose a colormap
c_m = matplotlib.cm.jet
# create a ScalarMappable and initialize a data structure
s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm)
s_m.set_array([])
for i in range(0, N):
plt.errorbar(x, Y[:,i], yerr=[a,a], color=s_m.to_rgba(T[i]), capsize=3, ls='none')
plt.grid()
plt.grid()
plt.show()
Related
I am trying to generate symmetrical circular images using python, but I couldn't find an easy way to do it.
Here is an example sort of what I want:
So far, I listed the processes, but I couldn't find an easy way to implement them.:
Is there a simple way to do it?
I would use a polar plot, then it's just a matter of calculating the coordinates. For this, I am using np.linspace and I combine a division of the trigonometric circle, the same points shifted by 10°, and zeros:
import matplotlib.pyplot as plt
from itertools import chain
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.grid(False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
n = 6
# division of the circle
a = np.linspace(0, 2*np.pi, n+1)
# angles
theta = list(chain.from_iterable(zip(a, np.zeros(n+1), a+(np.pi/180*10))))
# distance to center
r = np.tile([1,0,1], n+1)
# plotting line
ax.plot(theta, r)
# plotting fill (needs an extra (0,0) point)
ax.fill(theta+[0], list(r)+[0], alpha=0.2)
example:
example with a loop
import matplotlib.pyplot as plt
import numpy as np
from itertools import chain
fig, axes = plt.subplots(ncols=4, nrows=3, subplot_kw={'projection': 'polar'})
axes = axes.flatten()
def plot_shape(ax, n=3, delta=np.pi/180*10):
ax.grid(False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
a = np.linspace(0, 2*np.pi, n+1)-delta/2
theta = list(chain.from_iterable(zip(a, np.zeros(n+1), a+delta)))
r = np.tile([1,0,1], n+1)
ax.plot(theta, r)
ax.fill(theta+[0], list(r)+[0], alpha=0.2)
for i in range(len(axes)):
axes[i].set_title(f'n = {i+1}')
plot_shape(axes[i], n=i+1)
plt.tight_layout()
I am trying to vary opacity on each marker in a 3D scatter plot. I hit an issue with this where it was saying the color was not the correct length (even though it was). Moving to the simplest case it can be recreated with this.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
elements = 100
x = np.linspace(0, 10, elements)
ax.scatter(x, x, x, alpha=np.linspace(1, 0.5, elements), c=['b' for _ in range(elements)], label='label')
fig.legend()
plt.show()
If the label keyword argument is removed it works. If you do a 2D scatter plot it works regardless of the label. I am wondering if this is a bug but wanted to see if I was missing something first.
To accomplish what you want, try to use set_facecolors instead of directly using the alpha and color in the scatter plot.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
elements = 200
z = np.linspace(0, 2*np.pi, elements)
x = np.cos(z)
y = np.sin(z)
h = ax.scatter(x, y, z, label='label')
h.set_facecolors([[0., 0., 1., alpha] for alpha in np.linspace(0.1, 0.7, len(x))])
fig.legend()
plt.show()
I have a data set with a small sample size of data. For example:
My code looks something like this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf
df=pd.read_csv('test.csv')
df.head()
extent = x_extent = x_min, x_max, y_min, y_max = [df["X"].min()-1000, df["X"].max()+1000, df["Y"].min()-1000, df["Y"].min()+1000]
grid_x, grid_y = np.mgrid[x_min:x_max:100, y_min:y_max:100]
rbfi=Rbf(df["X"], df["Y"], df["Total"])
di=rbfi(grid_x, grid_y)
plt.scatter(grid_x, grid_y, s=10)
plt.figure(figsize=(15,15))
plt.imshow(di.T, origin="lower", extent=extent)
c2 = plt.scatter(df["X"], df["Y"], s=60, c=df["Total"], edgecolor='#ffffff66')
plt.colorbar(c2, shrink=0.6)
plt.show()
the result:
The result is a scatter plot of my points that appear to be in the correct place, but the interpolated grid is not covering the scatter points. So I think this has something to do with my origin not being correct, but I don't know how to fix this.
Two approaches here, one with a Delaunay triangulation, the other using the Radial Basis Function. Snippet and figure below.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
from scipy.interpolate import Rbf
rng = np.random.default_rng()
X = rng.random(size=(15))
Y = rng.random(size=(15))
Total = rng.random(size=(15))
fig, (ax, bx) = plt.subplots(nrows=1, ncols=2, num=0, figsize=(16, 8))
tri = Triangulation(X, Y)
tctrf = ax.tricontourf(tri, Total)
gridY, gridX = np.mgrid[np.amin(Y):np.amax(Y):100 * 1j,
np.amin(X):np.amax(X):100 * 1j]
rbfi = Rbf(X, Y, Total, function='linear')
iTotal = rbfi(gridX, gridY)
bx.contourf(gridX, gridY, iTotal)
scat = ax.scatter(X, Y, s=60, c=Total, edgecolor='black')
fig.colorbar(scat, ax=ax)
scat = bx.scatter(X, Y, s=60, c=Total, edgecolor='black')
fig.colorbar(scat, ax=bx)
ax.set_aspect('equal')
bx.set_aspect('equal')
fig.tight_layout()
fig.savefig('so.png')
plt.show()
I haven't found an answer to this yet: I have a grid defined in a text file with four columns: (lon,lat,depth,slip). Each row is a grid point.
I can generate a scatter plot of these points using the following simple code:
# Main imports:
import numpy as np
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
# Read the grid:
points = np.loadtxt("grid.txt")
# Retrieve parameters from the grid:
lon = points[:,0]
lat = points[:,1]
depth = points[:,2]
slip = points[:,3]
# 3-D plot of the model:
fig = figure(1)
ax = fig.add_subplot(111, projection='3d')
p = ax.scatter(lon, lat, depth, c=slip, vmin=0, vmax=max(slip), s=30, edgecolor='none', marker='o')
fig.colorbar(p)
title("Published finite fault in 3-D")
ax.set_xlabel("Longitude [degrees]")
ax.set_ylabel("Latitude [degrees]")
ax.set_zlabel("Depth [km]")
ax.invert_zaxis()
jet()
grid()
show()
And I get the following figure:
What I want to do is to be able to interpolate those points to create a "continuous" surface grid and plot it in both 2-D and 3-D plots. Therefore, somehow I've to consider all (lon,lat,depth,slip) in the interpolation. I'd appreciate your suggestions. Thanks in advance!
I'm a bit late, but if your data grid is properly ordered, you could resolve your iusse using plot_surface reshaping your 1D data to 2D.
An example supposing you're using a 10x10 grid:
# Main imports:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
# Set the fourth dimension`
color_dimension = slip.reshape(10,10)
# normalize the colours
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
# color map
m = plt.cm.ScalarMappable(norm=norm, cmap='hot')
m.set_array([])
fcolors = m.to_rgba(color_dimension)
# plot
fig = plt.figure()
ax = fig.gca(projection='3d')
#reshape 1D data to 2D
g=ax.plot_surface(lat.reshape(10, 10), lon.reshape(10, 10), depth.reshape(10, 10), cmap='hot',rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
cbar=fig.colorbar(g,aspect=50)
cbar.set_label('slip', rotation=270, fontsize=14)
title("Published finite fault in 3-D")
ax.set_xlabel("Longitude [degrees]")
ax.set_ylabel("Latitude [degrees]")
ax.set_zlabel("Depth [km]")
ax.invert_zaxis()
plt.show()
I have a range of points x and y stored in numpy arrays.
Those represent x(t) and y(t) where t=0...T-1
I am plotting a scatter plot using
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.show()
I would like to have a colormap representing the time (therefore coloring the points depending on the index in the numpy arrays)
What is the easiest way to do so?
Here is an example
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(100)
y = np.random.rand(100)
t = np.arange(100)
plt.scatter(x, y, c=t)
plt.show()
Here you are setting the color based on the index, t, which is just an array of [1, 2, ..., 100].
Perhaps an easier-to-understand example is the slightly simpler
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = x
t = x
plt.scatter(x, y, c=t)
plt.show()
Note that the array you pass as c doesn't need to have any particular order or type, i.e. it doesn't need to be sorted or integers as in these examples. The plotting routine will scale the colormap such that the minimum/maximum values in c correspond to the bottom/top of the colormap.
Colormaps
You can change the colormap by adding
import matplotlib.cm as cm
plt.scatter(x, y, c=t, cmap=cm.cmap_name)
Importing matplotlib.cm is optional as you can call colormaps as cmap="cmap_name" just as well. There is a reference page of colormaps showing what each looks like. Also know that you can reverse a colormap by simply calling it as cmap_name_r. So either
plt.scatter(x, y, c=t, cmap=cm.cmap_name_r)
# or
plt.scatter(x, y, c=t, cmap="cmap_name_r")
will work. Examples are "jet_r" or cm.plasma_r. Here's an example with the new 1.5 colormap viridis:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='viridis')
ax2.scatter(x, y, c=t, cmap='viridis_r')
plt.show()
Colorbars
You can add a colorbar by using
plt.scatter(x, y, c=t, cmap='viridis')
plt.colorbar()
plt.show()
Note that if you are using figures and subplots explicitly (e.g. fig, ax = plt.subplots() or ax = fig.add_subplot(111)), adding a colorbar can be a bit more involved. Good examples can be found here for a single subplot colorbar and here for 2 subplots 1 colorbar.
To add to wflynny's answer above, you can find the available colormaps here
Example:
import matplotlib.cm as cm
plt.scatter(x, y, c=t, cmap=cm.jet)
or alternatively,
plt.scatter(x, y, c=t, cmap='jet')
Subplot Colorbar
For subplots with scatter, you can trick a colorbar onto your axes by building the "mappable" with the help of a secondary figure and then adding it to your original plot.
As a continuation of the above example:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='viridis')
ax2.scatter(x, y, c=t, cmap='viridis_r')
# Build your secondary mirror axes:
fig2, (ax3, ax4) = plt.subplots(1, 2)
# Build maps that parallel the color-coded data
# NOTE 1: imshow requires a 2-D array as input
# NOTE 2: You must use the same cmap tag as above for it match
map1 = ax3.imshow(np.stack([t, t]),cmap='viridis')
map2 = ax4.imshow(np.stack([t, t]),cmap='viridis_r')
# Add your maps onto your original figure/axes
fig.colorbar(map1, ax=ax1)
fig.colorbar(map2, ax=ax2)
plt.show()
Note that you will also output a secondary figure that you can ignore.
Single colorbar for multiple subplots
sometimes it is preferable to have a single colorbar to indicate data values visualised on multiple subplots.
In this case, a Normalize() object needs to be created using the minimum and maximum data values across both plots.
Then a colorbar object can be created from a ScalarMappable() object, which maps between scalar values and colors.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y = x
t1 = x # Colour data for first plot
t2 = 2*x # Color data for second plot
all_data = np.concatenate([t1, t2])
# Create custom Normalise object using the man and max data values across both subplots to ensure colors are consistent on both plots
norm = plt.Normalize(np.min(all_data), np.max(all_data))
fig, axs = plt.subplots(1, 2)
axs[0].scatter(x, y, c=t1, cmap='viridis', norm=norm)
axs[1].scatter(x**2, y, c=t2, cmap='viridis', norm=norm)
# Create the colorbar
smap = plt.cm.ScalarMappable(cmap='viridis', norm=norm)
cbar = fig.colorbar(smap, ax=axs, fraction=0.1, shrink = 0.8)
cbar.ax.tick_params(labelsize=11)
cbar.ax.set_ylabel('T', rotation=0, labelpad = 15, fontdict = {"size":14})
plt.show()
subplots_colorbar