簡體   English   中英

Pyplot拒絕顯示網格

[英]Pyplot refuses to show grid

我有一個Python腳本,該腳本具有3個可繪制數據的函數。 . 其中2個使用來顯示網格線。 但是,沒有。 即使我到處都發送了ax.grid(b = True)垃圾郵件……我肯定做錯了什么,但是那又是什么呢?

def plotMSEProgress(times, bestScores, scores, xsplit=0, window=1):
    plot, ax = plt.subplots(figsize=(20,10), num=1)
    ax.grid(b=True, which='both')

    # plot = plt.figure(window)
    plt.ion()
    plt.minorticks_on()
    ax.grid(b=True, which='both')

    plt.show()

    plt.clf()

    if xsplit:
        plt.axvline(x=xsplit, color='g')

    plot = plt.plot_date(times, bestScores, '-', label="best score")
    plot = plt.setp(plot, color='y', linewidth=1.0)
    plot = plt.plot_date(times, scores, '-', label="score")
    plot = plt.setp(plot, color='b', linewidth=1.0)

    ax.grid(b=True, which='both')
    plt.xlabel('time')
    plt.ylabel('MSE')
    plt.suptitle('MSE over time', fontsize=16)
    plt.legend()
    ax.grid(b=True, which='both')
    plt.draw()
    ax.grid(b=True, which='both')
    plt.pause(0.001)        
    ax.grid(b=True, which='both')
    plt.plot()
    ax.grid(b=True, which='both')

? 也許與 因為我沒有其他顯示網格的繪圖函數。

, but to no avail sadly. 我已經嘗試過這個這個通過添加但無濟於事黯然。

有什么明顯的我想念的東西嗎? 還是還有其他隱藏的不兼容之處?

按要求繪制的屏幕截圖: 在此處輸入圖片說明

在函數內添加對plt.grid()的調用,並刪除無關的代碼:

import matplotlib.pyplot as plt
import datetime

def plotMSEProgress(times, bestScores, scores, xsplit=0, window=1):
    plot, ax = plt.subplots(figsize=(20,10), num=1)

    plt.ion()    
    plt.clf()

    if xsplit:
        plt.axvline(x=xsplit, color='g')

    plot = plt.plot_date(times, bestScores, '-', label="best score")
    plot = plt.setp(plot, color='y', linewidth=1.0)
    plot = plt.plot_date(times, scores, '-', label="score")
    plot = plt.setp(plot, color='b', linewidth=1.0)

    plt.minorticks_on()
    plt.grid(which='major')
    plt.grid(which='minor', linestyle = ':')

    plt.xlabel('time')
    plt.ylabel('MSE')
    plt.suptitle('MSE over time', fontsize=16)
    plt.legend(loc=2)
    plt.draw()
    plt.pause(0.001)

# Generate example data
base = datetime.datetime.today()
times = [base + datetime.timedelta(seconds=x) for x in range(0, 100)]
scores = np.random.rand(len(times))*30
bestScores = np.random.rand(len(times))*5

# Generate plot dynamically
for i in range(len(times)):
    plotMSEProgress(times[0:i], bestScores[0:i], scores[0:i], xsplit=0, window=1)

該代碼生成一個圖並動態更新它,同時始終顯示網格線。

用網格線繪制

我認為您有一些不必要的代碼,可以創建多個圖。 您擁有的第一個圖是空的,但帶有網格,而后面的圖包含數據,但不包含網格。

試試下面的代碼。 我評論了您的一些腳本並使它起作用。

def plotMSEProgress(times, bestScores, scores, xsplit=0, window=1):
    plot, ax = plt.subplots(figsize=(20,10), num=1)
    ax.grid(b=True, which='both')

    # plot = plt.figure(window)
    plt.ion()
    plt.minorticks_on()
    ax.grid(b=True, which='both')

#     plt.show()

#     plt.clf()

    if xsplit:
        plt.axvline(x=xsplit, color='g')

    plot = plt.plot(times, bestScores, '-', label="best score") # you can change it back to plot_date
    plot = plt.setp(plot, color='y', linewidth=1.0)
    plot = plt.plot(times, scores, '-', label="score") # you can change it back to plot_date
    plot = plt.setp(plot, color='b', linewidth=1.0)

    ax.grid(b=True, which='both')
    plt.xlabel('time')
    plt.ylabel('MSE')
    plt.suptitle('MSE over time', fontsize=16)
    plt.legend()
    ax.grid(b=True, which='both')
    plt.draw()
    ax.grid(b=True, which='both')
    plt.pause(0.001)        
    ax.grid(b=True, which='both')
#     plt.plot()
    ax.grid(b=True, which='both')

times = list(range(0,100))
bestScores = list(range(100,200))
scores = list(range(150,250))
xsplit=0
window=1
plotMSEProgress(times, bestScores, scores, xsplit=0, window=1)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM