gpt4 book ai didi

python - 如何在极坐标 matplotlib 图中旋转刻度标签?

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:35 33 4
gpt4 key购买 nike

我有很长的标识符,我想制作一个径向图,其中刻度线都处于不同的角度。例如,右侧 0 度处的第一个刻度应具有 0 度角。顶部的应该是 90 度。左边270度的应该是0度。我希望它看起来像径向树状图。使用 matplotlib 2.0.2python 3.6.2

在 matplotlib 中是否可以旋转单个刻度标签或单独添加文本标签?

注意:我已经更新了情节以回应下面的@ImportanceOfBeingErnest。

设置 ax.set_rticks([]) 会在添加散点和散点线时扭曲绘图。 label.get_position() 中的位置将标签显着偏移到图的右侧。

有没有办法使用角度和振幅坐标?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = {'0-b__|ce0Ji|aaaiIi9abGc_|ti5l-baa1tcciii|irGi': 0.28774963897009614, '0-b__|ce0Ji|aaaiIi9abGc_|ti6l-baa1tcciii|irGi': 0.18366735937444964, 'allb_e__|tla1a|ali|_auc7en_|e': -0.11720263463773731, 'b__0|lp|..ii80p.e7l_|an4obln.llll0ai|': -0.021168680215561269, 'b__Ass8._ii8.c4on|Ay|mbessoxiAxa': 0.17845443978725653, 'b__Bts4o_rrtiordae|Bei|obe7rattrniBno': 0.32077066676059313, 'b__|aaa|tteiatlim_|e1rblttaaeei|e': -0.27915536613715614, 'b__|as4.|ei2.l7ov_|e0tblaaoxi|xa': 0.43309499489274772, 'b__|as4.|ei2.l7ov_|e9tblaaoxi|xa': 0.47835581698425556, 'b__|cu|ppripcae_|co2tbopnccpei|': -0.20330386390053184, 'b__|eoea|cccimacnuuh_|ra0obarceenbi|ba': 0.062889648127927869, 'b__|oa|ggrigoip_|nr6ybmgvoohii|i': -0.045648268817583035, 'b__|p1|ooiioi4rs_|sr5eba0otsoi|ox': -0.52544820541720971, 'b__|paa|piatgn_|hy1cboippoli|la': 0.27260399422352155, 'b__|triu|mmriumay_|eb4ebcimrttnhi|hc': 0.62680074671550845, 'b__|tru|mmriumad_|eb2obcmittisi|': 0.34780388151174668, 'etob_m__|aol2l|ooeui|_lool7r': 0.4856468599203973, 'etpb_s__|apl2l|lleni|_loll8e': 0.24430277200521291, 'ib__rCalc_hhdiorchubai|CSt|absahodrsiCsaaca': -0.13484907188897891, 'nlab___|oa1i|ssni|_iesa9': 0.13636363636363635, 'nlnb_i__|dn1t|rrnfi|_tera8ig_|e': -0.056954668733049205, 'nrfb_h__|afl3r|ssnti|_resl3yn_': 0.56102285935683849, 'o5b__l|rcoa|eecialaeprh_|as1o5bie0trrnlii|irLa': 0.53377831002782572, 'oelb_a__Aelt3_rrovi__rro|a': 0.32230284245007218, 'oelb_a__Aelt4_rrovi__rro|a': 0.16580958754534889, 'porb_i__Ctrc6c_oopci__cloa|ny|C': 0.38260364199922509, 'porb_i__Ctrc7g_rrpci__glra|ay|C': 0.51829805219964076, 'ptab_a__|hac2b|uupci|_boui3ct_|': 0.50873516255151285, 'reab_a__|aa2a|rrrhi|_axrl4ra_|': -0.47742242259871087, 'sb__o|sSac|ccnibocsctlhd_|a0dbuacmssioai|anCca': 0.42733612764608503, 'teob___|oa1b|iiti|_bnil3': -0.32684653587404461, 'uoib_i__|ia2a|bbuli|_arbi2it': -0.13636363636363635}
Se_corr = pd.Series(data, name="correlation")


def plot_polar(r):
with plt.style.context("seaborn-whitegrid"):
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, polar=True)
ax.set_rmax(2)
# ax.set_rticks([])
ticks= np.linspace(0, 360, r.index.size + 1) [:-1]

ax.set_xticks(np.deg2rad(ticks))
ax.set_xticklabels(r.index, fontsize=15,)

angles = np.linspace(0,2*np.pi,len(ax.get_xticklabels()))
angles[np.cos(angles) < 0] = angles[np.cos(angles) < 0] + np.pi
angles = np.rad2deg(angles)

for i, theta in enumerate(angles):
ax.plot([theta,theta], [0,r[i]], color="black")
ax.scatter(x=theta,y=r[i], color="black")


labels = []
for label, theta in zip(ax.get_xticklabels(), angles):
x,y = label.get_position()
lab = ax.text(x, y, label.get_text(), transform=label.get_transform(),
ha=label.get_ha(), va=label.get_va())
lab.set_rotation(theta)
labels.append(lab)
ax.set_xticklabels([])

return fig, ax
fig,ax = plot_polar(Se_corr)

enter image description here

最佳答案

旋转极坐标图的刻度标签可能不像通常的笛卡尔图那样容易。对于笛卡尔图,可以简单地做类似的事情

for label in ax.get_xticklabels():
label.set_rotation(...)

这不适用于极坐标图,因为它们的旋转在绘制时重置为 0 度。

想到的一个选项是创建新的刻度标签作为附加文本对象,它复制刻度标签的属性但可以有一个持久的旋转。然后删除所有原始标记。

import numpy as np
import matplotlib.pyplot as plt

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

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rmax(2)
ax.set_rticks([])


plt.gcf().canvas.draw()
angles = np.linspace(0,2*np.pi,len(ax.get_xticklabels())+1)
angles[np.cos(angles) < 0] = angles[np.cos(angles) < 0] + np.pi
angles = np.rad2deg(angles)
labels = []
for label, angle in zip(ax.get_xticklabels(), angles):
x,y = label.get_position()
lab = ax.text(x,y, label.get_text(), transform=label.get_transform(),
ha=label.get_ha(), va=label.get_va())
lab.set_rotation(angle)
labels.append(lab)
ax.set_xticklabels([])

plt.show()

enter image description here

对于较长的标签,您可以使用标签的 y 坐标:

import numpy as np
import matplotlib.pyplot as plt

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

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rmax(2)
ax.set_rticks([])
ticks= np.linspace(0,360,9)[:-1]
ax.set_xticks(np.deg2rad(ticks))
ticklabels = ["".join(np.random.choice(list("ABCDE"),size=15)) for _ in range(len(ticks))]
ax.set_xticklabels(ticklabels, fontsize=10)

plt.gcf().canvas.draw()
angles = np.linspace(0,2*np.pi,len(ax.get_xticklabels())+1)
angles[np.cos(angles) < 0] = angles[np.cos(angles) < 0] + np.pi
angles = np.rad2deg(angles)
labels = []
for label, angle in zip(ax.get_xticklabels(), angles):
x,y = label.get_position()
lab = ax.text(x,y-.65, label.get_text(), transform=label.get_transform(),
ha=label.get_ha(), va=label.get_va())
lab.set_rotation(angle)
labels.append(lab)
ax.set_xticklabels([])

plt.subplots_adjust(top=0.68,bottom=0.32,left=0.05,right=0.95)
plt.show()

enter image description here


已编辑问题代码的更正版本:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = {'0-b__|ce0Ji|aaaiIi9abGc_|ti5l-baa1tcciii|irGi': 0.28774963897009614, '0-b__|ce0Ji|aaaiIi9abGc_|ti6l-baa1tcciii|irGi': 0.18366735937444964, 'allb_e__|tla1a|ali|_auc7en_|e': -0.11720263463773731, 'b__0|lp|..ii80p.e7l_|an4obln.llll0ai|': -0.021168680215561269, 'b__Ass8._ii8.c4on|Ay|mbessoxiAxa': 0.17845443978725653, 'b__Bts4o_rrtiordae|Bei|obe7rattrniBno': 0.32077066676059313, 'b__|aaa|tteiatlim_|e1rblttaaeei|e': -0.27915536613715614, 'b__|as4.|ei2.l7ov_|e0tblaaoxi|xa': 0.43309499489274772, 'b__|as4.|ei2.l7ov_|e9tblaaoxi|xa': 0.47835581698425556, 'b__|cu|ppripcae_|co2tbopnccpei|': -0.20330386390053184, 'b__|eoea|cccimacnuuh_|ra0obarceenbi|ba': 0.062889648127927869, 'b__|oa|ggrigoip_|nr6ybmgvoohii|i': -0.045648268817583035, 'b__|p1|ooiioi4rs_|sr5eba0otsoi|ox': -0.52544820541720971, 'b__|paa|piatgn_|hy1cboippoli|la': 0.27260399422352155, 'b__|triu|mmriumay_|eb4ebcimrttnhi|hc': 0.62680074671550845, 'b__|tru|mmriumad_|eb2obcmittisi|': 0.34780388151174668, 'etob_m__|aol2l|ooeui|_lool7r': 0.4856468599203973, 'etpb_s__|apl2l|lleni|_loll8e': 0.24430277200521291, 'ib__rCalc_hhdiorchubai|CSt|absahodrsiCsaaca': -0.13484907188897891, 'nlab___|oa1i|ssni|_iesa9': 0.13636363636363635, 'nlnb_i__|dn1t|rrnfi|_tera8ig_|e': -0.056954668733049205, 'nrfb_h__|afl3r|ssnti|_resl3yn_': 0.56102285935683849, 'o5b__l|rcoa|eecialaeprh_|as1o5bie0trrnlii|irLa': 0.53377831002782572, 'oelb_a__Aelt3_rrovi__rro|a': 0.32230284245007218, 'oelb_a__Aelt4_rrovi__rro|a': 0.16580958754534889, 'porb_i__Ctrc6c_oopci__cloa|ny|C': 0.38260364199922509, 'porb_i__Ctrc7g_rrpci__glra|ay|C': 0.51829805219964076, 'ptab_a__|hac2b|uupci|_boui3ct_|': 0.50873516255151285, 'reab_a__|aa2a|rrrhi|_axrl4ra_|': -0.47742242259871087, 'sb__o|sSac|ccnibocsctlhd_|a0dbuacmssioai|anCca': 0.42733612764608503, 'teob___|oa1b|iiti|_bnil3': -0.32684653587404461, 'uoib_i__|ia2a|bbuli|_arbi2it': -0.13636363636363635}
Se_corr = pd.Series(data, name="correlation")


def plot_polar(r):
with plt.style.context("seaborn-whitegrid"):
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, polar=True)
ax.set_rmax(2)
#ax.set_rticks([])
ticks= np.linspace(0, 360, r.index.size + 1)[:-1]

ax.set_xticks(np.deg2rad(ticks))
ax.set_xticklabels(r.index, fontsize=15,)

angles = np.linspace(0,2*np.pi,len(ax.get_xticklabels())+1)
angles[np.cos(angles) < 0] = angles[np.cos(angles) < 0] + np.pi
angles = np.rad2deg(angles)

for i, theta in enumerate(angles[:-1]):
ax.plot([theta,theta], [0,r[i]], color="black")
ax.scatter(x=theta,y=r[i], color="black")

fig.canvas.draw()
labels = []
for label, theta in zip(ax.get_xticklabels(), angles):
x,y = label.get_position()
lab = ax.text(x, y, label.get_text(), transform=label.get_transform(),
ha=label.get_ha(), va=label.get_va())
lab.set_rotation(theta)
labels.append(lab)
ax.set_xticklabels([])

return fig, ax
fig,ax = plot_polar(Se_corr)
plt.show()
; Image produced by that code

关于python - 如何在极坐标 matplotlib 图中旋转刻度标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719340/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com