[英]Matplotlib: How can I reorder the graphs' x-axis?
I have the following code:我有以下代码:
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')
it takes the output of a function tokenize as tokens and you have to provide a title for the graph.它将 function 标记化的 output 作为标记,您必须为图形提供标题。 For example, I tokenize a text with the title and provide the title (longjumeau) in the picture.
例如,我用标题标记文本并在图片中提供标题(longjumeau)。
The x-axis is the token-length, ie word length (or shortness) sorted after their occurence. x 轴是标记长度,即在它们出现之后排序的字长(或短)。 So that I can compare different graphs for texts with each other.
这样我就可以相互比较不同的文本图表。 It might be a bar-diagram, I on't 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 (2,3,4,5,6) as opposed to now seemingly being sorted by the highest value on the y-axis.编辑,bcs我不太清楚我有什么问题:如何按升序(2,3,4,5,6)对x轴值进行排序,而不是现在似乎按最高值排序y 轴。
if further code is needed, this is my git-repo, not perfect code, sorry: https://github.com/WunschK/Stylometry如果需要更多代码,这是我的 git-repo,不是完美的代码,抱歉: https://github.com/WunschK/Stylometry
additional info (not edited, but maybe necessary): my tokenize function:附加信息(未编辑,但可能需要):我的标记化 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
You have a graph showing that shorter words are used more frequently than longer words.您有一个图表显示较短的单词比较长的单词更频繁地使用。 Assuming that you are counting every occurrence of every word, and perhaps filtering out 1 and 2 letter words (eg on the basis of their being stop words), I find the graph's shape to be within expectation.
假设您正在计算每个单词的每次出现,并可能过滤掉 1 和 2 个字母的单词(例如,基于它们是停用词),我发现图表的形状在预期范围内。
For example, I took the text of your question and histogrammed by token length, with some filtering of punctuation and whatnot (which most tokenizers do).例如,我提取了您的问题文本并按标记长度进行直方图,并过滤了一些标点符号和诸如此类的东西(大多数标记器都会这样做)。
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()
The histogram shape looks kinda familiar, no?直方图的形状看起来有点熟悉,不是吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.