2017-01-12 10 views
1

私が望む視覚化をしていません。私はまだ画像を置くことができないので、リンクは下にあります。私はほとんど私が望むものを持っています。問題は、ラベルが正しく配置されていないことです。極座標グラフの内側の端にラベルを揃える方法

逆極性バー・デモ

enter image description here

私は、彼らがしているようにラベルを回転させることが持っていると思いますが、ラベル右エッジは、ちょうど円の外縁内側に整列しています。明確にするために

EDIT:私はこの例で使用
ラベルは、すべての「テスト」です。実際のデータでは、これらのラベルの長さは異なります。私はラベルの終わりを移動して、サークルの外側のエッジの隣に常に最後の文字が表示されるようにしたい。この場合、すべての 'g'は外側のエッジの隣にあります。

import matplotlib.pyplot as mpl 
import numpy as np 
import random 

bgcolor = '#222222' 
barcolor = '#6699cc' 
bottom = 15 

N = 32 
Values = np.random.random(N)*10 
MetricLabels = ['testing' for _ in range(1, N+1)] 

# Select the radii, thetas, and widths. 
Radii = -5*np.ones(N)-Values 
Theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) 
width = 2*np.pi/N 

# Make a list of shifted thetas to place the labels at. 
ThetaShifted = np.copy(Theta) 
for i in range(N-1): 
    ThetaShifted[i] = (Theta[i] + Theta[i+1])/2.0 
ThetaShifted[-1] = (Theta[-1] + 2.0*np.pi)/2.0 

# Make the figure 
fig = mpl.figure() 
ax = fig.add_subplot(111, projection='polar') 
bars = ax.bar(Theta, Radii, width=width, bottom=bottom) 

# Set the outer ring to be invisible. 
ax.spines["polar"].set_visible(False) 

# Set the grid line locations but set the labels to be invisible. 
ax.grid(False) 
ax.set_thetagrids([], visible=False) 
ax.set_rgrids([3], visible=False) 

# Apply colors to bars based on the settings above. 
for v, bar in zip(Values, bars): 
     bar.set_facecolor(barcolor) 
     bar.set_edgecolor(bar.get_facecolor()) 

# Show the metric and value labels 
for counter in range(N): 
    ax.text(ThetaShifted[counter], bottom-3, MetricLabels[counter], 
      horizontalalignment='center', verticalalignment='baseline', 
      rotation=(counter+.5)*360/N, color=bgcolor) 
    ax.text(ThetaShifted[counter], bottom+0.75, np.round(Values[counter],2), 
      horizontalalignment='center', verticalalignment='center', 
      color=bars[counter].get_facecolor()) 

# Set the background color to be a dark grey, 
ax.set_axis_bgcolor(bgcolor) 
fig.set_facecolor(bgcolor) 

# Show the figure. 
mpl.show() 
+0

あなた自身、問題を解決した場合、あなたはあなた自身の質問への答えとして、それを投稿することができます。答えを探して "質問"を読むのではなく、未来の人々を発見する方がはるかに簡単です。); –

答えて

1

私は実際に私を解きます問題。以下の画像とコードを参照してください。これを解決する主な点は、モノスペースフォントファミリを使用して、rjustを使用してラベルの文字列を固定長にし、最初から右揃えにすることでした。その後、それぞれのラベルの正しい半径位置を選択するだけで、同じ文字数のときははるかに簡単になります。

Initial demo solved

import matplotlib.pyplot as mpl 
import numpy as np 
import random 

bgcolor = '#222222' 
barcolor = '#6699cc' 
bottom = 15 

N = 32 
Values = np.random.random(N)*10 
MetricLabels = [('A'*(4+int(8*random.random()))).rjust(10) for _ in range(1, N+1)] 

# Select the radii, thetas, and widths. 
Radii = -5*np.ones(N)-Values 
Theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) 
width = 2*np.pi/N 

# Make a list of shifted thetas to place the labels at. 
ThetaShifted = np.copy(Theta) 
for i in range(N-1): 
    ThetaShifted[i] = (Theta[i] + Theta[i+1])/2.0 
ThetaShifted[-1] = (Theta[-1] + 2.0*np.pi)/2.0 

# Make the figure 
fig = mpl.figure() 
ax = fig.add_subplot(111, projection='polar') 
bars = ax.bar(Theta, Radii, width=width, bottom=bottom) 

# Set the outer ring to be invisible. 
ax.spines["polar"].set_visible(False) 

# Set the grid line locations but set the labels to be invisible. 
ax.grid(False) 
ax.set_thetagrids([], visible=False) 
ax.set_rgrids([3], visible=False) 

# Apply colors to bars based on the settings above. 
for v, bar in zip(Values, bars): 
    bar.set_facecolor(barcolor) 
    bar.set_edgecolor(bar.get_facecolor()) 

# Show the metric and value labels 
for counter in range(N): 
    ax.text(ThetaShifted[counter], bottom-.075*(10+len(MetricLabels[counter])), MetricLabels[counter]+' '*5, 
      horizontalalignment='center', verticalalignment='center', 
      rotation=(counter+.5)*360/N, color=bgcolor, 
      family='monospace') 
    ax.text(ThetaShifted[counter], bottom+1, np.round(Values[counter],2), 
      horizontalalignment='center', verticalalignment='center', 
      rotation=(counter+.5)*360/N, color=bars[counter].get_facecolor(), 
      family='monospace') 

# Set the background color to be a dark grey, 
ax.set_axis_bgcolor(bgcolor) 
fig.set_facecolor(bgcolor) 

# Show the figure. 
mpl.show() 
1

私はあなたがcounterサイクルの2回目の呼び出しにrotationプロパティを追加し、ここのようにテキストを配置する必要があり欲しいものを理解訂正した場合:

... 
# Show the metric and value labels 
for counter in range(N): 
    ax.text(ThetaShifted[counter], bottom-3, MetricLabels[counter], 
      horizontalalignment='center', verticalalignment='baseline', 
      rotation=(counter+.5)*360/N, color=bgcolor) 
    ax.text(ThetaShifted[counter], bottom+2.5, np.round(Values[counter],2), 
      horizontalalignment='center', verticalalignment='center', 
      rotation=(counter+.5)*360/N, 
      color=bars[counter].get_facecolor()) 
... 

enter image description here

+0

あまりにも数字を回転させていただきありがとうございます。しかし、私が望んでいたのは、すべての 'g'を外側にすることでした。 –

関連する問題