0

The same issue has been reported in this link. However, in my case, I require only one polar subplot in the entire 2*2 figure.

My python is at 3.9.13

The use of subplot_kw=dict(polar=True) converted the entire figure to a polar plot, which was not my intention.

"Here's my code:"

import matplotlib.pyplot as plt
import numpy as np

# random data
data_hist = np.random.randn(1000)

data_line = np.random.randn(50).cumsum()

data_scatter_x = np.random.randn(50)
data_scatter_y = np.random.randn(50)

data_radar = np.random.rand(3) * 2 * np.pi

# 2*2 figure
fig, axs = plt.subplots(2, 2)
axs = axs.flatten()


axs[0].hist(data_hist, bins=50, color='steelblue', edgecolor='black')
axs[0].set_title('Histogram')

axs[1].plot(data_line, color='r', marker='o')
axs[1].set_title('Line plot')

axs[2].scatter(data_scatter_x, data_scatter_y, color='g')
axs[2].set_title('Scatter plot')

axs[3].polar(data_radar, data_radar, marker='o')
axs[3].set_title('Radar plot')

plt.tight_layout()
plt.show()
  • Does the accepted answer to the following question help with your problem? https://stackoverflow.com/questions/33942233/how-do-i-change-matplotlibs-subplot-projection-of-an-existing-axis – fdireito Feb 24 '23 at 12:52

1 Answers1

0

The default projection of plt.subplots is rectilinear, check in the Doc matplotlib.pyplot.subplot, if you want to change the projection in subplots, you need to remove the ax[3] and re-define it.

import matplotlib.pyplot as plt
import numpy as np

# random data
data_hist = np.random.randn(1000)

data_line = np.random.randn(50).cumsum()

data_scatter_x = np.random.randn(50)
data_scatter_y = np.random.randn(50)

r = np.arange(0, 2, 0.1)
theta = 2 * np.pi * r

# 2*2 figure
fig, axs = plt.subplots(2, 2)
axs = axs.flatten()


axs[0].hist(data_hist, bins=50, color='steelblue', edgecolor='black')
axs[0].set_title('Histogram')

axs[1].plot(data_line, color='r', marker='o')
axs[1].set_title('Line plot')

axs[2].scatter(data_scatter_x, data_scatter_y, color='g')
axs[2].set_title('Scatter plot')

axs[3].remove()
axs[3] = plt.subplot(224, projection='polar')
axs[3].plot(theta, r, marker='o')
axs[3].set_title('Radar plot')

plt.tight_layout()
plt.show()

The figure:

enter image description here

HMH1013
  • 1,216
  • 2
  • 13