我遇到了一个使用Seaborn/matplotlib似乎很简单的问题,因为我的x轴值似乎与条上的标签没有正确关联。作为参考,我有一个pandas.DataFrame对象,并删除了前20行以显示更详细的数据,剩下的内容如下:
hypothesis1_df:
     revol_util  deviation
20           20 -37.978539
21           21 -27.313996
22           22 -23.790328
23           23 -19.729957
24           24 -16.115686
..          ...        ...
96           96  67.275585
97           97  91.489382
98           98  60.967792
99           99  48.385094
100         100  77.852812现在的问题是,当我使用以下代码将其绘制为图形时:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
ax = sns.barplot(x='revol_util', y='deviation', data=hypothesis1_df)
ax.set(xlabel="Revolving Credit Utilization (%)",
           ylabel="Deviation from Mean (%)",
           title="Credit Utilization and Likelihood of Late Payments\n(20 - 100%)")
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%d'))  # Format axis ticks as int
ax.xaxis.set_major_locator(ticker.MultipleLocator(base=10)) # Set tick label frequency = base
plt.show()我明白了:

注意x轴的值,以及它们不是从20开始的。有什么方法可以偏移滚动条吗?我尝试过ax.set_xlim(xmin=20, xmax=100),但它只切掉了图形的底部20,并将其向右扩展到空白处。如果我删除所有的轴格式,它是正确的标签,但太忙了,因为每个标签都列出了。谢谢你的帮助。
发布于 2017-09-26 18:18:55
问题是,在海上条形图中,条形图的位置实际上是0,1,...,N-1;它们的标签使用FixedLocator设置为对应于数据的数字。
因此,可以选择:使用多个定位器并手动设置刻度标签
    ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
    ax.set_xticklabels(df.index.tolist()[::10]) # take every tenth label from listhttps://stackoverflow.com/questions/46414812
复制相似问题