Adjust width of box in boxplot in python matplotlib - python

I would like to reduce the width of the boxes in the boxplot below. Here's my code, but it is not working:
bp = plt.boxplot(boxes, widths = 0.6, patch_artist = True)

From the documentation there is a widths option:
widths : array-like, default = 0.5
Either a scalar or a vector and sets the width of each box. The default is 0.5, or 0.15*(distance between extreme positions) if that is smaller.
Here is an example:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(937)
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
labels = list('ABCD')
fs = 10 # fontsize
plt.boxplot(data, labels=labels, showfliers=False, widths=(1, 0.5, 1.2, 0.1))
plt.show()

Try working via the axes and see if it works:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.boxplot(boxes, widths = 0.6, patch_artist = True)

Related

My colorbar doesn't show the right colors (Python, matplotlib)

I'm stuck trying to get my colorbar to show the same colorspectrum as my scatterplot. Instead of the pink/purple to black spectrum of my colorgraded datapoints, it shows the default colors of a colorbar. I have read multiple other threads on here to no avail, but please alert me to a similar thread, if I have missed something that could solve my problem.
I have made a short code illustrating my problem:
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng()
arr = np.arange(7000)
rng.shuffle(arr)
r = np.sqrt(np.random.random(7000))
theta = np.random.uniform(high = 2*np.pi, size = 7000)
X = np.array(r*np.cos(theta))
Y = np.array(r*np.sin(theta))
def values_to_colormap(values):
values_scale = values/np.max(values)
(a,) = np.shape(values)
cmap = values_scale.reshape(a,1)*np.array([[0.6, 0.4, 0.6]])
return cmap
points_colors = values_to_colormap(arr)
ps = plt.scatter(X,Y, marker = '.', color = points_colors)
plt.colorbar(ps, orientation='horizontal')
plt.axis('equal')
plt.show();
The colorbar uses the cmap and the norm of the scatter plot. In this case, individual colors are given, and the colorbar falls back to the default colormap ('viridis') and the default norm (as no vmin nor vmax nor explicit color values are given, 0 and 1 are used).
Your values_to_colormap function maps 0 to color (0, 0, 0) and the maximum value to (0.6, 0.4, 0.6). This is equivalent to use a norm with vmin=0, vmax=arr.max() and a LinearSegmentedColormap between the given colors:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
rng = np.random.default_rng()
arr = np.arange(7000)
rng.shuffle(arr)
r = np.sqrt(np.random.random(7000))
theta = np.random.uniform(high=2 * np.pi, size=7000)
X = np.array(r * np.cos(theta))
Y = np.array(r * np.sin(theta))
ps = plt.scatter(X, Y, marker='.', c=arr, vmin=0, vmax=arr.max(),
cmap=LinearSegmentedColormap.from_list('', [(0, 0, 0), (0.6, 0.4, 0.6)]))
plt.colorbar(ps, orientation='horizontal')
plt.axis('equal')
plt.show()

Modifying saved plot with matplotlib

I am having a problem right now. I have run an extremely heavy simulation and, thus, generated a plot with matplotlib containing the results and saved it (as .jpg). However, there are some elemnts of the plot I would like to change, such as labels size and one vertical line. Is there a straighforward way to do this using matplotlib? I know I could have stored the data and now just replot changing the parameters (and, actually, I have done this), but I was wondering whether there is an easier way. Maybe something like:
fig, ax = plt.figure(path_to_figure)
ax.set_ylabel("Y_label")
...
You can refer to below example, which gives you more idea on how you can do this while plotting everything.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
plt.rc('text', usetex=True)
def f(t):
return t ** 2
t1 = np.arange(0.0, 2.0, 0.1)
noise = np.random.randn(len(t1)) * 0.04
# x coordinates for the lines
xcoords = [0.1, 0.3, 0.5]
# colors for the lines
colors = ['r','k','b']
fig = plt.figure(figsize=(4, 3), dpi=200)
ax = fig.add_subplot(1, 1, 1)
plt.scatter(t1, f(t1 + noise), color = 'hotpink', label='Values obtained by experiment', edgecolors='k')
plt.plot(t1, f(t1), ls='solid', label='Theoretical expectation', color='b')
plt.title(r'This is latex title example $\mathbf{E = m \times c^2}$', fontsize='small')
for xc,c in zip(xcoords,colors):
plt.axvline(x=xc, label='line at x = {}'.format(xc), c=c)
plt.grid()
plt.legend(loc=0)
If you want to make all the fonts bold, you can also use below code to make everything bold:
font = {'weight' : 'bold',
'size' : 14 }
plt.rc('font', **font)
def f(t):
return t ** 2
t1 = np.arange(0.0, 2.0, 0.1)
noise = np.random.randn(len(t1)) * 0.04
# x coordinates for the lines
xcoords = [0.1, 0.3, 0.5]
# colors for the lines
colors = ['r','k','b']
fig = plt.figure(figsize=(4, 3), dpi=200)
ax = fig.add_subplot(1, 1, 1)
plt.scatter(t1, f(t1 + noise), color = 'hotpink', label='Values obtained by experiment', edgecolors='k')
plt.plot(t1, f(t1), ls='solid', label='Theoretical expectation', color='b')
plt.title(r'This is latex title example $\mathbf{E = m \times c^2}$', fontsize='small')
plt.xlabel("This is X-label.", fontsize=12)
plt.ylabel("This is Y-label.", fontsize=16)
for xc,c in zip(xcoords,colors):
plt.axvline(x=xc, label='line at x = {}'.format(xc), c=c)
plt.grid()
plt.legend(loc=(1.15,0.2))

Matplotlib subplot y-axis scale overlaps with plot above

I am trying to plot 3 subplots without any white space between them. The default y axis ticklabels use a scale displayed to the top right of the y axis (1e-8 in the example below), which would be fine except for the lower two plots this overlaps with the plot above. Anyone know how to fix this? A small example is below.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator
x = np.arange(0,200)
y = np.random.rand(200) * 10e-8
fig = plt.figure(figsize=(10,15))
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0)
ax0a = plt.subplot(gs1[0, :])
ax0b = plt.subplot(gs1[1, :])
ax0c = plt.subplot(gs1[2, :])
ax0a.set_xticklabels([])
ax0b.set_xticklabels([])
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
so one solution is to use mtick,
import matplotlib.ticker as mtick
ax0a.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
ax0b.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
ax0c.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
but I would prefer to be able to shift the scale to the left so it is outside the axis if possible.
I have two options you might want to look at.
First, set the axis location and size yourself as such:
# your imports and data above
fig = plt.figure()
ax0a = fig.add_axes([0.1, 0.1, 0.8, 0.25])
ax0b = fig.add_axes([0.1, 0.39, 0.8, 0.25], sharex=ax0a)
ax0c = fig.add_axes([0.1, 0.68, 0.8, 0.25], sharex=ax0a)
ax0a.set_xticklabels([])
ax0b.set_xticklabels([])
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
plt.show()
The second option is to manually adjust the location and maybe font size of the offset text:
# your original code minus data and imports
fig = plt.figure()
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0)
ax0a = plt.subplot(gs1[0, :])
ax0b = plt.subplot(gs1[1, :])
ax0c = plt.subplot(gs1[2, :])
ax0a.set_xticklabels([])
ax0b.set_xticklabels([])
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
# play around with location and font of offset text here
ax0a.get_yaxis().get_offset_text().set_x(-0.075)
ax0a.get_yaxis().get_offset_text().set_size(10)
ax0b.get_yaxis().get_offset_text().set_x(-0.075)
ax0b.get_yaxis().get_offset_text().set_size(10)
ax0c.get_yaxis().get_offset_text().set_x(-0.075)
ax0c.get_yaxis().get_offset_text().set_size(10)
plt.show()
Ok, this is probably an ugly solution but you could just simply upscale
your data in the lower plots with the range of the power i.e. ax0b.plot(x, y*1e8).
This works for your example at least.

Change colour scale increments in Python

I have created a frequency time spectrogram plot seen below.
I want to edit the colour scale so that the higher frequencies shown from 20 seconds are more prominent. I think having smaller increments at the lower end of the colour scale (blues) would achieve this but am not sure how to do it. Any help would be great!
Here is what I have so far:
import numpy as np
import matplotlib.pyplot as plt
from obspy.core import read
from obspy.signal.tf_misfit import cwt
import pylab
tr = read("whole.sac")[0]
npts = tr.stats.npts
dt = tr.stats.delta
t = np.linspace(0, dt * npts, npts)
f_min = 1
f_max = 10
scalogram = cwt(tr.data, dt, 8, f_min, f_max)
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.7, 0.60])
ax2 = fig.add_axes([0.1, 0.75, 0.75, 0.2])
ax3 = fig.add_axes([0.83, 0.1, 0.03, 0.6])
img = ax1.imshow(np.abs(scalogram)[-1::-1], extent=[t[0], t[-1], f_min, f_max],
aspect='auto', interpolation="nearest")
ax1.set_xlabel("Time after %s [s]" % tr.stats.starttime)
ax1.set_ylabel("Frequency [Hz]")
ax1.set_yscale('linear')
ax2.plot(t, tr.data, 'k')
pylab.xlim([30,72])
fig.colorbar(img, cax=ax3)
plt.show()
You could try other colormaps or make you own according to this recipe.
Or you may want to filter the data to set all values above a given threshold (e.g. 60) to the threshold value. This would use the entire range of the colormap on the range of interest. You can easily use np.clip() to do this.
So...
np.abs(scalogram)[-1::-1]
becomes
np.clip(np.abs(scalogram)[-1::-1], 0, 100)
to clip between 0 and 100.

matplotlib scatter_hist with stepfilled histtype in histogram

I modified scatter_hist.py example found here to have two data sets to be plotted.
I'd like to have histograms with "stepfilled" type, but somehow if I set the type "stepfilled" the Y-axis histogram (orientation = "horizontal") is not working.
Is there any other way to do the histogram to look like "stepfilled"-style or am I doing something wrong?
Here is my code with histtype = "bar" to show the idea what I try to do. Change it to
histtype="stepfilled"
to get strange histogram:
import numpy as np
import matplotlib.pyplot as plt
# the random data
x = np.random.randn(1000)
y = np.random.randn(1000)
x_vals = [x]
y_vals = [y]
x_vals.append( np.random.randn( 300 ) )
y_vals.append( np.random.randn( 300 ) )
fig = plt.figure(1, figsize=(5.5,5.5))
from mpl_toolkits.axes_grid1 import make_axes_locatable
colour_LUT = ['#0000FF',
'#00FF00']
# the scatter plot:
xymax = np.max(np.fabs(x))
colors = []
axScatter = plt.subplot(111)
for i in range( len(x_vals ) ):
colour = colour_LUT[i]
xymax = np.max( [np.max(np.fabs(x)), np.max(np.fabs(y)), xymax ] )
axScatter.scatter( x_vals[i], y_vals[i], color = colour )
colors.append(colour)
axScatter.set_aspect(1.)
# create new axes on the right and on the top of the current axes
# The first argument of the new_vertical(new_horizontal) method is
# the height (width) of the axes to be created in inches.
divider = make_axes_locatable(axScatter)
axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)
# make some labels invisible
plt.setp(axHistx.get_xticklabels() + axHisty.get_yticklabels(),
visible=False)
# now determine nice limits by hand:
binwidth = 0.25
lim = ( int(xymax/binwidth) + 1) * binwidth
bins = np.arange(-lim, lim + binwidth, binwidth)
histtype = "bar"
axHistx.hist(x_vals, bins=bins, histtype= histtype, color=colors)
axHisty.hist(y_vals, bins=bins, orientation='horizontal',histtype= histtype, color=colors)
# the xaxis of axHistx and yaxis of axHisty are shared with axScatter,
# thus there is no need to manually adjust the xlim and ylim of these
# axis.
#axHistx.axis["bottom"].major_ticklabels.set_visible(False)
for tl in axHistx.get_xticklabels():
tl.set_visible(False)
axHistx.set_yticks([0, 50, 100])
#axHisty.axis["left"].major_ticklabels.set_visible(False)
for tl in axHisty.get_yticklabels():
tl.set_visible(False)
axHisty.set_xticks([0, 50, 100])
plt.draw()
plt.show()
Thank You for help!
Edit:
Here is the images which I receive in windows environment with matplotlib 1.0.0.
With histtype="bar" I have this:
and with histtype="stepfilled" I have this:
The documentation only mentions special cases for multiple data when using 'bar' and 'barstacked', which I would assume means that this isn't properly implemented for the other two types. Changing your code to add multiple histograms instead of just one worked for me:
histtype = "stepfilled"
for i in xrange(len(x_vals)):
axHistx.hist(x_vals[i], bins=bins, histtype= histtype, color=colors[i])
axHisty.hist(y_vals[i], bins=bins, orientation='horizontal',histtype= histtype, color=colors[i])

Categories