我正在制作一张极 map ,其中的扁虱并不均匀地分布在圆圈周围.有几个非常好的问答对处理均匀分布的答案,它们都使用divide up the circle方法.E.g. this.
我想知道是否有可能使用标签中烘焙的转换来按我想要的方式旋转文本.
我可以做到这一点,但我不知道如何正确地锚定它.执行此操作的代码如下:
for tick in plt.xticks()[1]:
tick._transform = tick._transform + mpl.transforms.Affine2D().rotate_deg_around(0, 0, 10)
其输出如下所示:
鉴于我想要这样的输出:
(摘自上述链接的问题)
显然,我需要90度旋转,而不是10度,但90度旋转会让它脱离画布.
这种方法可行吗,或者我需要重新判断我的战略吗?
完整的代码块如下所示:
import random
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
one_person = {
"Human": {
"Collaboration": 4,
"Growth Mindset": 3,
"Inclusion": 5,
"Project and Studio Life": 2,
},
"Tectonics": {
"Office Manual and Procedures": 3,
"Documentation Standards": 3,
"Site Stage Services": 2,
"External and Public Domain Works": 2,
"Structure": 3,
"Enclosure": 2,
"Waterproofing (int. and ext.)": 3,
"Interiors": 1,
"Structure and Services": 2,
},
"Technology": {
"Bluebeam": 2,
"Confluence": 3,
"Drawing on screens": 0,
"dRofus": 0,
"Excel": 2,
"Grasshopper": 1,
"InDesign": 2,
"Outlook": 2,
"Python": 5,
"Rhino": 1,
"Teams": 2,
"Timesheets and expenses": 3,
},
"Regenerative": {
"REgenerative Design": 3,
"Materials and Embodied Carbon practice": 1,
"Materials and Embodied Carbon analysis": 2,
"Energy": 3,
"Resilience": 1,
"Rating Systems": 2,
},
"Design": {
"Predesign - Briefing, Stakeholder Engagement & Establishing Project Values": 2,
"Predesign - Feasibility Studies And Strategic Organisational Planning": 3,
"Initiating Design": 2,
"Conserving Design": 3,
"Design Communication - Written": 2,
"Design Communication - Visual": 4,
"Design Communication - Verbal": 3,
},
"Connecting with country": {"Connecting with Country": 2},
}
colours = [
"b", # blue.
"g", # green.
"r", # red.
"c", # cyan.
"m", # magenta.
"y", # yellow.
"k", # black.
# "w", # white.
]
def draw_radar(data, colour_letters, person_name=""):
"""Draw the graph.
Based substanitally on this SO thread:
https://stackoverflow.com/questions/60563106/complex-polar-plot-in-matplotlib
"""
# not really sure why -1, but if you don't you get an empty segment
num_areas = len(data) - 1
running_total = 0
thetas = {}
for key, value in data.items():
this_area_num_points = len(value)
this_area_theta = ((2 * np.pi) / num_areas) / (this_area_num_points)
thetas[key] = []
for i in range(len(value)):
thetas[key].append((i * this_area_theta) + running_total)
running_total += (2 * np.pi) / num_areas
labels = []
for key, value in data.items():
for area, score in value.items():
labels.append(f"{score} {key}: {area}")
for name, theta_list in thetas.items():
individual_scores = list(data[name].values())
colour = random.choice(colour_letters)
if len(theta_list) > 1:
plt.polar(theta_list, individual_scores, c=colour, label=name)
elif len(theta_list) == 1:
plt.scatter(theta_list, individual_scores, c=colour, label=name)
plt.yticks(np.arange(-5, 5), [""] * 5 + list(range(5)))
plt.xticks(
np.concatenate(tuple(list(thetas.values()))),
labels,
transform_rotates_text=True,
)
for tick in plt.xticks()[1]:
tick._transform = tick._transform + mpl.transforms.Affine2D().rotate_deg_around(
0, 0, 10
)
if person_name:
plt.title = f"Competency for {person_name}"
plt.savefig("radar.png")
draw_radar(one_person, colours)