How can I adjust the whitespace between some subplots? In the example below, let's say I want to eliminate all whitespace between the 1st and 2nd subplots as well as between the 3rd and 4th and increase the space between the 2nd and 3rd?
import matplotlib.pyplot as plt
import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, ax = plt.subplots(4,figsize=(10,10),sharex=True)
ax[0].plot(x, y)
ax[0].set_title('Panel: A')
ax[1].plot(x, y**2)
ax[2].plot(x, y**3)
ax[2].set_title('Panel: B')
ax[3].plot(x, y**4)
plt.tight_layout()
To keep the solution close to your code you may use create 5 subplots with the middle one being one forth in height of the others and remove that middle plot.
import matplotlib.pyplot as plt
import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, ax = plt.subplots(5,figsize=(7,7),sharex=True,
gridspec_kw=dict(height_ratios=[4,4,1,4,4], hspace=0))
ax[0].plot(x, y)
ax[0].set_title('Panel: A')
ax[1].plot(x, y**2)
ax[2].remove()
ax[3].plot(x, y**3)
ax[3].set_title('Panel: B')
ax[4].plot(x, y**4)
plt.tight_layout()
plt.show()
You would need to use GridSpec to have different spaces between plots:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f = plt.figure(figsize=(10,10))
gs0 = gridspec.GridSpec(2, 1)
gs00 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[0], hspace=0)
ax0 = f.add_subplot(gs00[0])
ax0.plot(x, y)
ax0.set_title('Panel: A')
ax1 = f.add_subplot(gs00[1], sharex=ax0)
ax1.plot(x, y**2)
gs01 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[1], hspace=0)
ax2 = f.add_subplot(gs01[0])
ax2.plot(x, y**3)
ax2.set_title('Panel: B')
ax3 = f.add_subplot(gs01[1], sharex=ax0)
ax3.plot(x, y**4)
plt.show()
Related
when plotting by below code I am getting c,d,e plots but I am getting only the last plot for plt.plot
def normalize(x):
return (x - x.min(0)) / x.ptp(0)
c=sns.distplot(mk[0]['mass'], hist=True, label='p', rug=True)
d=sns.distplot(mk[1]['mass'], hist=True, label='q', rug=True)
e=sns.distplot(mk[2]['mass'], hist=True, label='r', rug=True)
datadist=[c,d,e]
xd=dict()
yd2=dict()
for i in datadist:
line = i.get_lines()[0]
xd[i] = line.get_xdata()
yd = line.get_ydata()
yd2[i] = normalize(yd)
plt.plot(xd[c], yd2[c],color='black')
plt.plot(xd[d], yd2[d],color='yellow')
plt.plot(xd[e], yd2[e],color='green')
sns.distplot() returns the ax (the subplot) on which the histogram was drawn. All 3 are drawn on the same subplot, so the return value is the same three times.
The array lines = ax1.get_lines() contains exactly 3 elements: one for each of the kde curves, so you can extract them as follows:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def normalize(x):
return (x - x.min(0)) / x.ptp(0)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 4))
sns.distplot(np.random.randn(30) + 10, hist=True, label='p', rug=True, ax=ax1, color='black')
sns.distplot(np.random.randn(30) + 15, hist=True, label='q', rug=True, ax=ax1, color='gold')
sns.distplot(np.random.randn(30) + 20, hist=True, label='r', rug=True, ax=ax1, color='green')
for line in ax1.get_lines():
ax2.plot(line.get_xdata(), normalize(line.get_ydata()), color=line.get_color())
plt.show()
Now, if you just want the kde-curves and "normalize" them, you could use scipy.stats import gaussian_kde:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
def normalize(x):
return (x - x.min(0)) / x.ptp(0)
fig, ax = plt.subplots(figsize=(12, 4))
mk0mass = np.random.randn(30) + 10
mk1mass = np.random.randn(30) + 15
mk2mass = np.random.randn(30) + 20
all_mkmass = [mk0mass, mk1mass, mk2mass]
x = np.linspace(min([mki.min() for mki in all_mkmass]) - 2,
max([mki.max() for mki in all_mkmass]) + 2, 1000)
for mki, color in zip(all_mkmass, ['black', 'gold', 'green']):
kde = gaussian_kde(mki)
yd = normalize(kde(x))
ax.plot(x, yd, color=color)
ax.fill_between(x, 0, yd, color=color, alpha=0.3)
plt.show()
As part of displaying the progression of a linear regression model fit, I need to be able to update/refresh an xy plot. Below is a simple script for 3 sets of y data, which need to be shown sequentially. However, they are piled up on top of each other. When fig.canvas.flush_events() is substituted with fig.clear() or fig.clf() the result is a blank plot. What am I - as a newbie -missing?
import torch as tc
import matplotlib.pyplot as plt
tc.manual_seed(1)
X=tc.linspace(-3,3,30)
y0=X.pow(2)+0.5*tc.randn(X.shape[0])
y1=y0/1.3
y2=y0/1.6
y=[y0,y1,y2]
fig=plt.figure()
ax=fig.add_subplot()
ax.set_xlim(-3.3,3.3)
ax.set_ylim(-0.5,9.5)
for i in range(3):
y_new=y[i]
ax.plot(X,y_new,'db')
fig.canvas.draw()
fig.canvas.flush_events()
plt.pause(1)
fig.show()
In your loop, you are creating a new line every time you call ax.plot. The better way is to create a Line2D artist and to update the coordinates of the point in the loop:
(NB i've converted your example to using numpy instead of torch)
import matplotlib.pyplot as plt
import numpy as np
X = np.linspace(-3, 3, 30)
y0 = np.power(X, 2) + 0.5 * np.random.randn(X.shape[0])
y1 = y0 / 1.3
y2 = y0 / 1.6
y = [y0, y1, y2]
fig = plt.figure()
ax = fig.add_subplot()
l, = ax.plot(X, y0, 'db')
ax.set_xlim(-3.3, 3.3)
ax.set_ylim(-0.5, 9.5)
for i in range(3):
y_new = y[i]
l.set_ydata(y_new)
fig.canvas.draw()
plt.pause(1)
plt.show()
For this kind of things, you'd be better off using the FuncAnimation module provided by maptlotlib though:
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
X = np.linspace(-3, 3, 30)
y0 = np.power(X, 2) + 0.5 * np.random.randn(X.shape[0])
y1 = y0 / 1.3
y2 = y0 / 1.6
y = [y0, y1, y2]
fig = plt.figure()
ax = fig.add_subplot()
l, = ax.plot(X, y0, 'db')
ax.set_xlim(-3.3, 3.3)
ax.set_ylim(-0.5, 9.5)
def animate(y_new):
l.set_ydata(y_new)
return l,
ani = animation.FuncAnimation(fig, func=animate, frames=y, interval=1000)
fig.show()
A simple example is as follows:
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
N = 1000
x = np.linspace(1, 5, N)
y = npr.randint(1e16, size = N) / 1e16
y = np.sort(y)
fig, ax = plt.subplots()
ax.loglog(x, y, '.')
ax.grid(True, 'both')
Where I want to replace the xticks. So far everything I tried, failed to work:
ax.set_xticks([2, 3, 4], ['a', 'b', 'c'])
ax.xaxis.set_ticks_position('none')
ax.set_xticks([])
None of the above showed any effect. My goal is to replace the ticks with custom defined ticks (strings or integers). So instead of 2 x 10⁰ it should only be 2. Similar for other xticks.
Probably this is what you're after:
import numpy as np
import matplotlib.pyplot as plt
N = 1000
x = np.linspace(1, 5, N)
y = np.random.rand(N)
y = np.sort(y)
fig, ax = plt.subplots()
ax.loglog(x, y, '.')
ax.grid(True, 'both')
ax.set_xticks([2, 3, 4])
ax.set_xticklabels(['a', 'b', 'c'])
ax.minorticks_off()
plt.show()
I plan to create a figure in matplotlib, with a 3D surface on the left and its corresponding contour map on the right.
I used subplots but it only show the contour map (with blank space for the surface), and a separate figure for the surface.
Is it possible to create these plots in one figure side-by side?
EDIT: The code is as follows:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)
fig, (surf, cmap) = plt.subplots(1, 2)
fig = plt.figure()
surf = fig.gca(projection='3d')
surf.plot_surface(x,y,z)
cmap.contourf(x,y,z,25)
plt.show()
I guess it's hard to use plt.subplots() in order to create a grid of plots with different projections.
So the most straight forward solution is to create each subplot individually with plt.subplot.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)
ax = plt.subplot(121, projection='3d')
ax.plot_surface(x,y,z)
ax2 = plt.subplot(122)
ax2.contourf(x,y,z,25)
plt.show()
Of course one may also use the gridspec capabilities for more sophisticated grid structures.
I'm creating a plot consisting of several subplots in matplotlib, like this one:
But for some reason, I get weird Zeros on the y-axis (actually on both sides of the plot):
They don't seem to be ticks, since the ax1.get_yaxis().set_ticks([]) statement does not affect them.
Any ideas why I get these and how I can get rid of them?
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
subplots_adjust(hspace=0.000)
groups = ['01', '03', '05', '07']
for i in range(len(groups)):
x = np.linspace(0, 2*np.pi,400)
y = np.sin(x**2)
ax1 = subplot(len(groups),1,i+1)
ax1.scatter(x, y, s=20, c='b', marker='o')
plt.xlim(xmin=0,xmax=1)
ax1.get_yaxis().set_ticks([])
plt.show()
plt.close()
Thank you for any help!
These are just leftovers from the x ticks at 0.0 and 1.0:
import matplotlib.pyplot as plt
#from pylab import * # don't do it, btw
import numpy as np
groups = ['01' , '03', '05', '07']
fig = plt.figure()
ax = []
for i in range(len(groups)):
ax.append( fig.add_subplot( len(groups), 1, i+1 ) )
fig.subplots_adjust(hspace=0.000)
for i in range(len(groups)):
x = np.linspace(0, 2*np.pi,400)
y = np.sin(x**2)
ax[i] = plt.subplot(len(groups),1,i+1)
ax[i].scatter(x, y, s=20, c='b', marker='o')
ax[i].get_yaxis().set_ticks([])
ax[i].set_xlim([0.001,0.9999]) # <<<<========== here
plt.show()