我正在尝试将第一行末尾的雷达图向下移动,使其位于第一行和第二行之间的中间位置。我什至不知道从哪里开始尝试这种方法。我已经添加了所需的情节位置。
一些重现此问题的代码在这里:
import math
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
def make_spider(row, title, color, ax=None):
categories=list(radar_dfs)
N = len(categories)
angles = np.arange(N+1)/N*2*np.pi
values=radar_dfs.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=0.4)
radar_dfs = 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}})
fig, axes = plt.subplots(2, 3, subplot_kw=dict(polar=True), sharey=True,
figsize=(28,20))
palette =['#79BD9A','#69D2E7','#F38630', '#547980','#EDC951']
labels = ['A', 'B', 'C', 'D', 'E']
row_one = axes.flatten()
for row, (ax, letter, col) in enumerate(zip(row_one, labels, palette)):
make_spider( row = row, title='Group ' + str(letter), color=col, ax=ax)
fig.delaxes(axes[1][2])
plt.subplots_adjust(wspace=.4, hspace=.3)
plt.show()
有人有什么建议吗?
答案 0 :(得分:2)
一种方法是使用plt.subplot2grid()
创建每个子图并手动定位它们。可以使用的技巧是使每个图表的行数和高度加倍,从而可以更精确地调整位置。
import math
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
def make_spider(row, title, color, ax=None):
categories=list(radar_dfs)
N = len(categories)
angles = np.arange(N+1)/N*2*np.pi
values=radar_dfs.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=0.4)
radar_dfs = 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}})
palette =['#79BD9A','#69D2E7','#F38630', '#547980','#EDC951']
labels = ['A', 'B', 'C', 'D', 'E']
fig = plt.figure(figsize=(28,20))
position = [[0,0], [0,1], [1,2], [2,0], [2,1]]
axes = []
for row, (letter, col) in enumerate(zip(labels, palette)):
ax = plt.subplot2grid([4,3], position[row], rowspan=2, colspan=1, **{'polar': True}, sharey=axes[0] if row else None)
axes.append(ax)
make_spider( row = row, title='Group ' + str(letter), color=col, ax=ax)
plt.subplots_adjust(wspace=.4, hspace=.3)
plt.show()
使用此解决方案,必须手动设置sharey
。也许更干净的方法是使用ax.get_shared_y_axes().join(*axes)
(选中here)。
希望有帮助。