繁体   English   中英

Matplotlib:如何重新排列图表的 x 轴?

[英]Matplotlib: How can I reorder the graphs' x-axis?

我有以下代码:

def composition(tokens, title):
    '''creates a curve of composition for a corpus of texts'''
    token_lengths = [len(token) for token in tokens]
    fig = plt.figure()
    plt.gcf().subplots_adjust(bottom=0.15)
    len_distr = nltk.FreqDist(token_lengths)
    len_distr.plot(25, title=f'{title}')
    plt.show()
    fig.savefig(f'{title}.png')

它将 function 标记化的 output 作为标记,您必须为图形提供标题。 例如,我用标题标记文本并在图片中提供标题(longjumeau)。

在此处输入图像描述

x 轴是标记长度,即在它们出现之后排序的字长(或短)。 这样我就可以相互比较不同的文本图表。 它可能是一个条形图,我现在不太关心图表的类型。

编辑,bcs我不太清楚我有什么问题:如何按升序(2,3,4,5,6)对x轴值进行排序,而不是现在似乎按最高值排序y 轴。

如果需要更多代码,这是我的 git-repo,不是完美的代码,抱歉: https://github.com/WunschK/Stylometry

附加信息(未编辑,但可能需要):我的标记化 function:

def tokenize(text, language):
    '''Tokenises a given text (text) defined above and returns a list of tokens (tokens)'''
    tokens = nltk.word_tokenize(text=text.lower(), language=f"{language}")
    # strip punctuation of the list of word tokens:
    tokens = ([token for token in tokens if any(c.isalpha() for c in token)])
    return tokens

您有一个图表显示较短的单词比较长的单词更频繁地使用。 假设您正在计算每个单词的每次出现,并可能过滤掉 1 和 2 个字母的单词(例如,基于它们是停用词),我发现图表的形状在预期范围内。

例如,我提取了您的问题文本并按标记长度进行直方图,并过滤了一些标点符号和诸如此类的东西(大多数标记器都会这样做)。

text = """t takes the output of a function tokenize as tokens and you have to provide a title for the graph. For example, I tokenize a text with the title and provide the title longjumeau in the picture.
The x-axis is the token-length word length or shortness sorted after their occurence. So that I can compare different graphs for texts with each other. It might be a bar-diagram, I ont care too much about the kind of the graph at this moment.

Edit bcs I wasn't too clear about what question I have How can I order the x-axis values in ascending order as opposed to now seemingly being sorted by the highest value on the y-axis.

if further code is needed, this is my git-repo, not perfect code sorry 

additional info not edited but maybe necessary my tokenize function"""

lenList = [len(t) for t in text.split()]

import matplotlib.pyplot as plt
plt.figure(figsize=(7,5))
plt.hist(lenList, bins=10)
plt.grid(alpha = 0.3)
plt.title("Word Length Instance Histogram - KWunsch SO question text")
plt.show()

直方图的形状看起来有点熟悉,不是吗?

在此处输入图像描述

暂无
暂无

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

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