2017-01-20 58 views
0

這是代碼:無法連接點之間線pyplot

def make_ax(fdist,N): 

     ys= list(fdist.values()) 

     for i in range(len(ys)): 
      ys[i] = (ys[i]/N) 

     d = (list(fdist.values())) 
     c= list(reversed(sorted(list(set(d))))) 
     xs =[] 
     for i in range(len(d)): 

      xs.append((c.index(d[i])+1)) 

     return xs,ys 


def plot_graph(words): 
    fdist = FreqDist(words) 
    axis_x,axis_y= make_ax(fdist,len(words)) 
    plt.figure() 
    plt.xlabel('log rank') 
    plt.ylabel('log Probability') 
    plt.plot(axis_x,axis_y,'ko') 

現在,XS和相同的長度YS,他們是列表。 但由於某種原因,我不斷收到此:

如何連接點之間的一條線? enter image description here

UPDATE: 如果我寫plt.plot(axis_x,axis_y,'ko-')它給了我這樣的: enter image description here

+0

你試過'plt.plot(axis_x,axis_y指令,「k - ')'而不是? –

+0

@JoséSánchez是的,沒有幫助。請參閱更新 – user3552460

+1

正如您從答案和評論中看到的那樣,人們肯定會試圖幫助您,但您沒有真正幫助他們,因爲不提供能夠再現問題的[MCVE],並且不會提及「words」從什麼'FreqDist'是。因此,人們處於黑暗中,只能猜測可能是錯的。 – ImportanceOfBeingErnest

回答

2

您使用nltk這裏,其FreqDist方法返回一個未排序列表。
因此,您需要按相反的順序對該列表進行排序。但是,您無需以任何方式對x值進行排序。

爲了達到這個目的,使用numpy可能是有意義的,但這不是必需的。
用於繪圖使用linestyle="-"獲得一條線。

下面的圖中白鯨的80個高頻詞的概率分佈由赫爾曼·梅爾維爾:

import matplotlib.pyplot as plt 
import numpy as np 
from nltk import FreqDist 
from nltk.book import text1 

def make_ax(fdist,N): 
     # normalize frequency values 
     ys = np.array(fdist.values())/float(N) 
     # sort frequency values, then invert list, such that most frequent words come first 
     ys = np.sort(ys)[::-1] 
     #create an x range 
     xs = np.arange(len(ys))+1. 
     return xs,ys 


def plot_graph(words): 
    fdist = FreqDist(words) 
    axis_x,axis_y= make_ax(fdist,len(words)) 

    plt.figure() 
    plt.xlabel('rank') # no log here in this example 
    plt.ylabel('Probability') 
    plt.plot(axis_x[:80],axis_y[:80], #only plot the 80 most frequent words 
      color="k", marker="o", markersize="2", linestyle="-", linewidth=0.8) 
    # equall possible: 
    # plt.plot(axis_x[:80],axis_y[:80], "ko-") 

plot_graph(text1) 
plt.show() 

enter image description here

1

通過plt.plot(axis_x,axis_y,'ko-')更換plt.plot(axis_x,axis_y,'ko')-告訴pyplot在點之間畫線。

有關選項(線條樣式,標記樣式...)的更多詳細信息,請參閱plt.plot文檔。

編輯

我修改make_ax到積分排序:現在

def make_ax(fdist,N): 

    ys= list(fdist.values()) 

    for i in range(len(ys)): 
     ys[i] = (ys[i]/N) 

    d = (list(fdist.values())) 
    c= list(reversed(sorted(list(set(d))))) 
    xs =[] 
    for i in range(len(d)): 

     xs.append((c.index(d[i])+1)) 
    # make a list of tuples of coordinates 
    points = list(zip(xs, ys)) 
    # sort point according to their x coordinate 
    points.sort(key=lambda point: point[0]) 
    axis_x, axis_y = [], [] 
    for x, y in points: 
     axis_x.append(x) 
     axis_y.append(y) 
    return axis_x, axis_y 

和,線條應該是正確的點之間。

+0

沒有解決,請參閱更新 – user3552460

+1

@ user3552460我假設axis_x沒有排序,所以您的(x,y)點不是按照遞增的x值的順序繪製的,因此Zigzagging行 –

+0

但點很好 – user3552460

0

使用plt.plot(axis_x, axis_y, marker='o', color='k' linestyle='solid')或者:plt.plot(axis_x, axis_y, marker='o', color='k' linestyle='-')

這是很長的寫,但會增加你的代碼的可讀性也。在python中指定關鍵字也是一種很好的做法。

plt.plot()文檔:Matplotlib.pyplot.plot