Custom background sections for matplotlib figure - matplotlib

I would like to define colors sections (blue: [0-15000], green: [15000-23000], red[23000,]) that should be used for y-values. Is it somehow possible in matplotlib?

You can color regions on a matplotlib plot using collections.BrokenBarHCollection:
import matplotlib.pyplot as plt
import matplotlib.collections as collections
fig = plt.figure()
ax = fig.add_subplot(111)
# Plot your own data here
x = range(0, 30000)
y = range(0, 30000)
ax.plot(x, y)
xrange = [(0, 30000)]
yrange1 = (0, 15000)
yrange2 = (15000, 23000)
yrange3 = (23000, 30000)
c1 = collections.BrokenBarHCollection(xrange, yrange1, facecolor='blue', alpha=0.5)
c2 = collections.BrokenBarHCollection(xrange, yrange2, facecolor='green', alpha=0.5)
c3 = collections.BrokenBarHCollection(xrange, yrange3, facecolor='red', alpha=0.5)
ax.add_collection(c1)
ax.add_collection(c2)
ax.add_collection(c3)
plt.show()

Related

Adding patch distorts alignment

I am working with the following image:
from matplotlib import cbook
import matplotlib.patches as mpatches
from matplotlib.axes._base import _TransformedBoundsLocator
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
# a numpy array of 15x15
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
gs = GridSpec(2, 3)
fig = plt.figure(figsize=(3*3,2*3))
ax1 = fig.add_subplot(gs[:2, :2])
ax2 = fig.add_subplot(gs[1, 2])
Z2 = np.zeros((150, 150))
ny, nx = Z.shape
Z2[30:30+ny, 30:30+nx] = Z
ax1.imshow(Z2)
ax1.set_aspect("equal")
ax2.set_aspect("equal")
plt.tight_layout()
plt.show()
output:
As shown in the image, the x-axis of both plots are aligned. However, when I am adding a patch to the first plot the alignment becomes distorted:
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
gs = GridSpec(2, 3)
fig = plt.figure(figsize=(3*3,2*3))
ax1 = fig.add_subplot(gs[:2, :2])
ax2 = fig.add_subplot(gs[1, 2])
Z2 = np.zeros((150, 150))
ny, nx = Z.shape
Z2[30:30+ny, 30:30+nx] = Z
ax1.imshow(Z2)
x, y, width, height = 30, 30, 15, 15
ex, ey = (0,1)
xy_data = x + ex * width, y + ey * height
p = mpatches.ConnectionPatch(
xyA=(0,1), coordsA=ax2.transAxes,
xyB=xy_data, coordsB=ax1.transData)
ax1.add_patch(p)
ax1.set_aspect("equal")
ax2.set_aspect("equal")
plt.tight_layout()
plt.show()
output:
Why is this? How can I add a patch whilst retaining the original layout?

jupyter notebook matplotlib show plot and then plot on the origin figure

I want to plot a white plot with two axes, show it to the user, then add a line to the white plot with two axes, show it to the user, then add some dot to the line, then show it to the user. How can I do this without copying the code again and again?
What I'm doing now is in the first code chunk
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
fig = plt.figure(figsize=(5,5))
ax = plt.axes()
ax.set_xlabel('cat')
ax.set_ylabel('dog')
plt.title("Set of 2 animals")
plt.show()
then in the second code chunk
fig = plt.figure(figsize=(5,5))
ax = plt.axes()
x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)
ax.set_xlabel('cat')
ax.set_ylabel('dog')
plt.title("Set of 2 animals")
plt.show()
then in the third code chunk
fig = plt.figure(figsize=(5,5))
ax = plt.axes()
x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)
ax.set_xlabel('cat')
ax.set_ylabel('dog')
plt.title("Set of 2 animals")
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plt.show()
What I'm looking for is things like in the first code chunk
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
fig = plt.figure(figsize=(5,5))
ax = plt.axes()
ax.set_xlabel('cat')
ax.set_ylabel('dog')
plt.title("Set of 2 animals")
plt.show()
then in the second code chunk
add line directly without duplicating the code for making axes
plt.show()
then in the third code chunk
add point directly without duplicating the code for making axes and lines
plt.show()
Update: I actually figured out the answer.
def plot(step):
fig = plt.figure(figsize=(5,5))
ax = plt.axes()
ax.set_xlabel('cat')
ax.set_ylabel('dog')
plt.title("Set of 2 animals")
if step>=1:
x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)
if step>=2:
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plot.show()
should be able to solve the problem.

How can I update the title in my matplotlib Animation?

I'm trying to set the title as 'Electric Field of mode (i)', where i goes from 1 to N, but I don't find the way. After I import Numpy, Matplotlib, cmath and Animation, and define the parameters and neccesary funtions (modo) I have:
fig = plt.figure()
subplot = plt.axes(xlim=(0, 60*(10**(-15))), xlabel=("t[s]"), ylim=(-10, 10), ylabel=("Amplitud [u.a.]"))
modo, = subplot.plot([], [], lw=2)
def init1():
modo.set_data([],[])
return modo,
def animacion1(i):
EiR, Ei = Modo(Eo, wo, dw, t, phio, i)
modo.set_data(t,EiR)
return modo,
anim1 = animation.FuncAnimation(fig, animacion1, init_func=init1, frames=N, interval=20, blit=True, save_count=20000)
plt.show()
Here is a minimal example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x = np.linspace(0, 7.5, 100)
y1 = np.sin(x)
fig, ax = plt.subplots()
line1, = ax.plot(x, y1, color='orange')
def update(num):
line1.set_data(x[:num], y1[:num])
ax.set_title(f'{num}')
return line1
ani = animation.FuncAnimation(fig, func=update, frames=len(x),
interval = 50)
plt.show()
In your case you need to do subplot.set_title(f'Electric Field of mode ({i})') in your animacion1() method.

Legend location in matplotlib subplot

I am trying to create subplots on (6X3) grid. I am having a problem with the location of legend. The legend is common to all subplot. The lgend is now overlapping with the y-axis label
I tried with removing the constrained_layout=True option. But this keeps a lot of white space between legend and subplots.
import numpy as np
import matplotlib.pyplot as plt
#plt.rcParams["font.family"] = "Times New Roman"
#plt.rcParams.update({'font.size': 12})
font = {'family' : 'Times New Roman',
'size' : 14}
plt.rc('font', **font)
t = np.linspace(0,10, num=200)
fig, axs = plt.subplots(6, 3, figsize=(12,16))#, constrained_layout=True)
i = 0 # i = 0 for x = 0.25; i = 3 for x = -0.25
j = 6 # j = 6 for x = 0.25; j = 9 for x = -0.25
#%%
solution = np.loadtxt(open("sequential_legs=01.csv", "rb"), delimiter=",", skiprows=0)
axs[0, 0].plot(t, solution[:,i],'r-')
axs[0, 0].plot(t, solution[:,i+1],'g-')
axs[0, 0].plot(t, solution[:,i+2],'b-')
axs[0, 0].plot(t, solution[:,j],'r--')
axs[0, 0].plot(t, solution[:,j+1],'g--')
axs[0, 0].plot(t, solution[:,j+2],'b--')
axs[0, 0].set_title('DNN-S (p = 1)', fontsize=14)
axs[0, 0].set_xlim([0, 10])
(repeated for each grid)
line_labels = ["$y_1$ (True)","$y_2$ (True)", "$y_3$ (True)", "$y_1$ (ML-Pred)","$y_2$ (ML-Pred)", "$y_3$ (ML-Pred)"]
plt.figlegend( line_labels, loc = 'lower center', borderaxespad=0.1, ncol=6, labelspacing=0., prop={'size': 13} ) #bbox_to_anchor=(0.5, 0.0), borderaxespad=0.1,
for ax in axs.flat:
ax.set(xlabel='Time', ylabel='Response')
for ax in axs.flat:
ax.label_outer()
fig.savefig('LSE_X=025.pdf', bbox_inches = 'tight')
constrained_layout does not take figure legends into account. The easiest option I currently see here is to use tight_layout to have the subplots distribute nicely in the figure, and after that, add some space for the legend manually.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(6, 3, figsize=(12,8), sharex=True, sharey=True,
constrained_layout=False)
labels = [f"Label {i}" for i in range(1,5)]
for ax in axs.flat:
for i, lb in enumerate(labels):
ax.plot([1,2], [0,i+1], label=lb)
ax.set_xlabel("x label")
ax.label_outer()
fig.tight_layout()
fig.subplots_adjust(bottom=0.1) ## Need to play with this number.
fig.legend(labels=labels, loc="lower center", ncol=4)
plt.show()

How to plot secondary_y in log scale in pyplot

I would like to have two lines (or better scatter plots) in one plot.
The Secondary Y line should be in log scale. How to do it with python matplotlib?
You can create a second y axis by using ax2 = ax.twinx(). You can then, as tacaswell pointed out in the comments, set this second axis to log scale.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(5,3))
ax = fig.add_subplot(111)
ax2 = ax.twinx()
x = np.random.rand(10)
y = np.random.rand(10)
y2 = np.random.randint(1,10000, size=10)
l1 = ax.scatter(x,y, c="b", label="lin")
l2 = ax2.scatter(x,y2, c="r", label="log")
ax2.set_yscale("log")
ax2.legend(handles=[l1, l2])
ax.set_ylabel("Linear axis")
ax2.set_ylabel("Logarithmic axis")
plt.tight_layout()
plt.show()