Pyplot boxplots centered around xticks - python

I have a series of boxplots that I want to be centered around xticks (2 per xtick specifically). Consider the following:
# fake up some more data
spread= rand(50) * 100
center = ones(25) * 40
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
data.shape = (-1, 1)
d2.shape = (-1, 1)
#data = concatenate( (data, d2), 1 )
# Making a 2-D array only works if all the columns are the
# same length. If they are not, then use a list instead.
# This is actually more efficient because boxplot converts
# a 2-D array into a list of vectors internally anyway.
data = [data, d2, d2[::2,0]]
# multiple box plots on one figure
figure()
boxplot(data)
Which produces
However I would like to have 6 boxplots, with 2 centered around 1, 2 around 2, etc... If I add in three more it simply adds them to 4,5,6... Any help would be appreciated
EDIT To be clear by what I mean by "centered". I would want one boxplot just to the left of the xtick labled "1", and another just to the right. They would likely overlap in the y range so I don't want them to be drawn on top of each other.

To control the x-position of the boxplots, use the positions kwarg.
For example:
import numpy as np
import matplotlib.pyplot as plt
dists = [np.random.normal(i, 1, 100) for i in range(0, 10, 2)]
fig, ax = plt.subplots()
ax.boxplot(dists, positions=[0, 1, 2, 0, 1])
plt.show()
If you'd prefer to have the groups side-by-side, you'll need to calculate the positions yourself. One approach might be something like this:
def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
if ax is None:
ax = plt.gca()
max_group_size = max(len(item) for item in data_groups)
total_padding = pad * (max_group_size - 1)
width = (max_width - total_padding) / max_group_size
kwargs['widths'] = width
def positions(group, i):
span = width * len(group) + pad * (len(group) - 1)
ends = (span - width) / 2
x = np.linspace(-ends, ends, len(group))
return x + i
artists = []
for i, group in enumerate(data_groups, start=1):
artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
artists.append(artist)
ax.margins(0.05)
ax.set(xticks=np.arange(len(data_groups)) + 1)
ax.autoscale()
return artists
And as a quick example of using it:
data = [[np.random.normal(i, 1, 30) for i in range(2)],
[np.random.normal(i, 1.5, 30) for i in range(3)],
[np.random.normal(i, 2, 30) for i in range(4)]]
grouped_boxplots(data)
plt.show()
...And just for the sake of showing an excessively fancy example:
import numpy as np
import matplotlib.pyplot as plt
def main():
data = [[np.random.normal(i, 1, 30) for i in range(2)],
[np.random.normal(i, 1.5, 30) for i in range(3)],
[np.random.normal(i, 2, 30) for i in range(4)]]
fig, ax = plt.subplots()
groups = grouped_boxplots(data, ax, max_width=0.9,
patch_artist=True, notch=True)
colors = ['lavender', 'lightblue', 'bisque', 'lightgreen']
for item in groups:
for color, patch in zip(colors, item['boxes']):
patch.set(facecolor=color)
proxy_artists = groups[-1]['boxes']
ax.legend(proxy_artists, ['Group A', 'Group B', 'Group C', 'Group D'],
loc='best')
ax.set(xlabel='Year', ylabel='Performance', axisbelow=True,
xticklabels=['2012', '2013', '2014'])
ax.grid(axis='y', ls='-', color='white', lw=2)
ax.patch.set(facecolor='0.95')
plt.show()
def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
if ax is None:
ax = plt.gca()
max_group_size = max(len(item) for item in data_groups)
total_padding = pad * (max_group_size - 1)
width = (max_width - total_padding) / max_group_size
kwargs['widths'] = width
def positions(group, i):
span = width * len(group) + pad * (len(group) - 1)
ends = (span - width) / 2
x = np.linspace(-ends, ends, len(group))
return x + i
artists = []
for i, group in enumerate(data_groups, start=1):
artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
artists.append(artist)
ax.margins(0.05)
ax.set(xticks=np.arange(len(data_groups)) + 1)
ax.autoscale()
return artists
main()

Related

Rank line plot customization

Currently I'm trying to plot a graph showing the rank of some equipment in operation, the rank goes from 1 to 300 (1 is the best, 300 is the worst) over a few days (df columns). What I'm trying to do, is a graph similar to this:
And what I got is this:
I would like to make the lines inclined as it is on the first graph instead of vertical, but I can't figure it out how. I found the base for the first graph on this question here and I started the code from there, this is what I end up having:
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import numpy as np
def energy_rank(data, marker_width=.5, color='blue'):
y_data = np.repeat(data, 2)
x_data = np.empty_like(y_data)
x_data[0::2] = np.arange(1, len(data)+1) - (marker_width/2)
x_data[1::2] = np.arange(1, len(data)+1) + (marker_width/2)
lines = []
lines.append(plt.Line2D(x_data, y_data, lw=0.8, linestyle='dashed', color=color,alpha=1,marker='.'))
for x in range(0,len(data)*2, 2):
lines.append(plt.Line2D(x_data[x:x+2], y_data[x:x+2], lw=2, linestyle='solid', color=color))
return lines
head = 8
dfPlot = vazio.sort_values(dia, ascending = True).head(head)
data = dfPlot.to_numpy()
colorsHEX=('#FE5815','#001A70','#2F5C22','#B01338','#00030D','#2DE1FC','#2E020C','#B81D8C')
artists = []
for row, color in zip(data, colorsHEX):
artists.extend(energy_rank(row, color=color))
eixoXDatas = pd.to_datetime(list(vazio.columns),format='%d/%m/%y').strftime('%d/%b')
fig, ax = plt.subplots()
plt.xticks(np.arange(len(vazio.columns)),
eixoXDatas,
rotation = 35,
fontsize = 14)
plt.yticks(fontsize = 14)
plt.xlabel('Dias', fontsize=18)
plt.ylabel('Ranking', fontsize=18)
fig = plt.gcf()
fig.set_size_inches(16, 8)
for artist in artists:
ax.add_artist(artist)
ax.set_ybound([0,15])
ax.set_ylim(ax.get_ylim()[::-1])
ax.set_xbound([-0.1,float(len(vazio.columns))+2.5])
plt.yticks(np.arange(1,16,step=1))
ax.grid(axis='y',alpha=0.5)
lastDay = vazio.sort_values(vazio.iloc[:,-1:].columns.values[0], ascending = True).iloc[:,-1:]
lastDay = lastDay.head(head)
for inverter, pos in lastDay.iterrows():
ax.annotate(inverter, xy =(plt.gca().get_xlim()[1]-2.4, pos), color=colorsHEX[int(pos)-1])
I tried implementing on energy_rank function, removing the +/- parts on x_data but I only could end up with inclined lines with dots instead of the horizontal lines. Can anyone help me out how can I mantain the horziontal lines and instead of vertical dashed lines, implement inclined lines as the example above?
I imagine that is vertical because the points change on top of the x ticks. If you observe the 1st image, the horizontal bars are centralized on each x tick, so the lines "have some room" to be inclined.
vazio dataframe is as follows (contains the rank of each equipment):
Equipment 21-03-27 21-03-28 21-03-29 21-03-30 21-03-31 21-04-01 21-04-02
P01-INV-1-1 1 1 1 1 1 2 2
P01-INV-1-2 2 2 4 4 5 1 1
P01-INV-1-3 4 4 3 5 6 10 10
Here is an adaption of your energy_rank function creating horizontal line segments together with their connections. The line drawing part is inspired by this tutorial example. Optionally the area below the lines can be filled.
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
def energy_rank(data, marker_width=.5, color='blue', ax=None, fill=False):
ax = ax or plt.gca()
y = data
x = np.arange(1, len(data) + 1)
segments1 = np.array([x - marker_width / 2, y, x + marker_width / 2, y]).T.reshape(-1, 2, 2)
lc1 = LineCollection(segments1, color=color)
lc1.set_linewidth(2)
lc1.set_linestyle('-')
lines_hor = ax.add_collection(lc1)
segments2 = np.array([x[:-1] + marker_width / 2, y[:-1], x[1:] - marker_width / 2, y[1:]]).T.reshape(-1, 2, 2)
lc2 = LineCollection(segments2, color=color)
lc2.set_linewidth(0.5)
lc2.set_linestyle('--')
lines_connect = ax.add_collection(lc2)
if fill:
ax.fill_between(segments1.reshape(-1,2)[:,0], segments1.reshape(-1,2)[:,1],
color=color, alpha=0.05)
return lines_hor, lines_connect
fig, ax = plt.subplots()
M, N = 5, 25
y = np.random.uniform(-2, 2, (M, N)).cumsum(axis=1)
y += np.random.uniform(0.5, 2, (M, 1)) - y.min(axis=1, keepdims=True)
colorsHEX = ('#FE5815', '#001A70', '#2F5C22', '#B01338', '#00030D')
for yi, color in zip(y, colorsHEX):
energy_rank(yi, ax=ax, color=color)
ax.set_xlim(0, N + 1)
ax.set_ylim(0, y.max() + 1)
plt.show()

array has more elements than it's slice

I am scattering the nonzero elements of a numpy array (a raster plot) however array has more points than it should actually have.
fig,ax = plt.subplots(2, 1, figsize = (20,21))
raster_plot(all_concatenated[subject_names[0]][:,0:10000], ax = ax[0])
raster_plot(all_concatenated[subject_names[0]][:,0:8000], ax = ax[1])
where
def raster_plot(spikes, ax, t = 500):
binned_spikes = bin_time_series(spikes, binsize = t, total_frame = spikes.shape[1])
n,t,l = binned_spikes.shape
#cmap = plt.cm.Spectral
#norm = plt.Normalize(vmin = 0, vmax = float(np.max(spikes)))
for i in range(n):
for k in range(l):
for j in range(t):
if binned_spikes[i][j][k] == 0: pass
else:
ax.scatter(k*t+j, i, s = 5, c = 'b', marker = 'x')#, norm = norm, cmap = cmap)
ax.set_title('Spike Raster Plot', fontsize = 20)
ax.set_xlabel('Time (Frames)', fontsize = 15)
ax.set_ylabel('Neuron ID', fontsize = 15)
ax.set_xticks([t*i for i in range(l+1)])
ax.set_yticks([5*i for i in range(int(n/5)+1)]+[n])
ax.tick_params(axis = 'x', labelsize = 10)
ax.tick_params(axis = 'y', labelsize = 13)
and
def bin_time_series(array, binsize, total_frame):
binned_spikes = []
for i in range(len(array)):
A = array[i].reshape(binsize, int(total_frame/binsize))
binned_spikes.append(A)
return(np.array(binned_spikes, dtype=np.int32))
when I look at the first 8000 time points neurons with id 94-113 has no spikes which is what the array actually is however when I look at the first 10000 time points, I'm seeing extra spikes that should not exist. One strange thing is those spikes that should not exist in the 10000 slice are all near the multiples of 500.
Is this a sparsity issue? Am I going crazy or what am I missing?

Matplotlib: Shared axis for imshow images

I'm trying to plot multiple images with Matplotlib's imshow() method, and have them share a single y axis. Although the images have the same number of y pixels, the images don't end up the same height.
Demonstration code;
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import poisson
def ibp_oneparam(alpha, N):
"""One-parameter IBP"""
# First customer
Z = np.array([np.ones(poisson(alpha).rvs(1))], dtype=int)
# ith customer
for i in range(2, N+1):
# Customer walks along previously sampled dishes
z_i = []
for previously_sampled_dish in Z.T:
m_k = np.sum(previously_sampled_dish)
if np.random.rand() >= m_k / i:
# Customer decides to sample this dish
z_i.append(1.0)
else:
# Customer decides to skip this dish
z_i.append(0.0)
# Customer decides to try some new dishes
z_i.extend(np.ones(poisson(alpha / i).rvs(1)))
z_i = np.array(z_i)
# Add this customer to Z
Z_new = np.zeros((
Z.shape[0] + 1,
max(Z.shape[1], len(z_i))
))
Z_new[0:Z.shape[0], 0:Z.shape[1]] = Z
Z = Z_new
Z[i-1, :] = z_i
return Z
np.random.seed(3)
N = 10
alpha = 2.0
#plt.figure(dpi=100)
fig, (ax1, ax2, ax3) = plt.subplots(
1,
3,
dpi=100,
sharey=True
)
Z = ibp_oneparam(alpha, N)
plt.sca(ax1)
plt.imshow(
Z,
extent=(0.5, Z.shape[1] + 0.5, len(Z) + 0.5, 0.5),
cmap='Greys_r'
)
plt.ylabel("Customers")
plt.xlabel("Dishes")
plt.xticks(range(1, Z.shape[1] + 1))
plt.yticks(range(1, Z.shape[0] + 1))
Z = ibp_oneparam(alpha, N)
plt.sca(ax2)
plt.imshow(
Z,
extent=(0.5, Z.shape[1] + 0.5, len(Z) + 0.5, 0.5),
cmap='Greys_r'
)
plt.xlabel("Dishes")
plt.xticks(range(1, Z.shape[1] + 1))
Z = ibp_oneparam(alpha, N)
plt.sca(ax3)
plt.imshow(
Z,
extent=(0.5, Z.shape[1] + 0.5, len(Z) + 0.5, 0.5),
cmap='Greys_r'
)
plt.xlabel("Dishes")
plt.xticks(range(1, Z.shape[1] + 1))
plt.show()
Output;
I expect these images to each have the same height, and have varying widths. How can I achieve this?
Aside: The code above is demonstrating the Indian Buffet Process. For the purposes of this post, consider the three images to be random binary matrices with the same number of rows, but variable numbers of columns.
Thank you,
I got a decent result with grid-spec width_ratios.
"""fig, (ax1, ax2, ax3) = plt.subplots(
1,
3,
dpi=100,
sharey=True,
constrained_layout=True
)"""
# I commented the above code and replaced with below.
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=True)
gs = gridspec.GridSpec(ncols=3, nrows=1, figure=fig, width_ratios=[7./4.,1,6./4.])
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[0,1])
ax3 = fig.add_subplot(gs[0,2])
It's some what counter intuitive that you need to use width ratios to adjust the heights but in the context of a grid with multiple rows it makes sense that you can only scale columns independently by width. and rows independently by height.
https://matplotlib.org/tutorials/intermediate/gridspec.html

Generate grid cells (occupancy grid), color cells, and remove xlabels

I am working on some graph based planning algorithms but I first wanted to setup a plotting script that takes a 2D matrix and plots it as grid cells, and colors the cells based on certain values.
I am trying to determine two things:
How can I completely remove the Xticks, if you look at the images generated they are quite faint but still there?
Is there a better approach to the grid plotting and generation that I am overlooking? I know the generate_moves is not perfect but this is the first take.
Here is a link to the repo where I have got to (same as the code below)
Here is the code I have
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
import random
EMPTY_CELL = 0
OBSTACLE_CELL = 1
START_CELL = 2
GOAL_CELL = 3
MOVE_CELL = 4
# create discrete colormap
cmap = colors.ListedColormap(['white', 'black', 'green', 'red', 'blue'])
bounds = [EMPTY_CELL, OBSTACLE_CELL, START_CELL, GOAL_CELL, MOVE_CELL ,MOVE_CELL + 1]
norm = colors.BoundaryNorm(bounds, cmap.N)
def plot_grid(data, saveImageName):
fig, ax = plt.subplots()
ax.imshow(data, cmap=cmap, norm=norm)
# draw gridlines
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=1)
ax.set_xticks(np.arange(0.5, rows, 1));
ax.set_yticks(np.arange(0.5, cols, 1));
plt.tick_params(axis='both', labelsize=0, length = 0)
# fig.set_size_inches((8.5, 11), forward=False)
plt.savefig(saveImageName + ".png", dpi=500)
def generate_moves(grid, startX, startY):
num_rows = np.size(grid, 0)
num_cols = np.size(grid, 1)
# Currently do not support moving diagonally so there is a max
# of 4 possible moves, up, down, left, right.
possible_moves = np.zeros(8, dtype=int).reshape(4, 2)
# Move up
possible_moves[0, 0] = startX - 1
possible_moves[0, 1] = startY
# Move down
possible_moves[1, 0] = startX + 1
possible_moves[1, 1] = startY
# Move left
possible_moves[2, 0] = startX
possible_moves[2, 1] = startY - 1
# Move right
possible_moves[3, 0] = startX
possible_moves[3, 1] = startY + 1
# Change the cell value if the move is valid
for row in possible_moves:
if row[0] < 0 or row[0] >= num_rows:
continue
if row[1] < 0 or row[1] >= num_cols:
continue
grid[row[0], row[1]] = MOVE_CELL
if __name__ == "__main__":
rows = 20
cols = 20
# Randomly create 20 different grids
for i in range(0, 20):
data = np.zeros(rows * cols).reshape(rows, cols)
start_x = random.randint(0, rows - 1)
start_y = random.randint(0, cols - 1)
data[start_x, start_y] = START_CELL
goal_x = random.randint(0, rows - 1)
# Dont want the start and end positions to be the same
# so keep changing the goal x until its different.
# If X is different dont need to check Y
while goal_x is start_x:
goal_x = random.randint(0, rows - 1)
goal_y = random.randint(0, cols - 1)
data[goal_x, goal_y] = GOAL_CELL
generate_moves(data, start_x, start_y)
plot_grid(data, "week1/images/grid_" + str(i))
Use the following to hide ticks and labels
def plot_grid(data, saveImageName):
fig, ax = plt.subplots()
ax.imshow(data, cmap=cmap, norm=norm)
# draw gridlines
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=1)
ax.set_xticks(np.arange(0.5, rows, 1));
ax.set_yticks(np.arange(0.5, cols, 1));
plt.tick_params(axis='both', which='both', bottom=False,
left=False, labelbottom=False, labelleft=False)
fig.set_size_inches((8.5, 11), forward=False)
plt.savefig(saveImageName + ".png", dpi=500)

Bulls eye plot of image

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.

Categories

Resources