2

Python Matplotlib で疑似カラー イメージ プロットを作成しようとしていますが、レイアウトに小さな問題があります。軸の目盛りラベルは非常に小さい数値 (1e-7 程度) であるため、Matplotlib は全体に指数を付けます。軸。これでもいいのですが、x軸のラベルとタイトルが重なっています!

タイトルがどのように重なっているかを確認してください

タイトルを上向きに、xlabel を下向きにハッキングする以外に、これを修正するより良い方法はありますか?最大限のコードの再利用のため、Matplotlib にテキストの配置を手動で変更せずにこれを修正する方法があれば、それが最善です!

このプロットを生成する方法は次のとおりです。

fig = Figure(figsize=(5.5, 4.25))
ax = fig.add_subplot(111)
ax.set_title('The title', size=12)
ax.set_xlabel('The xlabel', size=10)
ax.set_ylabel('The Ylabel', size=10)
ax.ticklabel_format(scilimits=(-3,3))

pcm = ax.pcolor(X, Y, Z, cmap=cm.jet) #X,Y is a meshgrid and Z is the function evaluated on it
ax.get_figure().colorbar(pcm, ax=ax, use_gridspec=True)
ax.set_axes([0, 1e-3, -5e-7, 5e-7])

#Some code for the hatching at the top and bottom of the plot

for ticklabel in ax.get_xticklabels():
    ticklabel.set_size(8)
for ticklabel in ax.get_yticklabels():
    ticklabel.set_size(8)
ax.get_xaxis().get_offset_text().set_size(8)
ax.get_yaxis().get_offset_text().set_size(8)
fig.subplots_adjust()
c = FigureCanvas(fig)
c.print_figure('filename.png', dpi=300)
4

1 に答える 1

4

最も簡単な方法は、X と Y に 1e3 と 1e7 を掛けて、指数の必要性をなくすことです。

pcm = ax.pcolor(X*1e3, Y*1e7, Z, cmap=cm.jet)

次に、ラベルを次のように変更します。

ax.set_xlabel('Longitudinal distance across waveguide ($10^{-3}$ m)', size=10)
ax.set_ylabel('Transverse distance across waveguide ($10^{-7}$ m)', size=10)

目盛りラベルを直接変更するか、matplotlib.ticker.Formatter を使用することもできますが、前者は少し面倒ですが、後者はやり過ぎです。

于 2012-10-06T05:10:41.780 に答える