我试图使子图共享相同的轴,因为它们当前是不同的(图中的圆圈似乎未完全对齐)。当我尝试将sharex=True
传递到ax = plt.subplot(1, 5, row+1, polar=True, sharex=True)
时,返回错误提示TypeError: cannot create weak reference to 'bool' object
。
这是我的绘图当前的样子,正如您应该看到的,绘图内的轴(圆)未完全对齐,我无法弄清楚如何使用plt.subplot
对齐它们。 / p>
有人有什么建议吗?
代码重现示例:
import matplotlib.pyplot as plt
import pandas as pd
def make_spider(row, title, color):
import math
categories = list(df)
N = len(categories)
angles = [n / float(N) * 2 * math.pi for n in range(N)]
angles += angles[:1]
ax = plt.subplot(1, 5, row+1, polar=True)
plt.xticks(angles[:-1], categories, color='grey', size=8)
values = df.iloc[row].values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
ax.fill(angles, values, color=color, alpha = .4)
plt.gca().set_rmax(.2)
my_dpi = 40
plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=96)
my_palette = plt.cm.get_cmap('Set2', len(df.index)+1)
for row in range(0, len(df.index)):
make_spider( row = row, title='Cluster: ' + str(row), color=my_palette(row) )
数据框:
df = pd.DataFrame.from_dict({"no_rooms":{"0":-0.3470532925,"1":-0.082144001,"2":-0.082144001,"3":-0.3470532925,"4":-0.3470532925},"total_area":{"0":-0.1858487321,"1":-0.1685491141,"2":-0.1632483955,"3":-0.1769700284,"4":-0.0389887094},"car_park_spaces":{"0":-0.073703681,"1":-0.073703681,"2":-0.073703681,"3":-0.073703681,"4":-0.073703681},"house_price":{"0":-0.2416123064,"1":-0.2841806825,"2":-0.259622004,"3":-0.3529449824,"4":-0.3414842657},"pop_density":{"0":-0.1271390651,"1":-0.3105853643,"2":-0.2316607937,"3":-0.3297832328,"4":-0.4599021194},"business_rate":{"0":-0.1662745006,"1":-0.1426329043,"2":-0.1577528867,"3":-0.163560133,"4":-0.1099718326},"noqual_pc":{"0":-0.0251535462,"1":-0.1540641646,"2":-0.0204666924,"3":-0.0515740013,"4":-0.0445135996},"level4qual_pc":{"0":-0.0826103951,"1":-0.1777759951,"2":-0.114263357,"3":-0.1787044751,"4":-0.2709496389},"badhealth_pc":{"0":-0.105481688,"1":-0.1760349683,"2":-0.128215043,"3":-0.1560577648,"4":-0.1760349683}})
答案 0 :(得分:3)
最好创建绘图的共享先验。到已共享轴的图。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dic = {"no_rooms":{"0":-0.347,"1":-0.082,"2":-0.082, "3":-0.347,"4":-0.347},
"total_area":{"0":-0.185,"1":-0.168,"2":-0.163, "3":-0.176,"4":-0.038},
"car_park_spaces":{"0":-0.073,"1":-0.073,"2":-0.073, "3":-0.073,"4":-0.073},
"house_price":{"0":-0.241,"1":-0.284,"2":-0.259,"3":-0.352,"4":-0.341},
"pop_density":{"0":-0.127,"1":-0.310,"2":-0.231,"3":-0.329,"4":-0.459},
"business_rate":{"0":-0.166,"1":-0.142,"2":-0.157,"3":-0.163,"4":-0.109},
"noqual_pc":{"0":-0.025,"1":-0.15,"2":-0.020,"3":-0.051,"4":-0.044},
"level4qual_pc":{"0":-0.082,"1":-0.17,"2":-0.114,"3":-0.178,"4":-0.270},
"badhealth_pc":{"0":-0.105,"1":-0.176,"2":-0.128,"3":-0.156,"4":-0.176}}
df = pd.DataFrame.from_dict(dic)
def make_spider(row, title, color, ax=None):
categories = list(df)
N = len(categories)
angles = np.arange(N+1)/N*2*np.pi
ax.set_xticks(angles[:-1])
ax.set_xticklabels(categories, color='grey', size=8)
ax.tick_params(labelleft=True)
values = df.iloc[row].values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
ax.fill(angles, values, color=color, alpha = .4)
fig, axes = plt.subplots(ncols=len(df.index), subplot_kw=dict(polar=True), sharey=True,
figsize=(15,8))
my_palette = plt.cm.get_cmap('Set2', len(df.index)+1)
for row, ax in enumerate(axes):
make_spider( row = row, title='Cluster: ' + str(row), color=my_palette(row), ax=ax )
plt.show()
答案 1 :(得分:2)
必须为所有轴设置相同的y_lim
/ r_lim
和y_ticks
/ r_ticks
。例如,可以通过将最后一个轴参考传递到plt.subplot
来为所有轴设置sharey
来完成:
def make_spider(row, title, color, last_ax=None):
import math
categories = list(df)
N = len(categories)
angles = [n / float(N) * 2 * math.pi for n in range(N)]
angles += angles[:1]
# add last ax as sharey here:
ax = plt.subplot(1, 5, row+1, polar=True, sharey=last_ax)
plt.xticks(angles[:-1], categories, color='grey', size=8)
values = df.iloc[row].values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
ax.fill(angles, values, color=color, alpha = .4)
plt.gca().set_rmax(.2)
# return axes to store them
return plt.gca()
my_dpi = 40
plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=96)
my_palette = plt.cm.get_cmap('Set2', len(df.index)+1)
axs = [] # store axes
for row in range(0, len(df.index)):
if row != 0: # if not the first subplot, pass last ax as argument
axs.append(
make_spider(row=row, title='Cluster: ' + str(row), color=my_palette(row),
last_ax=axs[row - 1]))
else:
axs.append(
make_spider(row=row, title='Cluster: ' + str(row), color=my_palette(row)))
或通过将限制/刻度直接传递到图上来
def make_spider(row, title, color, rlim, rticks):
import math
categories = list(df)
N = len(categories)
angles = [n / float(N) * 2 * math.pi for n in range(N)]
angles += angles[:1]
ax = plt.subplot(1, 5, row+1, polar=True)
plt.xticks(angles[:-1], categories, color='grey', size=8)
values = df.iloc[row].values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
ax.fill(angles, values, color=color, alpha = .4)
ax.set_rlim(rlim)
ax.set_rticks(rticks)
# return axes to store them (not needed but may help later)
return ax
my_dpi = 40
plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=96)
my_palette = plt.cm.get_cmap('Set2', len(df.index)+1)
axs = []
for row in range(0, len(df.index)):
axs.append(
make_spider(
row=row, title='Cluster: ' + str(row), color=my_palette(row),
rlim=(-.5, 0), rticks=[-.3, -.2, -.1, 0.]))