简体   繁体   English

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

[英]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.

相关问题 Matplotlib:在指定的x轴范围内绘制两个图形 - Matplotlib: Plotting two graphs within a specified x-axis range Matplotlib多个图,x轴下方有额外空间 - Matplotlib multiple graphs, extra space underneath x-axis 绘制在matplotlib中共享x轴的两个图形 - Plotting two graphs that share an x-axis in matplotlib 我可以将 label 的 x 轴设为 \rho,即 matplotlib 中的希腊字母吗? - Can I label the x-axis as \rho, the Greek letter in matplotlib? 如何在彼此之上创建 2 个图形,它们都具有相同的 x 轴和不同的 y 轴? - How can would I create 2 graphs on top of each other with both having the same x-axis and different y-axis? 如何在matplotlib中的pdf上显示图例而不使其与某些图形的x轴重叠? - How does one display a legend on a pdf in matplotlib without it overlapping the x-axis of some graphs? 如何使用matplotlib / seaborn和pandas数据框创建带有共享x轴的上下条形图 - how to create upside down bar graphs with shared x-axis with matplotlib / seaborn and a pandas dataframe 如何指定要在x轴上绘制的离散值(matplotlib,boxplot)? - How can I specify the discrete values that I want to plot on the x-axis (matplotlib, boxplot)? Matplotlib:当我在x轴上有日期时,如何添加交替的背景色? - Matplotlib: How can I add an alternating background color when I have dates on the x-axis? 如何在matplotlib中设置x轴? - How can you set the x-axis in matplotlib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM