fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(12,12))
axes[0].barh(bar_chart.index, bar_chart['% Vaccinated'], align='edge', height=0.3,
color='red', zorder=1)
axes[0].set(title='% Fully Vaccinated)')
axes[1].barh(bar_chart.index, bar_chart['Deaths_Per_Confirmed_Case'], align='edge',height=0.3,
color='pink', zorder=1)
axes[1].set(title='% Deaths Per New Confirmed Cases')
axes[0].invert_xaxis()
axes[0].set(yticks=bar_chart.index)
axes[0].yaxis.tick_right()
for ax in axes.flat:
ax.margins(0.09)
ax.grid(True)
fig.tight_layout()
fig.subplots_adjust(wspace=.06)
plt.show()
所以基本上我只想將我的 yticks 向上或向下移動一個檔次。上面的代碼產生如下圖:

你可能會在那里看到我的問題。我的 y 軸值在酒吧后面迷路了。我可以用底部的 subplots_adjust 擴大條形之間的空間,但這不太漂亮。有什么辦法可以向上(或向下)移動 yticks?
此外,在 xticks 上使用 0 和 0.0 也非常好。
任何幫助表示贊賞。干杯人。
uj5u.com熱心網友回復:
由于無法在圖形中向上顯示 y 軸,我建議將兩個圖形以零間距合并。如何注釋該州的國家/地區名稱和數字以確保美觀?為了處理右側x軸刻度線的起點,我得到了當前刻度值,并將第一個刻度值替換為零作為字串。
fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(12,12))
axes[0].barh(bar_chart.index, bar_chart['% Vaccinated'], align='center', height=0.3, color='red', zorder=1)
axes[0].set(title='% Fully Vaccinated)')
axes[1].barh(bar_chart.index, bar_chart['Deaths_Per_Confirmed_Case'], align='center',height=0.3, color='pink', zorder=1)
axes[1].set(title='% Deaths Per New Confirmed Cases')
axes[0].invert_xaxis()
axes[0].set(yticks=bar_chart.index)
axes[0].yaxis.tick_right()
new_labels = bar_chart.index.tolist()
for ax in axes.flat:
ax.margins(0.09)
ax.grid(True)
new_labels = bar_chart.index.tolist()
for rect,rect2,lbl in zip(axes[0].patches, axes[1].patches,new_labels):
width = rect.get_width()
width2 = rect2.get_width()
ypos = rect.get_y()
axes[0].annotate(lbl, (width, ypos 0.1), xytext=(3, 0), textcoords='offset points', size=20, color='white')
axes[0].annotate(str(width), (12, ypos 0.1), xytext=(3, 0), textcoords='offset points', size=20, color='white')
axes[1].annotate(str(width2), (0.01, ypos 0.1), xytext=(3, 0), textcoords='offset points', size=20, color='red')
ax1_labels = [str(round(l,1)) for l in axes[1].get_xticks()]
ax1_labels[0] = '0'
axes[1].set_xticklabels(ax1_labels)
fig.tight_layout()
fig.subplots_adjust(wspace=0.0)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367146.html
標籤:Python matplotlib
