Shading the union of areas greater than 2 lines - numpy

Could you please help me shade the area highlighted as red below.
Everything I have tried or read on this topic using "fill_between" will fill the area between the lines.
However, this actually needs to shade the area greater than Y=X UNION'd with the area great than 1/X (which is shaded as red in my crude example.
As you can see, my attempts always result in some combination of the area between the lines being filled.
Code:
x = np.linspace(0.0,15.0,150)
y = x
y_ = 1/x
d = scipy.zeros(len(y))
fig, ax = plt.subplots(1,1)
ax.plot(x, y)
ax.plot(x, y_)
ax.legend(["y >= x", "y >= 1/x"])
ax.fill_between(x, y, y_, where=y_>d, alpha=0.5, interpolate=True)
Thank you for the suggestions
Regards,
F.

What about this?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.0,15.0,150)
y = x
y_ = 1/x
ceiling = 100.0
max_y = np.maximum(y, y_)
d = np.zeros(len(y))
fig, ax = plt.subplots(1,1)
ax.plot(x, y)
ax.plot(x, y_)
ax.legend(["y >= x", "y >= 1/x"])
plt.ylim((0, 10))
ax.fill_between(x, max_y, ceiling, where=y_>d, alpha=0.5, interpolate=True)
plt.show()
i.e. take the max (np.maximum) of the two functions, then fill the area between this new max function and some suitably high ceiling.
Of course you also have to manually set the y-lim or your plot will reach the ceiling value in y.

Related

Surface Plot of a function B(x,y,z)

I have to plot a surface plot which has axes x,y,z and a colormap set by a function of x,y,z [B(x,y,z)].
I have the coordinate arrays:
x=np.arange(-100,100,1)
y=np.arange(-100,100,1)
z=np.arange(-100,100,1)
Moreover, my to-be-colormap function B(x,y,z) is a 3D array, whose B(x,y,z)[i] elements are the (x,y) coordinates at z.
I have tried something like:
Z,X,Y=np.meshgrid(z,x,y) # Z is the first one since B(x,y,z)[i] are the (x,y) coordinates at z.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
img = ax.scatter(Z, X, Y, c=B(x,y,z), cmap=plt.hot())
fig.colorbar(img)
plt.show()
However, it unsurprisingly plots dots, which is not what I want. Rather, I need a surface plot.
The figure I have obtained:
The kind of figure I want:
where the colors are determined by B(x,y,z) for my case.
You have to:
use plot_surface to create a surface plot.
your function B(x, y, z) will be used to compute the color parameter, a number assigned to each face of the surface.
the color parameter must be normalized between 0, 1. We use matplotlib's Normalize to achieve that.
then, you create the colors by applying the colormap to the normalized color parameter.
finally, you create the plot.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
t = np.linspace(0, 2*np.pi)
p = np.linspace(0, 2*np.pi)
t, p = np.meshgrid(t, p)
r1, r2 = 1, 3
x = (r2 + r1 * np.cos(t)) * np.cos(p)
y = (r2 + r1 * np.cos(t)) * np.sin(p)
z = r1 * np.sin(t)
color_param = np.sin(x / 2) * np.cos(y) + z
cmap = cm.jet
norm = Normalize(vmin=color_param.min(), vmax=color_param.max())
norm_color_param = norm(color_param)
colors = cmap(norm_color_param)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.plot_surface(x, y, z, facecolors=colors)
ax.set_zlim(-4, 4)
plt.show()

Shaded region up to the end of frame with Matplotlib

Using Matplotlib I am trying to shade the region y > N, with N some number.
The problem is that I am not able to have the shaded region going to the end of the frame.
Here is a simple example.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1,1)
x = [i for i in range(0,100,10)]
ax.plot(x,x,'-o')
N=110
ylim = max(ax.get_ylim())
ax.axhspan(N,ylim,alpha=.5)
plt.show()
The result is this:
How to have the shaded region not stopping, but arriving up to the end of the frame?
ax.autoscale() can come in handy here. Default, matplotlib automatically adapts the limits of the axes every time something is added. Normally also some padding is added to leave some free space above and below (and left and right).
Calling ax.autoscale(enable=True, axis='y', tight=True) changes this behavior for the y-axis, forcing "tight" limits, so without padding.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [i for i in range(0, 100, 10)]
ax.plot(x, x, '-o')
N = 110
ylim1 = max(ax.get_ylim())
ax.autoscale(enable=True, axis='y', tight=True)
ax.axhspan(N, ylim1, alpha=.5)
plt.show()
Alternatively, you could collect the limits before calling axhspan and setting them manually afterwards:
ax.plot(x, x, '-o')
N = 110
ylim0, ylim1 = ax.get_ylim()
ax.axhspan(N, ylim1, alpha=.5)
ax.set_ylim(ylim0, max(N, ylim1))

How to fill histogram with color gradient where a fixed point represents the middle of of colormap

This code
import numpy as np
import matplotlib.pyplot as plt
def randn(n, sigma, mu):
return sigma * np.random.randn(n) + mu
x = randn(1000, 40., -100.)
cm = plt.cm.get_cmap("seismic")
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
_, bins, patches = ax.hist(x,color="r",bins=30)
bin_centers = 0.5*(bins[:-1]+bins[1:])
col = bin_centers - min(bin_centers)
col /= max(col)
for c, p in zip(col, patches):
plt.setp(p, "facecolor", cm(c))
plt.savefig("b.png", dpi=300, bbox_inches="tight")
produces the following histograms
I want to use the diverging colormap seismic and would like to have all bars representing the occurrence of negative numbers to be bluish and all bars representing positive numbers reddish. Around zero the bars should always be white. Therefore the first graph should be mostly reddish and the last one should be mostly bluish. How can I achieve that?
If this is about visual appearance only, you can normalize your colors to the range between the maximum absolute value and its negative counterpart, such that zero is always in the middle (max |bins|).
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 6.4,4
def randn(n, sigma, mu):
return sigma * np.random.randn(n) + mu
x1 = randn(999, 40., -80)
x2 = randn(750, 40., 80)
x3 = randn(888, 16., -30)
def hist(x, ax=None):
cm = plt.cm.get_cmap("seismic")
ax = ax or plt.gca()
_, bins, patches = ax.hist(x,color="r",bins=30)
bin_centers = 0.5*(bins[:-1]+bins[1:])
maxi = np.abs(bin_centers).max()
norm = plt.Normalize(-maxi,maxi)
for c, p in zip(bin_centers, patches):
plt.setp(p, "facecolor", cm(norm(c)))
fig, axes = plt.subplots(nrows=3, sharex=True)
for x, ax in zip([x1,x2,x3], axes):
hist(x,ax=ax)
plt.show()
I have an alternative answer for a different use case. I wanted to have the different colours from the divergent colormap be dynamically mapped to their respective "width" on either side of the divergence point. Additionally, I wanted to explicitly set the divergence point (in my case, 1).
I achieved this by modifying the answer from #ImportanceofBeingErnest, although in the end I didn't need to do any normalization, I just used two plots on the same figure, and chose the sequential colormaps which, when put end-to-end, re-formed the target divergent colormap.
def hist2(x, vmin, vmax, cmmap_name, ax=None,):
cm = plt.cm.get_cmap(cmmap_name)
ax = ax or plt.gca()
_, bins, patches = ax.hist(x,color="r",bins=50)
bin_centers = 0.5*(bins[:-1]+bins[1:])
norm = plt.Normalize(vmin, vmax)
for c, p in zip(bin_centers, patches):
plt.setp(p, "facecolor", cm(norm(c)))
data = <YOUR DATA>
left_data = [i for i in data if i < <YOUR DIVERGENCE POINT>]
right_data = [i for i in data if i >= <YOUR DIVERGENCE POINT>]
fig, ax = plt.subplots(nrows=1)
hist2(left_data, min(left_data), max(left_data), "YlOrRd_r", ax=ax)
hist2(right_data, min(right_data), max(right_data), "YlGn", ax=ax)
plt.show()
Some of my results:

group boxplot histogramming

I would like to group my data and to plot the boxplot for all the groups. There are many questions and answer about that, my problem is that I want to group by a continuos variable, so I want to histogramming my data.
Here what I have done. My data:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
x = np.random.chisquare(5, size=100000)
y = np.random.normal(size=100000) / (0.05 * x + 0.1) + 2 * x
f, ax = plt.subplots()
ax.plot(x, y, '.', alpha=0.05)
plt.show()
I want to study the behaviour of y (location, width, ...) as a function of x. I am not interested in the distribution of x so I will normalized it.
f, ax = plt.subplots()
xbins = np.linspace(0, 25, 50)
ybins = np.linspace(-20, 50, 50)
H, xedges, yedges = np.histogram2d(y, x, bins=(ybins, xbins))
norm = np.sum(H, axis = 0)
H /= norm
ax.pcolor(xbins, ybins, np.nan_to_num(H), vmax=.4)
plt.show()
I can plot histogram, but I want boxplot
binning = np.concatenate(([0], np.sort(np.random.random(20) * 25), [25]))
idx = np.digitize(x, binning)
data_to_plot = [y[idx == i] for i in xrange(len(binning))]
f, ax = plt.subplots()
midpoints = 0.5 * (binning[1:] + binning[:-1])
widths = 0.9 * (binning[1:] - binning[:-1])
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorLocator = MultipleLocator(2)
ax.boxplot(data_to_plot, positions = midpoints, widths=widths)
ax.set_xlim(0, 25)
ax.xaxis.set_major_locator(majorLocator)
ax.set_xlabel('x')
ax.set_ylabel('median(y)')
plt.show()
Is there an automatic way to do that, like ax.magic(x, y, binning)? Is there a better way to do that? (Have a look to https://root.cern.ch/root/html/TProfile.html for example, which plot the mean and the error of the mean as error bars)
In addition, I want to minize the memory footprint (my real data are much more than 100000), I am worried about data_to_plot, is it a copy?

Set color of points with -inf using scatter

I'm drawing a scatter plot in which I'm specifying the color of each point:
ax.scatter(x, y, c=z)
The problem is that some values of z are -inf, and these points simply aren't plotted. I'd like to set them to some color. I tried this:
cm = mpl.cm.get_cmap()
cm.set_under('k',1.0)
cm.set_bad('purple',1.0)
ax.scatter(x, y, c=z, cmap=cm)
but there's still no points shown for the -inf values.
I think it's easiest to overplot the points at infinity:
cm = mpl.cm.get_cmap()
cm.set_under('k',1.0)
cm.set_bad('purple',1.0)
ax.scatter(x, y, c=z)
ax.scatter(x[numpy.isinf(z)], y[numpy.isinf(z)], c='b')
If you want to exclude positive infinity, this seems to work:
cm = mpl.cm.get_cmap()
cm.set_under('k',1.0)
cm.set_bad('purple',1.0)
ax.scatter(x, y, c=z)
indices = numpy.isinf(z) & (z < 0)
ax.scatter(x[indices], y[indices], c='b')
All code untested though.