Format of legend with equation in matplotlib - python

How do we show an equation in the legend of a pyplot plot?
Sample code below:
fig = plt.figure(figsize=(5, 3), dpi=my_dpi)
ax = fig.add_subplot(111)
a = 2.0
b = 3.0
xdata = np.arange(6)
ydata = a*np.power(xdata,b)
# I want the b variable in the equation below to be a subscript.
label = '${0:.2f} x^{{0:.2f}}$'.format(a,b)
plt.plot(xdata,ydata, alpha = 0.5, label = label)
plt.legend()
plt.show()

One possible solution: first format your float to strings, than put the strings together:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5, 3))
ax = fig.add_subplot(111)
a = 2.0
b = 3.0
xdata = np.arange(6)
ydata = a*np.power(xdata,b)
f2str = '{:.2f}'
label = '$%s x^{%s}$' % tuple(f2str.format(t) for t in [a, b])
plt.plot(xdata,ydata, alpha = 0.5, label = label)
plt.legend()
plt.show()

Related

How to reduce the gap between a pcolormesh and a colorbar in matplotlib?

I have a dataset that I want to plot as 4 panels (each a pcolormesh with its associated colorbar). This is the code I'm using to do this, with some mocked up data
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
xs = np.linspace(0.1, 0.2, 100)
ys = np.linspace(0, 2*np.pi*0.1, 400)
x_mesh, y_mesh = np.meshgrid(xs, ys)
# mocked up data arrays
A = np.full_like(x_mesh, 1.0)
B = np.full_like(x_mesh, 1.0)
C = np.full_like(x_mesh, 1.0)
D = np.full_like(x_mesh, 1.0)
fig = plt.figure()
gs = gridspec.GridSpec(nrows = 2, ncols = 4, height_ratios = (0.5, 0.5), width_ratios = (0.45, 0.05, 0.45, 0.05))
ax0 = fig.add_subplot(gs[0,0])
ax0_cbar = fig.add_subplot(gs[0,1])
ax1 = fig.add_subplot(gs[0,2])
ax1_cbar = fig.add_subplot(gs[0,3])
ax2 = fig.add_subplot(gs[1,0])
ax2_cbar = fig.add_subplot(gs[1,1])
ax3 = fig.add_subplot(gs[1,2])
ax3_cbar = fig.add_subplot(gs[1,3])
a = ax0.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, A, \
shading = 'auto')
cb1 = plt.colorbar(a, cax=ax0_cbar)
cb1.set_label(r"A")
b = ax1.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, B, \
shading = 'auto')
cb1 = plt.colorbar(b, cax=ax1_cbar)
cb1.set_label(r"B")
c = ax2.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, C, \
shading = 'auto')
cb1 = plt.colorbar(c, cax=ax2_cbar)
cb1.set_label(r"C")
d = ax3.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, D, \
shading = 'auto')
cb1 = plt.colorbar(d, cax=ax3_cbar)
cb1.set_label(r"D")
ax0.xaxis.set_ticklabels([])
ax1.xaxis.set_ticklabels([])
fig.tight_layout()
But when I actually do this, I find that there are really large gaps between the pcolormesh and the colorbars that are really unappealing (picture attached). How can I reduce these? I though I would be able to do it with fig.tight_layout() and width_ratios in gridspec
You don't require new axes elements for your colorbars, simply use the ax keyword argument to specify the colorbars for each subplot. The matplotlib documentation shows that using ax will produce a colorbar axis stolen from the parent axes ax (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.colorbar.html). The documentation should be your first port of call, always!
Here I have written a working version of your code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
xs = np.linspace(0.1, 0.2, 100)
ys = np.linspace(0, 2*np.pi*0.1, 400)
x_mesh, y_mesh = np.meshgrid(xs, ys)
# mocked up data arrays
A = np.full_like(x_mesh, 1.0)
B = np.full_like(x_mesh, 1.0)
C = np.full_like(x_mesh, 1.0)
D = np.full_like(x_mesh, 1.0)
fig = plt.figure(figsize=(6,4))
gs = gridspec.GridSpec(nrows = 2, ncols = 2, height_ratios = (0.5, 0.5), width_ratios = (0.5, 0.5))
ax0 = fig.add_subplot(gs[0,0])
ax1 = fig.add_subplot(gs[0,1])
ax2 = fig.add_subplot(gs[1,0])
ax3 = fig.add_subplot(gs[1,1])
a = ax0.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, A, \
shading = 'auto')
cb1 = plt.colorbar(a, ax=ax0)
cb1.set_label(r"A")
b = ax1.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, B, \
shading = 'auto')
cb1 = plt.colorbar(b, ax=ax1)
cb1.set_label(r"B")
c = ax2.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, C, \
shading = 'auto')
cb1 = plt.colorbar(c, ax=ax2)
cb1.set_label(r"C")
d = ax3.pcolormesh(x_mesh/1.0e-2, y_mesh/1.0e-2, D, \
shading = 'auto')
cb1 = plt.colorbar(d, ax=ax3)
cb1.set_label(r"D")
ax0.xaxis.set_ticklabels([])
ax1.xaxis.set_ticklabels([])
fig.tight_layout()
plt.show()
Happy coding
The approach above is correct. It can break down if you have equal aspect axes, for which you can now use layout='compressed' for simple cases to remove white space:
fig, axs = plt.subplots(2, 2, layout='compressed', figsize=(6, 3))
for ax in axs.flat:
pc = ax.pcolormesh(np.random.randn(10, 10))
ax.set_aspect(1)
fig.colorbar(pc, ax=ax)
plt.show()
See also: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/colorbar_placement.html

how do i display the label that more than 50% accuracy in line chart using python?

fig = plt.figure(figsize=[5, 5])
plt.plot(data["recall"])
plt.title('256 Classes Performance')
plt.xlabel('class')
plt.ylabel('Accuracy Rate')
plt.show()
image 1 is my image, image 2 is what I want, I want to add labels for those classes which more than 50% accuracy, including class number and accuracy rate displaying in the line chart
Maybe like so:
import matplotlib.pyplot as plt
import numpy as np
my_vals = np.random.rand(50)
categories = np.arange(50)
colors = ['red' if v >= 0.5 else 'green' for v in my_vals]
fig, ax = plt.subplots(figsize=(5, 4))
ax.bar(categories, my_vals, color=colors)
plt.show()
You can use a combination of ax.annotate and ax.scatter. With xytext you can move the text (see matplotlib.pyplot.annotate).
import matplotlib.pyplot as plt
import numpy as np
from numpy.lib.financial import pmt
y_vals = np.random.rand(50)
x_vals = np.arange(50)
annotations = [f'{y:.1f}' if y >= 0.5 else '' for y in y_vals]
dots = [y if y >= 0.5 else 9999.0 for y in y_vals]
fig, ax = plt.subplots(figsize=(5, 4))
ax.set_ylim(-0.1, 1.1)
ax.plot(x_vals, y_vals)
ax.scatter(x_vals, dots, color='red')
for x, y, text_val in zip(x_vals, y_vals, annotations):
ax.annotate(
text_val,
xy=(x, y),
)
plt.show()

Remove lowest color from colorbar in Seaborn/Matplotlib

If I set shade_lowest = False, the colorbar still contains the lowest level (purple-ish). Is there any generic way to remove it entirely?
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
a = np.random.normal(0, 1, 100)
b = np.random.normal(0, 1, 100)
fig, ax = plt.subplots()
sns.kdeplot(a, b, shade = True, shade_lowest = False, cmap = "viridis", cbar = True, n_levels = 4, ax = ax)
plt.show()
A solution is for sure to not create this level from the beginning.
Here we choose maximally 5 levels according to a locator and remove the lowest one when calling the contourf plot, such that this level does not even exist in the first place. Then the automatic colorbar creation works flawlessly.
import numpy as np; np.random.seed(5)
import matplotlib.pyplot as plt
from matplotlib import ticker
from scipy import stats
x = np.random.normal(3, 1, 100)
y = np.random.normal(0, 2, 100)
X, Y = np.mgrid[x.min():x.max():100j, y.min():y.max():100j]
positions = np.vstack([X.ravel(),Y.ravel()])
values = np.vstack([x,y])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
N=4
locator = ticker.MaxNLocator(N + 1, min_n_ticks=N)
lev = locator.tick_values(Z.min(), Z.max())
fig, ax = plt.subplots()
c = ax.contourf(X,Y,Z,levels=lev[1:])
ax.scatter(x,y, s=9, c="k")
fig.colorbar(c)
plt.show()

Custom scale for radial contour plot in matplotlib

I have a sample script to generate a polar contour plot in matplotlib:
import os
import math
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist.floating_axes as floating_axes
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist.grid_finder import FixedLocator, MaxNLocator, DictFormatter
import random
# ------------------------------------ #
def setup_arc_radial_axes(fig, rect, angle_ticks, radius_ticks, min_rad, max_rad):
tr = PolarAxes.PolarTransform()
pi = np.pi
grid_locator1 = FixedLocator([v for v, s in angle_ticks])
tick_formatter1 = DictFormatter(dict(angle_ticks))
grid_locator2 = FixedLocator([a for a, b in radius_ticks])
tick_formatter2 = DictFormatter(dict(radius_ticks))
grid_helper = floating_axes.GridHelperCurveLinear(tr,
extremes=((370.0*(pi/180.0)), (170.0*(pi/180.0)), max_rad, min_rad),
grid_locator1=grid_locator1,
grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1,
tick_formatter2=tick_formatter2,
)
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
ax1.grid(True)
# create a parasite axes whose transData in RA, cz
aux_ax = ax1.get_aux_axes(tr)
aux_ax.patch = ax1.patch
ax1.patch.zorder=0.9
#ax1.axis["left"].set_ticklabel_direction("+")
return ax1, aux_ax
# ------------------------------------ #
# write angle values to the plotting array
angles = []
for mic_num in range(38):
angle = float(mic_num)*(180.0/36.0)*(math.pi/180.0)+math.pi
angles.append(angle)
# ------------------------------------ #
### these are merely the ticks that appear on the plot axis
### these don't actually get plotted
angle_ticks = range(0,190,10)
angle_ticks_rads = [a*math.pi/180.0 for a in angle_ticks]
angle_ticks_rads_plus_offset = [a+math.pi for a in angle_ticks_rads]
angle_ticks_for_plot = []
for i in range(len(angle_ticks)):
angle_ticks_for_plot.append((angle_ticks_rads_plus_offset[i],r"$"+str(angle_ticks[i])+"$"))
# ------------------------------------ #
scale = 1.0
aspect = 1.50
height = 8.0
fig = plt.figure(1, figsize=(height*aspect*scale, height*scale))
fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95, top=0.84)
fig.subplots_adjust()
plot_real_min = 30.0
plot_real_max = 100.0
plot_fake_min = 0.0
plot_fake_max = 5000.0
rad_tick_increment = 500.0
radius_ticks = []
for i in range(int(plot_fake_min),int(plot_fake_max)+int(rad_tick_increment),int(rad_tick_increment)):
plot_fake_val = ((i-plot_fake_min)/(plot_fake_max-plot_fake_min))*(plot_real_max-plot_real_min)+plot_real_min
radius_ticks.append((plot_fake_val, r"$"+str(i)+"$"))
ax2, aux_ax2 = setup_arc_radial_axes(fig, 111, angle_ticks_for_plot, radius_ticks, plot_real_min, plot_real_max)
azimuths = np.radians(np.linspace(0, 180, 91))
azimuths_adjusted = [ (x + math.pi) for x in azimuths ]
zeniths = np.arange(0, 5050, 50)
zeniths_adjusted = [((x-plot_fake_min)/(plot_fake_max-plot_fake_min))*(plot_real_max-plot_real_min)+plot_real_min for x in zeniths]
r, theta = np.meshgrid(zeniths_adjusted, azimuths_adjusted)
values = 90.0+5.0*np.random.random((len(azimuths), len(zeniths)))
aux_ax2.contourf(theta, r, values)
cbar = plt.colorbar(aux_ax2.contourf(theta, r, values), orientation='vertical')
cbar.ax.set_ylabel('Contour Value [Unit]', fontsize = 16)
plt.suptitle('Plot Title ', fontsize = 24, weight="bold")
plt.legend(loc=3,prop={'size':20})
plt.xlabel('Angle [deg]', fontsize=20, weight="bold")
plt.ylabel('Frequency [Hz]', fontsize=20, weight="bold")
# plt.show()
plt.savefig('test.png', dpi=100)
plt.close()
This script will generate a plot that looks something like:
My question is how can I plot with an alternate color bar scale? Is it possible to define a custom scale?
Something like a blue-white-red scale where deltas around a central value can easily be shown would be the best, something like:
You can create a custom scale, but matplotlib already has what you want. All you have to do is add an argument to contourf:
aux_ax2.contourf(theta, r, values, cmap = 'bwr')
If you don't like bwr, coolwarm and seismic are also blue to red. If you need to reverse the scale, just add _r to the colormap name. You can find more colormaps here: http://matplotlib.org/examples/color/colormaps_reference.html
I can't run your code, but I think you could solve your problem this way:
from matplotlib import pyplot as plt
import matplotlib as mpl
f = plt.figure(figsize=(5,10))
ax = f.add_axes([0.01, 0.01, 0.4, 0.95])
#here we create custom colors
cmap = mpl.colors.LinearSegmentedColormap.from_list(name='Some Data',colors=['b', 'w','w', 'r'])
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, orientation='vertical')
cb.set_label('Some Data')
plt.show()
And if linear way is not what you are looking for here is some other types:
http://matplotlib.org/api/colors_api.html#module-matplotlib.colors

Matplotlib imshow/matshow display values on plot

I am trying to create a 10x10 grid using either imshow or matshow in Matplotlib. The function below takes a numpy array as input, and plots the grid. However, I'd like to have values from the array also displayed inside the cells defined by the grid. So far I could not find a proper way to do it. I can use plt.text to place things over the grid, but this requires coordinates of each cell, totally inconvenient. Is there a better way to do what I am trying to accomplish?
Thanks!
NOTE: The code below does not take the values from the array yet, I was just playing with plt.text.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
board = np.zeros((10, 10))
def visBoard(board):
cmap = colors.ListedColormap(['white', 'red'])
bounds=[0,0.5,1]
norm = colors.BoundaryNorm(bounds, cmap.N)
plt.figure(figsize=(4,4))
plt.matshow(board, cmap=cmap, norm=norm, interpolation='none', vmin=0, vmax=1)
plt.xticks(np.arange(0.5,10.5), [])
plt.yticks(np.arange(0.5,10.5), [])
plt.text(-0.1, 0.2, 'x')
plt.text(0.9, 0.2, 'o')
plt.text(1.9, 0.2, 'x')
plt.grid()
visBoard(board)
Output:
Can you do something like:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
min_val, max_val = 0, 10
ind_array = np.arange(min_val + 0.5, max_val + 0.5, 1.0)
x, y = np.meshgrid(ind_array, ind_array)
for i, (x_val, y_val) in enumerate(zip(x.flatten(), y.flatten())):
c = 'x' if i%2 else 'o'
ax.text(x_val, y_val, c, va='center', ha='center')
#alternatively, you could do something like
#for x_val, y_val in zip(x.flatten(), y.flatten()):
# c = 'x' if (x_val + y_val)%2 else 'o'
ax.set_xlim(min_val, max_val)
ax.set_ylim(min_val, max_val)
ax.set_xticks(np.arange(max_val))
ax.set_yticks(np.arange(max_val))
ax.grid()
Edit:
Here is an updated example with an imshow background.
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
min_val, max_val, diff = 0., 10., 1.
#imshow portion
N_points = (max_val - min_val) / diff
imshow_data = np.random.rand(N_points, N_points)
ax.imshow(imshow_data, interpolation='nearest')
#text portion
ind_array = np.arange(min_val, max_val, diff)
x, y = np.meshgrid(ind_array, ind_array)
for x_val, y_val in zip(x.flatten(), y.flatten()):
c = 'x' if (x_val + y_val)%2 else 'o'
ax.text(x_val, y_val, c, va='center', ha='center')
#set tick marks for grid
ax.set_xticks(np.arange(min_val-diff/2, max_val-diff/2))
ax.set_yticks(np.arange(min_val-diff/2, max_val-diff/2))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlim(min_val-diff/2, max_val-diff/2)
ax.set_ylim(min_val-diff/2, max_val-diff/2)
ax.grid()
plt.show()
For your graph you should should try with pyplot.table:
import matplotlib.pyplot as plt
import numpy as np
board = np.zeros((10, 10))
board[0,0] = 1
board[0,1] = -1
board[0,2] = 1
def visBoard(board):
data = np.empty(board.shape,dtype=np.str)
data[:,:] = ' '
data[board==1.0] = 'X'
data[board==-1.0] = 'O'
plt.axis('off')
size = np.ones(board.shape[0])/board.shape[0]
plt.table(cellText=data,loc='center',colWidths=size,cellLoc='center',bbox=[0,0,1,1])
plt.show()
visBoard(board)
Some elaboration on the code of #wflynny making it into a function that takes any matrix no matter what size and plots its values.
import numpy as np
import matplotlib.pyplot as plt
cols = np.random.randint(low=1,high=30)
rows = np.random.randint(low=1,high=30)
X = np.random.rand(rows,cols)
def plotMat(X):
fig, ax = plt.subplots()
#imshow portion
ax.imshow(X, interpolation='nearest')
#text portion
diff = 1.
min_val = 0.
rows = X.shape[0]
cols = X.shape[1]
col_array = np.arange(min_val, cols, diff)
row_array = np.arange(min_val, rows, diff)
x, y = np.meshgrid(col_array, row_array)
for col_val, row_val in zip(x.flatten(), y.flatten()):
c = '+' if X[row_val.astype(int),col_val.astype(int)] < 0.5 else '-'
ax.text(col_val, row_val, c, va='center', ha='center')
#set tick marks for grid
ax.set_xticks(np.arange(min_val-diff/2, cols-diff/2))
ax.set_yticks(np.arange(min_val-diff/2, rows-diff/2))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlim(min_val-diff/2, cols-diff/2)
ax.set_ylim(min_val-diff/2, rows-diff/2)
ax.grid()
plt.show()
plotMat(X)

Categories

Resources