[英]x-labels rendered twice on matplotlib plot
我有一個在 x 軸上使用 9 個標簽的圖。 但是,由於我已將繪圖分成兩個軸,因此由於某種原因,它似乎需要 18 個標簽(因此添加了空字符串列表)。
這似乎使 x-labels 被渲染兩次,使它們看起來有一個粗體字。 附上問題的圖片。
這是我正在使用的當前代碼。 我為代碼的質量道歉。 我是 matplotlib 的新手。
benchmark_data = all_benchmark_data[loader.pingpongKey]
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(9,7), dpi=80)
fig.subplots_adjust(hspace=0.05)
ax1.boxplot(benchmark_data.values())
ax2.boxplot(benchmark_data.values())
ax1.set_ylim(380, 650)
ax2.set_ylim(110, 180)
# hide the spines between ax and ax2
ax1.spines.bottom.set_visible(False)
ax2.spines.top.set_visible(False)
ax1.xaxis.tick_top()
ax1.tick_params(labeltop=False) # don't put tick labels at the top
ax2.xaxis.tick_bottom()
ax1.tick_params(axis='both', labelsize=10)
ax2.tick_params(axis='both', labelsize=10)
xlabels = ['', '', '', '', '', '', '', '', ''] + (list(benchmark_data.keys()))
ax1.set_xticklabels(xlabels)
ax1.set_ylabel('Time (ms)', fontsize=10)
ax1.yaxis.set_label_coords(-0.06,0)
#ax2.set_ylabel('Time (ms)', fontsize=10)
plt.xticks(fontsize=10, rotation=45)
ax1.yaxis.set_major_locator(ticker.MaxNLocator(nbins=5, min_n_ticks=5))
ax2.yaxis.set_major_locator(ticker.MaxNLocator(nbins=5, min_n_ticks=5))
d = .5 # proportion of vertical to horizontal extent of the slanted line
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
linestyle="none", color='k', mec='k', mew=1, clip_on=False)
ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)
plt.tight_layout()
plt.savefig('plots/boxplots/' + loader.pingpongKey + '-boxplot.png',
bbox_inches='tight')
因為您使用的是sharex=True
,所以第二次繪制箱線圖時,您將創建另外 9 個添加到軸的刻度(這在ax1
和ax2
之間是共同的)。 您的解決方案是關閉sharex
(無論如何,軸都會對齊)並在 ax2 上設置ax2
:
# No sharex.
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(9,7), dpi=80)
# ...
# Set ticks for ax2 instead of ax1 and only the 9 labels are needed.
ax2.set_xticklabels(list(benchmark_data.keys()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.