【问题标题】:seaborn, pylab - changing xticks from float to intseaborn, pylab - 将 xticks 从 float 更改为 int
【发布时间】:2015-07-31 08:54:48
【问题描述】:

我正在用 seaborn 作为 sns 和 pylab 作为 plt 绘制图表:

plt.figure(figsize=(10,10),)
sns.barplot(y = 'whatever_y', x = 'whatever_x' , data=mydata)
plt.xticks(fontsize=14, fontweight='bold')

xticks 应该是 0、1、2、3,但它们的绘制如下:0.0、1.0、2.0、3.0

有谁知道我必须添加什么才能将它们作为整数? - (数据是熊猫数据框)谢谢

【问题讨论】:

  • 什么是mydata?它是一个 numpy 数组还是其他什么?
  • 您可以使用df['x_col_name'] = df['x_col_name'].astype(int)更改列的dtype
  • 谢谢它的工作 - 所以这个问题仍然没有答案,或者有没有办法在 seaborn/pylab 中操纵 xticks 从 float 到 int?
  • 是的,你可以操纵它们,我认为你需要在 ticker 上使用格式化程序,但我不是 matplotlib 专家
  • 好吧,无论如何,谢谢。我会把问题留在那里 - 也许有人知道在 matplotlib 中这样做的一个方便的技巧

标签: python matplotlib seaborn


【解决方案1】:

很简单,使用 MaxNLocator 如下:

import seaborn as sns
from seaborn import displot 
import matplotlib.pyplot as plt


import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

y=[1, 0, 0, 1, 1, 1]
face_grid = sns.displot(y, color="aquamarine")
fig = face_grid.figure
ax = fig.gca()
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

【讨论】:

    【解决方案2】:

    如果您使用seaborn(也适用于 jupyter notebook),您也可以这样做。

    from  matplotlib.ticker import FuncFormatter
    ax = sns.barplot(x='x', y='y',hue='', data=data_set_pd)
    ax.xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))
    plt.show()
    

    【讨论】:

      【解决方案3】:

      该指南显示了如何操作 facetgrid: https://seaborn.pydata.org/tutorial/axis_grids.html

      with sns.axes_style("white"):
           g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True, height=2.5)
      g.map(sns.scatterplot, "total_bill", "tip", color="#334488")
      g.set_axis_labels("Total bill (US Dollars)", "Tip")
      g.set(xticks=[10, 30, 50], yticks=[2, 6, 10])
      g.fig.subplots_adjust(wspace=.02, hspace=.02)
      

      “g.set”行采用 xticks 参数。 例如,我有一个 pandas 数据框列,其中整数 1 到 18 显示为 1.0 等。 我是这样解决的:

      g.set(xticks=list(range(1,19)))
      

      希望这是有用的。

      【讨论】:

        【解决方案4】:

        这是使用 matplotlib 修复刻度的直观方法:

        import matplotlib.pyplot as plt
        plt.scatter(x,y)
        plt.xticks(np.arange(min(x), max(x)+1, 1))
        plt.yticks(np.arange(min(y), max(y)+1, 1))
        

        代码在 seaborn 人物之后进入同一个单元格(在 jupyter notebook 的情况下)。因为 seaborn 使用 matplotlib 后端,所以它可以工作。

        【讨论】:

        • 问题是关于 seaborn 的,请考虑为您的回答提供解释
        • 好吧,也许它不是“la crème de la crème”,但 Artur 的东西可以解决问题:x= df["myvar1"] plt_plot = sns.relplot(x=x, y=" myvar2", kind="line", data=df, ci=None,aspect=2) plt.xticks(np.arange(min(x), max(x)+1, 1))
        【解决方案5】:

        您可以使用轴格式化程序来做到这一点:

        from  matplotlib.ticker import FuncFormatter
        

        然后,在你的条形图线之后:

        plt.gca().xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))
        

        【讨论】:

          猜你喜欢
          • 2023-01-28
          • 2020-07-08
          • 1970-01-01
          • 2018-10-01
          • 2013-03-11
          • 1970-01-01
          • 1970-01-01
          • 2019-12-06
          • 1970-01-01
          相关资源
          最近更新 更多