How to format seaborn/matplotlib axis tick labels from number to thousands or Millions? (125,436 to 125.4K)

前端 未结 4 1527
深忆病人
深忆病人 2020-11-29 08:21
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
import pandas as pd
sns.set(style="darkgrid")    
fig, ax = plt.su         


        
4条回答
  •  臣服心动
    2020-11-29 08:50

    The canonical way of formatting the tick labels in the standard units is to use an EngFormatter. There is also an example in the matplotlib docs.

    Here it might look as follows.

    import numpy as np; np.random.seed(42)
    import matplotlib.pyplot as plt
    import matplotlib.ticker as ticker
    import seaborn as sns
    import pandas as pd
    
    df = pd.DataFrame({"xaxs" : np.random.randint(50000,250000, size=20),
                       "yaxs" : np.random.randint(7,15, size=20),
                       "col"  : np.random.choice(list("ABC"), size=20)})
    
    fig, ax = plt.subplots(figsize=(8, 5))    
    palette = sns.color_palette("bright", 6)
    sns.scatterplot(ax=ax, x="xaxs", y="yaxs", hue="col", data=df, 
                    marker='o', s=100, palette="magma")
    ax.legend(bbox_to_anchor=(1, 1), ncol=1)
    ax.set(xlim = (50000,250000))
    
    ax.xaxis.set_major_formatter(ticker.EngFormatter())
    
    plt.show()
    

提交回复
热议问题