I'm trying to create a grid using a matplotlib function like imshow.
From this array:
[[ 1 8 13 29 17 26 10 4],
[16 25 31 5 21 30 19 15]]
I would like to plot the value as a color AND the text value itself (1,2, ...) on the same grid. This is what I have for the moment (I can only plot the color associated to each value):
from matplotlib import pyplot
import numpy as np
grid = np.array([[1,8,13,29,17,26,10,4],[16,25,31,5,21,30,19,15]])
print 'Here is the array'
print grid
fig1, (ax1, ax2)= pyplot.subplots(2, sharex = True, sharey = False)
ax1.imshow(grid, interpolation ='none', aspect = 'auto')
ax2.imshow(grid, interpolation ='bicubic', aspect = 'auto')
pyplot.show()
You want to loop over the values in grid, and use ax.text to add the label to the plot.
Fortunately, for 2D arrays, numpy has ndenumerate, which makes this quite simple:
for (j,i),label in np.ndenumerate(grid):
ax1.text(i,j,label,ha='center',va='center')
ax2.text(i,j,label,ha='center',va='center')
If for any reason you have to use a different extent from the one that is provided naturally by imshow the following method (even if more contrived) does the job:
size = 4
data = np.arange(size * size).reshape((size, size))
# Limits for the extent
x_start = 3.0
x_end = 9.0
y_start = 6.0
y_end = 12.0
extent = [x_start, x_end, y_start, y_end]
# The normal figure
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
im = ax.imshow(data, extent=extent, origin='lower', interpolation='None', cmap='viridis')
# Add the text
jump_x = (x_end - x_start) / (2.0 * size)
jump_y = (y_end - y_start) / (2.0 * size)
x_positions = np.linspace(start=x_start, stop=x_end, num=size, endpoint=False)
y_positions = np.linspace(start=y_start, stop=y_end, num=size, endpoint=False)
for y_index, y in enumerate(y_positions):
for x_index, x in enumerate(x_positions):
label = data[y_index, x_index]
text_x = x + jump_x
text_y = y + jump_y
ax.text(text_x, text_y, label, color='black', ha='center', va='center')
fig.colorbar(im)
plt.show()
If you want to put other type of data and not necessarily the values that you used for the image you can modify the script above in the following way (added values after data):
size = 4
data = np.arange(size * size).reshape((size, size))
values = np.random.rand(size, size)
# Limits for the extent
x_start = 3.0
x_end = 9.0
y_start = 6.0
y_end = 12.0
extent = [x_start, x_end, y_start, y_end]
# The normal figure
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
im = ax.imshow(data, extent=extent, origin='lower', interpolation='None', cmap='viridis')
# Add the text
jump_x = (x_end - x_start) / (2.0 * size)
jump_y = (y_end - y_start) / (2.0 * size)
x_positions = np.linspace(start=x_start, stop=x_end, num=size, endpoint=False)
y_positions = np.linspace(start=y_start, stop=y_end, num=size, endpoint=False)
for y_index, y in enumerate(y_positions):
for x_index, x in enumerate(x_positions):
label = values[y_index, x_index]
text_x = x + jump_x
text_y = y + jump_y
ax.text(text_x, text_y, label, color='black', ha='center', va='center')
fig.colorbar(im)
plt.show()
Related
I've created a plot of normal distribution like this:
fig, ax = plt.subplots()
ax.set_title('Плотнось распределения вероятности')
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
x = np.linspace(148, 200, 100) # X от 148 до 200
y = (1 / (5 * math.sqrt(2*math.pi))) * np.exp((-(x-178)**2) / (2*5**2))
ax.plot(x, y)
plt.show()
But I also need to add vertical lines inside the graph area, color inner segments and add marks like in picture on axis = 0.
How can I do it in python using matplotlib?
I've tried to use plt.axvline, but the vertical lines go outside of my main plot:
plt.axvline(x = 178, color = 'g', label = 'axvline - full height')
plt.axvline(x = 178+5, color = 'b', label = 'axvline - full height')
plt.axvline(x = 178-5, color = 'b', label = 'axvline - full height')
plt.axvline(x = 178+5*2, color = 'r', label = 'axvline - full height')
plt.axvline(x = 178-5*2, color = 'r', label = 'axvline - full height')
The line version can be implemented using vlines, but note that your reference figure can be better reproduced using fill_between.
Line version
Instead of axvline, use vlines which supports ymin and ymax bounds.
Change your y into a lambda f(x, mu, sd) and use that to define the ymax bounds:
# define y as a lambda f(x, mu, sd)
f = lambda x, mu, sd: (1 / (sd * (2*np.pi)**0.5)) * np.exp((-(x-mu)**2) / (2*sd**2))
fig, ax = plt.subplots(figsize=(8, 3))
x = np.linspace(148, 200, 200)
mu = 178
sd = 5
ax.plot(x, f(x, mu, sd))
# define 68/95/99 locations and colors
xs = mu + sd*np.arange(-3, 4)
colors = [*'yrbgbry']
# draw lines at 68/95/99 points from 0 to the curve
ax.vlines(xs, ymin=0, ymax=[f(x, mu, sd) for x in xs], color=colors)
# relabel x ticks
plt.xticks(xs, [f'${n}\sigma$' if n else '0' for n in range(-3, 4)])
Shaded version
Use fill_between to better recreate the sample figure. Define the shaded bounds using the where parameter:
fig, ax = plt.subplots(figsize=(8, 3))
x = np.linspace(148, 200, 200)
mu = 178
sd = 5
y = (1 / (sd * (2*np.pi)**0.5)) * np.exp((-(x-mu)**2) / (2*sd**2))
ax.plot(x, y)
# use `where` condition to shade bounded regions
bounds = mu + sd*np.array([-np.inf] + list(range(-3, 4)) + [np.inf])
alphas = [0.1, 0.2, 0.5, 0.8, 0.8, 0.5, 0.2, 0.1]
for left, right, alpha in zip(bounds, bounds[1:], alphas):
ax.fill_between(x, y, where=(x >= left) & (x < right), color='b', alpha=alpha)
# relabel x ticks
plt.xticks(bounds[1:-1], [f'${n}\sigma$' if n else '0' for n in range(-3, 4)])
To label the region percentages, add text objects at the midpoints of the bounded regions:
midpoints = mu + sd*np.arange(-3.5, 4)
percents = [0.1, 2.1, 13.6, 34.1, 34.1, 13.6, 2.1, 0.1]
colors = [*'kkwwwwkk']
for m, p, c in zip(
midpoints, # midpoints of bounded regions
percents, # percents captured by bounded regions
colors, # colors of text labels
):
ax.text(m, 0.01, f'{p}%', color=c, ha='center', va='bottom')
Having the following line in my plot code
ax.plot(x, pdf_individual, '--k', label = "single Gaussians")
, with pdf_individual being a list of lists, results in this picture:
Is there a way to just have "single Gaussians" once in the labels, instead of 6 times, which is the amount of single Gaussians for the Gaussian Mixture Model?
This is the whole post with the suggested solution
import matplotlib as mpl
import matplotlib.ticker as mtick
from matplotlib.lines import Line2D
mpl.rcParams['figure.dpi'] = 600
test_input = input_list # THIS IS A 1D LIST with a few hundred items
X = np.asarray(test_input).reshape(-1,1)
N = np.arange(1, 11)
models = [None for i in range(len(N))]
for i in range(len(N)):
models[i] = GaussianMixture(N[i]).fit(X)
# compute the AIC and the BIC
AIC = [m.aic(X) for m in models]
BIC = [m.bic(X) for m in models]
fig = plt.figure(figsize=(12, 4))
fig.subplots_adjust(left=0.1, right=0.9,
bottom=0.21, top=0.9, wspace=0.3)
ax = fig.add_subplot(131)
M_best = models[np.argmin(AIC)]
comp_count = str(M_best)
x = np.linspace(0, 0.1, 100)
logprob = M_best.score_samples(x.reshape(-1, 1))
responsibilities = M_best.predict_proba(x.reshape(-1, 1))
pdf = np.exp(logprob)
pdf_individual = responsibilities * pdf[:, np.newaxis]
left, width = .245, .5
bottom, height = .4, .5
right = left + width
top = bottom + height
plt.setp( ax.xaxis.get_majorticklabels(), rotation=-45, ha="left" )
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
ax.hist(X, 30, density=True, histtype='stepfilled', alpha=0.4, label="Data")
ax.plot(x, pdf, '-k', color = "red", label='GMM')
for i, pdf_individual in enumerate(pdf_individual):
ax.plot(x, pdf_individual, '--k', label = "single Gaussians" if i == 0 else "")
#for pdf in pdf_individual[1:]: ax.plot(x, pdf, '--k')
ax.text(right, top, "Anzahl Komponenten: " + comp_count[-2],
horizontalalignment='center',
verticalalignment='bottom',
transform=ax.transAxes)
ax.set_xlabel('$x$')
ax.set_ylabel('$p(x)$')
plt.legend()
plt.show()
It results in this error:
ValueError: x and y must have same first dimension, but have shapes (100,) and (6,)
EDIT:
Putting
pdf_individual = np.transpose(pdf_individual)
makes the code above work
Hey I have a bit of a weird issue with matplotlib, I have this function:
def line_plot(results):
gen_eight = all_of_g(results, 8)
gen_sixteen = all_of_g(results, 16)
gen_thirty_two = all_of_g(results, 32)
print(len(gen_eight))
print(len(gen_sixteen))
print(len(gen_thirty_two))
N = 12
fig, ax = plt.subplots()
#ind = np.arange(N) # the x locations for the groups
#width = 0.15 # the width of the bars
plt.plot(gen_eight)
plt.plot(gen_sixteen)
plt.plot(gen_thirty_two)
ax.set_xticklabels(('1MB', '4MB', '8MB', '10MB', '16MB', '32MB', '64MB', '100MB', '128MB', '256MB', '512MB', '1GB'))
plt.tight_layout()
plt.show()
But what it plots is what you seen in the image below and the three twelves are the prints of the length. So missing half the values.
What I find really weird is that this function
def bar_plot(results):
gen_eight = all_of_g(results, 8)
gen_sixteen = all_of_g(results, 16)
gen_thirty_two = all_of_g(results, 32)
print(gen_eight)
N = 12
fig, ax = plt.subplots()
ind = np.arange(N) # the x locations for the groups
width = 0.30 # the width of the bars
p1 = ax.bar(ind, gen_eight, width, color='r')
p2 = ax.bar(ind+width, gen_sixteen, width, color='y')
p3 = ax.bar(ind+width+width, gen_thirty_two, width, color='b')
#ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('1MB', '4MB', '8MB', '10MB', '16MB', '32MB', '64MB', '100MB', '128MB', '256MB', '512MB', '1GB'))
plt.xticks(rotation=75)
ax.legend((p1[0], p2[0], p3[0]), ('g=8', 'g=16','g=32'))
ax.autoscale_view()
plt.ylabel('time (ms)')
plt.xlabel('Data Size')
plt.yscale("log", nonposy='clip')
plt.tight_layout()
fig.savefig('NOT_SHOWING')
plt.show()
Works and plots the figure as it is supposed to. I hope someone can help me
I am trying to align these plots so the x axis of the top plot perfectly aligns with the x axis values of the imshow. I'm able to do this by setting the aspect to auto, but then my image is warped. is there a way to do this?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 1200)
y = np.linspace(-20, 20, 1600)
xv, yv = np.meshgrid(x, y)
w = 3
xpos = 0
ypos = 5
z = np.exp(-((xv - xpos)**2 + (yv - ypos)**2) / w**2)
xh = np.linspace(0, 2)
yh = np.sin(xh)
sumvertical = np.sum(z, 0)
xvert = range(np.shape(z)[1])
sumhoriz = np.sum(z, 1)
yhoriz = range(np.shape(z)[0])
# definitions for the axes
left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left + width + 0.02
rect_scatter = [left, bottom, width, height]
rect_x = [left, bottom_h, width, 0.2]
rect_y = [left_h, bottom, 0.2, height]
plt.figure(1, figsize=(8, 8))
axCenter = plt.axes(rect_scatter)
axhoriz = plt.axes(rect_x)
axvert = plt.axes(rect_y)
axCenter.imshow(z, origin='lower', cmap='jet') #aspect='auto')
axhoriz.plot(xvert, sumvertical)
axvert.plot(sumhoriz, yhoriz)
plt.show()
I would recommend using the tools from mpl_toolkits.axes_grid1, namely make_axes_locatable to divide the center axes to leave room for the marginal axes.
You should then also set the margins to 0 along the shared direction to have the ranges match up.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
x = np.linspace(-10, 10, 1200)
y = np.linspace(-20, 20, 1600)
xv, yv = np.meshgrid(x, y)
w = 3
xpos = 0
ypos = 5
z = np.exp(-((xv - xpos)**2 + (yv - ypos)**2) / w**2)
xh = np.linspace(0, 2)
yh = np.sin(xh)
sumvertical = np.sum(z, 0)
xvert = range(np.shape(z)[1])
sumhoriz = np.sum(z, 1)
yhoriz = range(np.shape(z)[0])
fig, axCenter = plt.subplots(figsize=(8, 8))
fig.subplots_adjust(.05,.1,.95,.95)
divider = make_axes_locatable(axCenter)
axvert = divider.append_axes('right', size='30%', pad=0.5)
axhoriz = divider.append_axes('top', size='20%', pad=0.25)
axCenter.imshow(z, origin='lower', cmap='jet')
axhoriz.plot(xvert, sumvertical)
axvert.plot(sumhoriz, yhoriz)
axhoriz.margins(x=0)
axvert.margins(y=0)
plt.show()
I want to plot the Bull's Eye diagram of an image. I tried these codes
Shade 'cells' in polar plot with matplotlib
For Bull's Eye diagram I want to use different colors. Is there is any way to set this colors? In color = choice(['navy','maroon','lightgreen']) colors are repeating according as for loop iterates.
Does anyone know if in matplotlib there is a function correponding to matlab bullseye()?
To provide something similar to the matlab function you mentioned, I drafted some code (shared on git hub as well) for the creation of bullseyes with any number of sector for each radial subdivision. The colour is given by the colormap and by the values below the sector. Moreover, this allows for the modification the centering of the radial subdivision.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as ticker
def bulls_eye(ax, data, cmap=None, norm=None, raidal_subdivisions=(2, 8, 8, 11),
centered=(True, False, False, True), add_nomenclatures=True, cell_resolution=128,
pfi_where_to_save=None, colors_bound='-k'):
"""
Clockwise, from smaller radius to bigger radius.
:param ax:
:param data:
:param cmap:
:param norm:
:param raidal_subdivisions:
:param centered:
:param add_nomenclatures:
:param cell_resolution:
:param pfi_where_to_save:
:return:
"""
line_width = 1.5
data = np.array(data).ravel()
if cmap is None:
cmap = plt.cm.viridis
if norm is None:
norm = mpl.colors.Normalize(vmin=data.min(), vmax=data.max())
theta = np.linspace(0, 2*np.pi, 768)
r = np.linspace(0, 1, len(raidal_subdivisions)+1)
nomenclatures = []
if isinstance(add_nomenclatures, bool):
if add_nomenclatures:
nomenclatures = range(1, sum(raidal_subdivisions)+1)
elif isinstance(add_nomenclatures, list) or isinstance(add_nomenclatures, tuple):
assert len(add_nomenclatures) == sum(raidal_subdivisions)
nomenclatures = add_nomenclatures[:]
add_nomenclatures = True
# Create the circular bounds
line_width_circular = line_width
for i in range(r.shape[0]):
if i == range(r.shape[0])[-1]:
line_width_circular = int(line_width / 2.)
ax.plot(theta, np.repeat(r[i], theta.shape), colors_bound, lw=line_width_circular)
# iterate over cells divided by radial subdivision
for rs_id, rs in enumerate(raidal_subdivisions):
for i in range(rs):
cell_id = sum(raidal_subdivisions[:rs_id]) + i
theta_i = - i * 2 * np.pi / rs + np.pi / 2
if not centered[rs_id]:
theta_i += (2 * np.pi / rs) / 2
theta_i_plus_one = theta_i - 2 * np.pi / rs # clockwise
# Create colour fillings for each cell:
theta_interval = np.linspace(theta_i, theta_i_plus_one, cell_resolution)
r_interval = np.array([r[rs_id], r[rs_id+1]])
angle = np.repeat(theta_interval[:, np.newaxis], 2, axis=1)
radius = np.repeat(r_interval[:, np.newaxis], cell_resolution, axis=1).T
z = np.ones((cell_resolution, 2)) * data[cell_id]
ax.pcolormesh(angle, radius, z, cmap=cmap, norm=norm)
# Create radial bounds
if rs > 1:
ax.plot([theta_i, theta_i], [r[rs_id], r[rs_id+1]], colors_bound, lw=line_width)
# Add centered nomenclatures if needed
if add_nomenclatures:
if rs == 1 and rs_id ==0:
cell_center = (0, 0)
else:
cell_center = ((theta_i + theta_i_plus_one) / 2., r[rs_id] + .5 * r[1] )
if isinstance(nomenclatures[0], (int, long, float, complex)):
ax.annotate(r"${:.3g}$".format(nomenclatures[cell_id]), xy=cell_center,
xytext=(cell_center[0], cell_center[1]),
horizontalalignment='center', verticalalignment='center', size=8)
else:
ax.annotate(nomenclatures[cell_id], xy=cell_center,
xytext=(cell_center[0], cell_center[1]),
horizontalalignment='center', verticalalignment='center', size=12)
ax.grid(False)
ax.set_ylim([0, 1])
ax.set_yticklabels([])
ax.set_xticklabels([])
if pfi_where_to_save is not None:
plt.savefig(pfi_where_to_save, format='pdf', dpi=200)
def multi_bull_eyes(multi_data, cbar=None, cmaps=None, normalisations=None,
global_title=None, canvas_title='title', titles=None, units=None, raidal_subdivisions=(2, 8, 8, 11),
centered=(True, False, False, True), add_nomenclatures=(True, True, True, True),
pfi_where_to_save=None, show=True):
plt.clf()
n_fig = len(multi_data)
if cbar is None:
cbar = [True] * n_fig
if cmaps is None:
cmaps = [mpl.cm.viridis] * n_fig
if normalisations is None:
normalisations = [mpl.colors.Normalize(vmin=np.min(multi_data[i]), vmax=np.max(multi_data[i]))
for i in range(n_fig)]
if titles is None:
titles = ['Title {}'.format(i) for i in range(n_fig)]
h_space = 0.15 / n_fig
h_dim_fig = .8
w_dim_fig = .8 / n_fig
def fmt(x, pos):
# a, b = '{:.2e}'.format(x).split('e')
# b = int(b)
# return r'${} \times 10^{{{}}}$'.format(a, b)
return r"${:.4g}$".format(x)
# Make a figure and axes with dimensions as desired.
fig = plt.figure(figsize=(3 * n_fig, 4))
fig.canvas.set_window_title(canvas_title)
if global_title is not None:
plt.suptitle(global_title)
for n in range(n_fig):
origin_fig = (h_space * (n + 1) + w_dim_fig * n, 0.15)
ax = fig.add_axes([origin_fig[0], origin_fig[1], w_dim_fig, h_dim_fig], polar=True)
bulls_eye(ax, multi_data[n], cmap=cmaps[n], norm=normalisations[n], raidal_subdivisions=raidal_subdivisions,
centered=centered, add_nomenclatures=add_nomenclatures[n])
ax.set_title(titles[n], size=10)
if cbar[n]:
origin_cbar = (h_space * (n + 1) + w_dim_fig * n, .15)
axl = fig.add_axes([origin_cbar[0], origin_cbar[1], w_dim_fig, .05])
cb1 = mpl.colorbar.ColorbarBase(axl, cmap=cmaps[n], norm=normalisations[n], orientation='horizontal',
format=ticker.FuncFormatter(fmt))
cb1.ax.tick_params(labelsize=8)
if units is not None:
cb1.set_label(units[n])
if pfi_where_to_save is not None:
plt.savefig(pfi_where_to_save, format='pdf', dpi=330)
if show:
plt.show()
if __name__ == '__main__':
# Very dummy data:
data = np.array(range(29)) + 1
# TEST bull-eye three-fold
if True:
fig, ax = plt.subplots(figsize=(12, 8), nrows=1, ncols=3,
subplot_kw=dict(projection='polar'))
fig.canvas.set_window_title('Left Ventricle Bulls Eyes')
# First one:
cmap = mpl.cm.viridis
norm = mpl.colors.Normalize(vmin=1, vmax=29)
bulls_eye(ax[0], data, cmap=cmap, norm=norm)
ax[0].set_title('Bulls Eye ')
axl = fig.add_axes([0.14, 0.15, 0.2, 0.05])
cb1 = mpl.colorbar.ColorbarBase(axl, cmap=cmap, norm=norm, orientation='horizontal')
cb1.set_label('Some Units')
# Second one
cmap2 = mpl.cm.cool
norm2 = mpl.colors.Normalize(vmin=1, vmax=29)
bulls_eye(ax[1], data, cmap=cmap2, norm=norm2)
ax[1].set_title('Bulls Eye ')
axl2 = fig.add_axes([0.41, 0.15, 0.2, 0.05])
cb2 = mpl.colorbar.ColorbarBase(axl2, cmap=cmap2, norm=norm2, orientation='horizontal')
cb2.set_label('Some other units')
# Third one
cmap3 = mpl.cm.winter
norm3 = mpl.colors.Normalize(vmin=1, vmax=29)
bulls_eye(ax[2], data, cmap=cmap3, norm=norm3)
ax[2].set_title('Bulls Eye third')
axl3 = fig.add_axes([0.69, 0.15, 0.2, 0.05])
cb3 = mpl.colorbar.ColorbarBase(axl3, cmap=cmap3, norm=norm3, orientation='horizontal')
cb3.set_label('Some more units')
plt.show()
if True:
fig = plt.figure(figsize=(5, 7))
fig.canvas.set_window_title('Bulls Eyes - segmentation assessment')
# First and only:
cmap = mpl.cm.viridis
norm = mpl.colors.Normalize(vmin=1, vmax=29)
ax = fig.add_axes([0.1, 0.2, 0.8, 0.7], polar=True)
bulls_eye(ax, data, cmap=cmap, norm=norm)
ax.set_title('Bulls Eye')
axl = fig.add_axes([0.1, 0.15, 0.8, 0.05])
cb1 = mpl.colorbar.ColorbarBase(axl, cmap=cmap, norm=norm, orientation='horizontal')
cb1.set_label('Some Units')
plt.show()
if True:
multi_data = [range(1,17), list( 0.000000001 * np.array(range(1,17))), list( 0.001 * np.array(range(1,17)))]
print multi_data
multi_bull_eyes(multi_data, raidal_subdivisions=(3,3,4,6),
centered=(True, True, True, True), add_nomenclatures=[True]*3)
plt.show(block=True)
It is proposed within my code LabelsManager on github, that you are welcome to use.
Here is an example, for raidal_subdivisions=(2, 8, 8, 11), and centered=(True, False, False, True):
I don't think that there is a specific bullseye method in matplotlib, but comparing what the matlab function does and the right plot in this answer, I think that you should be able to get what you want playing a bit with the radius coordinates. E.g. (borrowing from the aforesaid answer)
import numpy as np
import matplotlib.pyplot as plt
theta, r = np.mgrid[0:2*np.pi:20j, 0.2:1:10j]
z = np.random.random(theta.size).reshape(theta.shape)
fig, ax = plt.subplots(ncols=1, subplot_kw=dict(projection='polar'))
ax.pcolormesh(theta, r, z)
ax.set_yticklabels([])
ax.set_ylim([0, 1])
plt.show()
plots
I don't understand exactly what you mean with the question about the colors. ax.pcolormesh accept either a colormap or a matplotlib color. If you want specific colors you can play with those two parameters and/or create your own colormap.