pandas 更改 seaborn boxplot 中的 X 轴标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37109021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:12:14  来源:igfitidea点击:

Changing X axis labels in seaborn boxplot

python-3.xpandasboxplotseaborn

提问by Tars

I have a pandas dataframe with multiple columns I am trying to plot the column "Score" (on x axis) with another column called "interest rate". I am using the following commands:

我有一个包含多列的 Pandas 数据框,我试图用另一列名为“利率”的列绘制“分数”列(在 x 轴上)。我正在使用以下命令:

box_plot=sns.boxplot(x=list(Dataframe['Score']),y=list(Dataframe['Interest.Rate']),data=Dataframe)
box_plot.set(xlabel='FICO Score',ylabel='Interest Rate')

This works fine and it create a boxplot with appropriate axes. Seems like I have to pass the variables as list in boxplot function. Maybe there is better way to do it.

这很好用,它创建了一个带有适当轴的箱线图。似乎我必须在 boxplot 函数中将变量作为列表传递。也许有更好的方法来做到这一点。

The problem is x axis labels are too crowded and are not readable so I don't want them all too print, only some of them for better readability.

问题是 x 轴标签太拥挤且不可读,所以我不希望它们全部打印,只有其中一些以获得更好的可读性。

I have tried multiple options with xticks and xticklabel functions but none of them seem to work.

我已经尝试了 xticks 和 xticklabel 函数的多个选项,但它们似乎都不起作用。

回答by Serge

you could do simply this:

你可以简单地这样做:

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

data = pd.read_csv('your_data.csv', index_col=0)

sns.boxplot(
    x='Score', 
    y='Interest.Rate', 
    data=data
).set(
    xlabel='FICO Score', 
    ylabel='Interest Rate'
)
plt.show()

回答by MaxU

try it this way:

试试这样:

box_plot=sns.boxplot(x='Score', y='Interest.Rate',data=Dataframe)

instead of converting pandas series to lists

而不是将Pandas系列转换为列表

if you need help with the X axis please post sample data setwhich helps to reproduce your problem.

如果您需要有关 X 轴的帮助,请发布有助于重现您的问题的示例数据集