【问题标题】:Tick labels for y axis are very long. How to truncate them in seaborn?y 轴的刻度标签很长。如何在seaborn中截断它们?
【发布时间】:2017-08-02 07:38:42
【问题描述】:

我正在绘制来自 kaggle 的“麦当劳菜单”数据。我用食物绘制了营养物质。但是,y-tick-labels 很长。如何截断它们以使其正确适应?

grouped = df.groupby(df["Protein"])
item = grouped["Item"].sum()
item_list = item.sort_index()
item_list = item_list[-20:]
#Sizing the image canvas
plt.figure(figsize=(8,9))
#To plot bargraph
sns.barplot(item_list.index,item_list.values)

如何纠正它,它会改善我的情节吗?

【问题讨论】:

  • 您必须决定如何压缩信息。例如您只想使用每个字符串中的最后 6 个字母吗?这很容易做到,但可能信息量不大。
  • 我是否可以保留最后 6-10 个字母,当我将鼠标悬停在条上时,它会显示该特定条的两个 x&y 标签?

标签: python python-2.7 matplotlib seaborn


【解决方案1】:

一个想法可能是让标签中的文本像选框一样滚动。

import matplotlib.pyplot as plt
import matplotlib.text
import matplotlib.animation

class Roller():
    def __init__(self):
        self.texts = []

    def append(self, text, **kwargs):
        RollingText.assimilate(text,  **kwargs)
        self.texts.append(text)

    def roll(self, i=0):
        for text in self.texts:
            text.roll()

    def ticklabelroll(self, i=0):
        self.roll()
        ticklabels = [t.get_text() for t in self.texts]
        plt.gca().set_yticklabels(ticklabels)



class RollingText(matplotlib.text.Text):
    n = 10
    p = 0
    def __init__(self, *args, **kwargs):
        self.n = kwargs.pop("n", self.n)
        matplotlib.text.Text(*args, **kwargs)
        self.set_myprops()

    def set_myprops(self, **kwargs):
        self.fulltext = kwargs.get("fulltext", self.get_text())
        self.n = kwargs.get("n", self.n)
        if len(self.fulltext) <=self.n:
            self.showntext = self.fulltext
        else:
            self.showntext = self.fulltext[:self.n]
        self.set_text(self.showntext)

    def roll(self, by=1):
        self.p += by
        self.p = self.p % len(self.fulltext)
        if self.p+self.n <= len(self.fulltext):
            self.showntext = self.fulltext[self.p:self.p+self.n]
        else:
            self.showntext = self.fulltext[self.p:] + " " + \
                        self.fulltext[0:(self.p+self.n) % len(self.fulltext)]          
        self.set_text(self.showntext)

    @classmethod
    def assimilate(cls, instance, **kwargs):
        # call RollingText.assimilate(Text, n=10, ...)
        instance.__class__ = cls 
        instance.set_myprops(**kwargs)

if __name__ == "__main__":
    import pandas as pd
    import seaborn as sns

    fn = r"data\nutrition-facts-for-mcdonald-s-menu\menu.csv"
    df = pd.read_csv(fn)

    grouped = df.groupby(df["Protein"])
    item = grouped["Item"].sum()
    item_list = item.sort_index()
    item_list = item_list[-20:]

    fig, ax = plt.subplots(figsize=(10,7.5))
    plt.subplots_adjust(left=0.3)

    sns.barplot(item_list.index,item_list.values, ax=ax)

    # create a Roller instance
    r = Roller()
    # append all ticklabels to the Roller
    for tl in ax.get_yticklabels():
        r.append(tl, n=25) 

    #animate the ticklabels
    ani = matplotlib.animation.FuncAnimation(fig, r.ticklabelroll, 
                            frames=36, repeat=True, interval=300)
    ani.save(__file__+".gif", writer="imagemagick")
    plt.show()

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2020-12-24
    • 2019-10-29
    • 2020-08-05
    • 2015-12-18
    相关资源
    最近更新 更多