简体   繁体   English

如何降低seaborn中x-ticks的密度

[英]How to decrease the density of x-ticks in seaborn

I have some data, based on which I am trying to build a countplot in seaborn.我有一些数据,我试图根据这些数据在 seaborn 中构建计数图。 So I do something like this:所以我做这样的事情:

data = np.hstack((np.random.normal(10, 5, 10000), np.random.normal(30, 8, 10000))).astype(int)
plot_ = sns.countplot(data)

and get my countplot:并得到我的计数图:

在此处输入图片说明

The problem is that ticks on the x-axis are too dense (which makes them useless).问题是 x 轴上的刻度太密集(这使得它们无用)。 I tried to decrease the density with plot_.xticks=np.arange(0, 40, 10) but it didn't help.我试图用plot_.xticks=np.arange(0, 40, 10)降低密度,但没有帮助。

Also is there a way to make the plot in one color?还有一种方法可以用一种颜色制作情节吗?

Tick frequency滴答频率

There seem to be multiple issues here:这里似乎有多个问题:

    1. You are using the = operator while using plt.xticks.您在使用 plt.xticks 时使用了 = 运算符。 You should use a function call instead (but not here; read point 2 first)!您应该改用函数调用(但不是在这里;请先阅读第 2 点)!
    1. seaborn's countplot returns an axes-object, not a figure seaborn 的计数图返回一个轴对象,而不是一个数字
      • you need to use the axes-level approach of changing x-ticks (which is not plt.xticks() )您需要使用更改 x-ticks 的轴级方法(不是plt.xticks()

Try this:试试这个:

for ind, label in enumerate(plot_.get_xticklabels()):
    if ind % 10 == 0:  # every 10th label is kept
        label.set_visible(True)
    else:
        label.set_visible(False)

Colors颜色

I think the data-setup is not optimal here for this type of plot.我认为对于这种类型的图,这里的数据设置不是最佳的。 Seaborn will interpret each unique value as new category and introduce a new color. Seaborn 将每一个独特的价值诠释为新的品类,并推出新的颜色。 If i'm right, the number of colors / and x-ticks equals the number of np.unique(data).如果我是对的,颜色 / 和 x-ticks 的数量等于 np.unique(data) 的数量。

Compare your data to seaborn's examples (which are all based on data which can be imported to check).将您的数据与 seaborn 的示例进行比较(这些示例均基于可以导入进行检查的数据)。

I also think working with seaborn is much easier using pandas dataframes (and not numpy arrays; i often prepare my data in a wrong way and subset-selection needs preprocessing; dataframes offer more).我还认为使用 pandas 数据帧(而不是 numpy 数组;我经常以错误的方式准备数据并且子集选择需要预处理;数据帧提供更多)更容易与 seaborn 合作。 I think most of seaborn's examples use this data-input.我认为 seaborn 的大多数示例都使用此数据输入。

As a slight modification of the accepted answer, we typically select labels based on their value (and not index), eg to display only values which are divisible by 10, this would work:作为对已接受答案的轻微修改,我们通常根据标签的值(而不是索引)选择标签,例如仅显示可被 10 整除的值,这将起作用:

for label in plot_.get_xticklabels():
    if np.int(label.get_text()) % 10 == 0:  
        label.set_visible(True)
    else:
        label.set_visible(False)

Since you have tagged matplotlib , one solution different from setting the ticks visible True/False is to plot every n th label as following由于您已标记matplotlib ,与设置刻度可见True/False不同的一种解决方案是绘制每个第n个标签,如下所示

fig = plt.figure(); np.random.seed(123)

data = np.hstack((np.random.normal(10, 5, 10000), np.random.normal(30, 8, 10000))).astype(int)
plot_ = sns.countplot(data)

fig.canvas.draw()
new_ticks = [i.get_text() for i in plot_.get_xticklabels()]
plt.xticks(range(0, len(new_ticks), 10), new_ticks[::10])

在此处输入图片说明

even though this has been answered a while ago, adding another perhaps simpler alternative that is more flexible.尽管这已经在不久前得到了回答,但添加了另一个可能更简单的更灵活的替代方案。

you can use an matplotlib axis tick locator to control which ticks will be shown.您可以使用matplotlib 轴刻度定位器来控制将显示哪些刻度。

in this example you can use LinearLocator to achieve the same thing:在本例中,您可以使用LinearLocator来实现相同的目的:

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.ticker as ticker

data  = np.hstack((np.random.normal(10, 5, 10000), np.random.normal(30, 8, 10000))).astype(int)
plot_ = sns.countplot(data)
plot_.xaxis.set_major_locator(ticker.LinearLocator(10))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM