Python中绘制条形图

Python中绘制条形图

本文介绍了使用旋转的xlabel在Seaborn Python中绘制条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Seaborn中绘制没有任何统计信息的简单条形图?数据集就是名称和值.

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]})

我想将其绘制为条形图,其中xlabels从 name 列中拉出,y轴值从 vals 中拉出,并且x轴标签旋转45度.如何做呢?像这样使用 sns.barplot :

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.

推荐答案

您的意思是那样的( set_xticklabels 方法):

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)


或者 plt.xticks 方法可能会有所帮助:

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()

这篇关于使用旋转的xlabel在Seaborn Python中绘制条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:23