I was trying to plot a figure with a combination of a 3d subplot and 3 2d ones. Why do they overlap each other?
Here are my codes:
JavaScript
x
19
19
1
fig = plt.figure(figsize=(10,10))
2
ax = fig.add_subplot(3, 2, 1, projection='3d')
3
ax = plt.axes(projection='3d')
4
ax.scatter3D(extents[0], extents[1], extents[2], color='yellow')
5
6
ax = fig.add_subplot(3, 2, 2)
7
ax = sns.distplot(extents[0], color='red')
8
ax.set_title("Extent_0 Distribution")
9
10
ax = fig.add_subplot(3, 2, 4)
11
ax = sns.distplot(extents[1], color='blue')
12
ax.set_title("Extent_1 Distribution")
13
14
ax = fig.add_subplot(3, 2, 6)
15
ax = sns.distplot(extents[2], color='green')
16
ax.set_title("Extent_2 Distribution")
17
18
plt.show()
19
Advertisement
Answer
- In each group, an
ax
is created withax = fig.add_subplot(3, 2, 1, projection='3d')
, but then you reassign the variable withax = plt.axes(projection='3d')
; this does not plot toax
. - To plot to a specific axes, use the
ax
parameter in the plot methodsns.histplot(df['freq: 1x'], ax=ax)
- Also, upgrade seaborn to version 0.11, because
sns.distplot
is deprecated fordisplot
orhistplot
.
JavaScript
1
28
28
1
import pandas as pd
2
import numpy as np # for sample data
3
4
# sinusoidal sample data
5
sample_length = range(1, 3+1)
6
rads = np.arange(0, 2*np.pi, 0.01)
7
data = np.array([np.sin(t*rads) for t in sample_length])
8
df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=[f'freq: {i}x' for i in sample_length])
9
10
# plot the figures and correctly use the ax parameter
11
fig = plt.figure(figsize=(10,10))
12
ax = fig.add_subplot(3, 2, 1, projection='3d')
13
ax.scatter3D(df['freq: 1x'], df['freq: 2x'], df['freq: 3x'], color='green', s=5)
14
15
ax = fig.add_subplot(3, 2, 2)
16
sns.histplot(df['freq: 1x'], ax=ax)
17
ax.set_title("Extent_0 Distribution")
18
19
ax = fig.add_subplot(3, 2, 4)
20
sns.histplot(df['freq: 2x'], ax=ax)
21
ax.set_title("Extent_1 Distribution")
22
23
ax = fig.add_subplot(3, 2, 6)
24
sns.histplot(df['freq: 3x'], ax=ax)
25
ax.set_title("Extent_2 Distribution")
26
27
plt.tight_layout()
28
Using matplotlib gridspec
- Customizing Figure Layouts Using GridSpec and Other Functions
- Tight Layout guide
- The size of the 3D plot can be increased by changing the number of rows,
nrows
.gs1 = fig.add_gridspec(nrows=4, ncols=3)
JavaScript
1
20
20
1
fig = plt.figure(constrained_layout=False, figsize=(10, 10))
2
gs1 = fig.add_gridspec(nrows=3, ncols=3)
3
4
ax1 = fig.add_subplot(gs1[:-1, :], projection='3d')
5
ax1.scatter3D(df['freq: 1x'], df['freq: 2x'], df['freq: 3x'], color='green', s=10)
6
7
ax2 = fig.add_subplot(gs1[-1, 0])
8
sns.histplot(df['freq: 1x'], kde=True, ax=ax2)
9
ax2.set_title("Extent_0 Distribution")
10
11
ax3 = fig.add_subplot(gs1[-1, 1])
12
sns.histplot(df['freq: 2x'], kde=True, ax=ax3)
13
ax3.set_title("Extent_1 Distribution")
14
15
ax4 = fig.add_subplot(gs1[-1, 2])
16
sns.histplot(df['freq: 3x'], kde=True, ax=ax4)
17
ax4.set_title("Extent_2 Distribution")
18
19
plt.tight_layout()
20