mvd mvd - 2 months ago 23
Python Question

plotting bar plot in seaborn python with rotated xlabels

how can I plot a simple bar graph in seaborn without any statistics? the data set is simply names and values

import pandas
df = pandas.DataFrame({"name": ["Bob Johnson", "Mary Cramer", "Joe Ellis"], "vals": [1,2,3]})


i would like to plot this as a bar graph with xlabels pulled from
name
column and y-axis values from
vals
and have x-axis labels rotated 45 degrees. how can it be done? using
sns.barplot
like:

sns.barplot(x="name", y="vals", data=df)


will compute statistics that are not relevant here.

Answer

You mean like that (set_xticklabels approach):

import pandas

df = pandas.DataFrame({"name": ["Bob Johnson", "Mary Cramer", "Joe Ellis"], "vals": [1,2,3]})
g = sns.barplot(x='name', y='vals', data=df)
g.set_xticklabels(g.get_xticklabels(), rotation=45)

Or probably plt.xticks approach can help:

import pandas
import matplotlib.pylab as plt

df = pandas.DataFrame({"name": ["Bob Johnson", "Mary Cramer", "Joe Ellis"], "vals": [1,2,3]})

bar_plot = sns.barplot(x='name', y='vals', data=df)
plt.xticks(rotation=45)
plt.show()