【问题标题】:Refine X- Axis so that the distances are better recognizable优化 X 轴,以便更好地识别距离
【发布时间】:2021-12-29 16:00:37
【问题描述】:

我有问题。我有一个数据框df。我想在 seaborn 的帮助下绘制散点图。但我想改变 x 轴。我希望 x 轴从 4.0 到 5.0 更精细。距离应该更小,例如4.1、4.2 甚至更好。 如何设置让 x 轴显示更精细,以便更好地查看 4.0 的值?

我看了seaborn, pylab - changing xticks from float to intHow to change the X axis range in seaborn in python?

d = {'review_scores_accuracy': [1.1, 2.0, 4.5, 5.0, 4.9, 4.8, 4.7], 
     'review_scores_rating': [1.1, 2.0, 4.6, 3.9, 4.2, 4.5, 4.2]}
df = pd.DataFrame(data=d)


fig, ax = plt.subplots(figsize=(20,10))
sns.scatterplot(data=df, x="review_scores_rating", y="review_scores_accuracy", ax = ax ) 
# ax.set_xlim(1,5)
# ax.set_xticks(1,2,3,4,4.1,4.2)
plt.show()

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    使用matplotlib.ticker,按照here

    import pandas as pd
    import seaborn as sns
    import matplotlib.ticker as ticker
    
    d = {'review_scores_accuracy': [1.1, 2.0, 4.5, 5.0, 4.9, 4.8, 4.7], 
     'review_scores_rating': [1.1, 2.0, 4.6, 3.9, 4.2, 4.5, 4.2]}
    df = pd.DataFrame(data=d)
    
    fig, ax = plt.subplots(figsize=(20,10))
    sns.scatterplot(data=df, x="review_scores_rating",y="review_scores_accuracy", ax=ax) 
    ax.xaxis.set_major_locator(ticker.MultipleLocator(0.1))
    plt.show()
    

    如果您只想绘制 4 到 5 之间的数据,请缩小数据范围。

    【讨论】:

    • 当 4.0 时,是否还可以选择让它仅以 0.1 步开始,但仍应从 1.0 开始
    • 您必须指定使用类似ax.set_xticks([1, 2, 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5]) 而不是ticker.MultipleLocator
    • 非常感谢您的帮助! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多