私は、symlog軸を使って極地のヒストグラムを作成する際に問題が発生しています。しかし、これは負のr値がある場合にのみ壊れます。これを修正するにはどうすればよいですか?matplotlibでは、極座標投影とsymlog r軸を使用して2Dヒストグラムを作成するにはどうすればよいですか?
5つのプロットを表示する最小限の例が含まれています。プロット4は機能しないものです(真ん中の行)プロット5は正の数で動作することを示します。ここで
は出力です:
ここでコードがあります:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.scale import SymmetricalLogTransform
MIN_BINS = 500
MAX_BINS = 1000
# utility function for making bins of correct size
def get_bins(min_val, max_val, scaling):
w = 1 + max(MIN_BINS, min(MAX_BINS, abs(min_val - max_val) + 1))
if scaling == 'log':
return np.logspace(np.log10(min_val), np.log10(max_val), w)
elif scaling == 'symlog':
tr = SymmetricalLogTransform(base=10, linthresh=1, linscale=1)
ss = tr.transform([min_val, max_val])
ls = tr.inverted().transform(np.linspace(*ss, num=w))
return ls
else:# linear
return np.linspace(min_val, max_val, w)
TESTS = [1,2,3,4,5]
plt.figure(figsize=(12,18))
plot_pos = 320
for TEST in TESTS:
plot_pos += 1
if TEST == 1:
NEG = True
YSCALE = 'linear'
PROJECTION = None
if TEST == 2:
NEG = True
YSCALE = 'symlog'
PROJECTION = None
if TEST == 3:
NEG = True
YSCALE = 'linear'
PROJECTION = 'polar'
# This is what I want to work
if TEST == 4:
NEG = True
YSCALE = 'symlog'
PROJECTION = 'polar'
# However, remove the negative numbers and it seems to work
if TEST == 5:
NEG = False
YSCALE = 'symlog'
PROJECTION = 'polar'
sample_size = 100000
xs = np.pi * np.linspace(0.0, 2.0, sample_size)
ys = 20 * np.random.random(sample_size)
if NEG:
ys -= 10
ax = plt.subplot(plot_pos, projection=PROJECTION)
ax.set_title('Neg/Scale/Proj.:%s,%s,%s' % (NEG, YSCALE, PROJECTION))
min_y = np.min(ys)
max_y = np.max(ys)
min_x = np.min(xs)
max_x = np.max(xs)
x_bins = get_bins(min_x, max_x, 'linear')
y_bins = get_bins(min_y, max_y, YSCALE)
hist, xedges, yedges = np.histogram2d(
xs,
ys,
bins=[x_bins, y_bins]
)
X, Y = np.meshgrid(xedges, yedges)
ax.pcolormesh(
X, Y,
hist.T,
cmap=cm.gray_r,
)
if PROJECTION == 'polar':
ax.set_rscale(YSCALE)
ax.set_rmax(max_y)
ax.set_rmin(min_y)
else:
ax.set_ylim([min_y, max_y])
ax.set_yscale(YSCALE)
ax.grid(True)
plt.savefig('polar_example.png')
に放射状のケースを変更するとここにあなたのテストです。 com/questions/3305865/what-is-the-difference-between-log-and-symlog)を使用してください。彼らは非常に便利です。 – Annan
ええ、申し訳ありませんが、私はコメントを残した後に気づいた:)午前2時以降スマートなおしゃれではありません...私はちょっとだけ私のコメントを削除しなかった;)申し訳ありません。 –
@AndrasDeak問題はありません:)それはよくあることではありません。 – Annan