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()
So basically I just want to move my yticks up or down a notch. The above code produces the following graph:
You can probably see my problem there. My y-axis values are getting lost behind the bars. I can widen the space between the bars with the subplots_adjust at the bottom, but that's less pretty. Is there any way I can move the yticks up (or down)?
Also it'd be really nice to get ride of the 0 and 0.0 on the xticks.
Any helps appreciated. Cheers folks.
CodePudding user response:
Since it is not possible to display the y-axis up across the graphs, I suggest merging the two graphs with zero spacing. How about annotating the country names and numbers in that state to ensure a good look? To deal with the starting point of the x-axis tick marks on the right, I got the current tick value and replaced the first tick value with zero as a string.
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()