【问题标题】:Seaborn change x axis valuesSeaborn 更改 x 轴值
【发布时间】:2018-03-20 00:29:33
【问题描述】:

我正在尝试做一个虚线图(就像我在下面创建的那样),但将 x 轴标签更改为类别的名称(a 和 b)而不是数字 0-1。我喜欢能够一目了然地看到分布的想法(橙色与蓝色点)。

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


d = {'vote': [100, 50,1,23,55,67,89,44], 
     'ballot': ['a','b','a','a','b','a','a','b'],
     'whichballot':[1,2,1,1,2,1,1,2]}
dfwl=pd.DataFrame(d)

dfwl['whichballot'] = dfwl['whichballot'].astype('category')
dfwl['ballot'] = dfwl['ballot'].astype('category').cat.codes
dfwl=pd.DataFrame(dfwl.reset_index())

fig=sns.pairplot(x_vars=pd.Categorical(['ballot']), y_vars=['vote'], data=dfwl, hue="whichballot", size=5)

plt.show(fig)

【问题讨论】:

    标签: python-3.x plot seaborn


    【解决方案1】:

    正常的散点图似乎足以生成所需的图。

    import matplotlib.pyplot as plt
    import pandas as pd
    
    d = {'vote': [100, 50,1,23,55,67,89,44], 
         'ballot': ['a','b','a','a','b','a','a','b'],
         'whichballot':[1,2,1,1,2,1,1,2]}
    df=pd.DataFrame(d)
    
    plt.scatter(df.ballot, df.vote, c=df.whichballot)
    plt.margins(x=0.8)
    plt.show()
    

    或者,如果使用 seaborn,stripplot 会有意义。

    import matplotlib.pyplot as plt
    import pandas as pd
    import seaborn as sns
    
    d = {'vote': [100, 50,1,23,55,67,89,44], 
         'ballot': ['a','b','a','a','b','a','a','b'],
         'whichballot':[1,2,1,1,2,1,1,2]}
    df=pd.DataFrame(d)
    
    ax = sns.stripplot(x='ballot', y='vote', data=df, jitter=False)
    plt.show()
    

    但是,为了查看分布,人们可能更愿意使用箱线图或小提琴图。

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      相关资源
      最近更新 更多