Home > OS >  How to set ticklabel rotation and add bar annotations with subplots
How to set ticklabel rotation and add bar annotations with subplots

Time:11-09

enter image description here

I would like to draw the following bar plot with annotation and I want to keep the x-label 45 degree so that it is easily readable. I am not sure why my code is not working. I have added the sample data and desired bar plots as a attachment. I appreciate your suggestions! Thanks!

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

#sns.set(rc={"figure.dpi":300, 'savefig.dpi':300})
sns.set_context('notebook')
sns.set_style("ticks")
#sns.set_style('white')
sns.set_context("paper", font_scale = 2)
colors = ['b', 'g', 'r', 'c', 'm']
#sns.set(style="whitegrid")
#sns.set_palette(sns.color_palette(colors))

#fig, (ax1,ax2) = plt.subplots(1, 2, figsize=(16, 8))
#fig.subplots_adjust(wspace=0.3)

plots1 = sns.barplot(x="Model", y="G-mean", data=df_Aussel2014_5features, ax=ax1,palette='Spectral')
# Iterrating over the bars one-by-one
for bar in plots1.patches:

    # Using Matplotlib's annotate function and
    # passing the coordinates where the annotation shall be done
    plots1.annotate(format(bar.get_height(), '.2f'),
                (bar.get_x()   bar.get_width() / 2,
                    bar.get_height()), ha='center', va='center',
                size=10, xytext=(0, 5),
                textcoords='offset points')
plt.show()
# Save figure
#plt.savefig('Aussel2014_5features.png', dpi=300, transparent=False, bbox_inches='tight')

I got the following image.

enter image description here

CodePudding user response:

import matplotlib.pyplot as plt. plt.xticks(rotation=‌​45)

Example :

import matplotlib.pyplot as plt

plt.xticks(rotation=‌​45)

CodePudding user response:

  • You are using the object oriented interface (e.g. axes) so don't mix plt. and axes. methods
  • enter image description here

  • Related