我正在尝试在matplotlib中的极坐标图上弯曲文本。
以下是我的案例:
import matplotlib.pyplot as plt
import numpy as np
text = ["Electoral Process", "Political Pluralism", "Rule of Law",
"Freedom of expression", "Freedom of believe"]
no_labels =len(text)
angle_size = int(360/no_labels)
#define the number of angles in degrees and convert it to radians
theta = [i/180*np.pi for i in range(0, 360,angle_size) ]
#where to put the labels on the radial axis
radius = [1]*no_labels
#Find the mid point of each angle
mid_point =[i/180*np.pi for i in range(int(angle_size/2), 360, angle_size)]
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'},figsize=(10, 6), facecolor="white")
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_xticklabels([]) #remove the original xlabels
ax.set_yticklabels([]) #remove the original ylabels
# Arrange the grid into number of sales equal parts in degrees
lines = plt.thetagrids(range(0, 360, int(360/len(text))))
#Place the text in the midle of each pie
for m, r, t in zip(mid_point,radius, text):
ax.annotate(t, xy=[m, r], fontsize=12, ha="center", va="center", )
这会生成以下内容:
我已经找到了其他这样做的帖子,但我不理解代码,所以我试图从头开始构建它。这是我到目前为止得到的:
import numpy as np
import matplotlib as mpl
text = ["Electoral Process", "Political Pluralism", "Rule of Law",
"Freedom of expression", "Freedom of believe"]
no_labels =len(text)
angle_size = int(360/no_labels)
#define the number of angles in degrees and convert it to radians
theta = [i/180*np.pi for i in range(0, 360,angle_size) ]
#where to put the labels on the radial axis
radius = [1]*no_labels
#Find the mid point of each angle
mid_point =[i/180*np.pi for i in range(int(angle_size/2), 360, angle_size)]
fig,ax=plt.subplots(subplot_kw={'projection': 'polar'},figsize=(10,6), dpi=100)
# Arrange the grid into number of sales equal parts in degrees
lines = plt.thetagrids(range(0, 360, int(360/len(text))))
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_xticklabels([]) #remove the original xlabels
ax.set_yticklabels([]) #remove the original ylabels
#start with one label
start_text = theta[0]
text2=["ELECTORAL PROCESS"]
spacing=len(text[0])+4
end_text = theta[1]
x= np.linspace(start_text, end_text, spacing)
y= [1, 0.5]
for txt in text2:
print(txt)
for a,tx in zip(x,txt):
ax.text(a, 1.05 ,tx,rotation=-28, fontsize= 8, ha="center", va= "center"),
print(a,tx)
生成如下结果:
现在,我必须为每个标签添加它,旋转每个字母以遵循曲线(目前使用硬编码的Angular ),并根据代码的位置向上或向下书写,看起来我可能会使事情变得复杂。
有人用“简单的代码”做过这件事吗?或者可以解释下面的代码是如何工作的?
Similar problem but dont understand the code
2条答案
按热度按时间093gszye1#
这是一个非常困难的问题,这是我经过2个小时的编码后得到的(我不能做得更好)。
结果如下:
我希望这对你有帮助。
v09wglhw2#
感谢@aradhna,我设法修复了代码。(万分感谢)在这里!
结果如下: