簡體   English   中英

在 MatPlotlib 中使散點圖的線條平滑

[英]Making the lines of the scatter plot smooth in MatPlotlib

我想讓下圖的線條平滑。 我嘗試搜索,似乎我們必須根據浮點數或某種類型(例如日期時間)來表示 x 軸。 在這里,由於 x 軸只是標簽,我無法弄清楚應該如何更改我的代碼。 任何幫助表示贊賞。

import matplotlib.pyplot as plt

x1 = [">1", ">10",">20"]

y1 = [18,8,3]
y2 = [22,15,10]
y3=[32,17,11]
fig = plt.figure()
ax1 = fig.add_subplot(111)

ax1.scatter(x1, y1, color='blue', label='Heuristic')
ax1.scatter(x1, y2, color='green', label='SAFE')
ax1.scatter(x1, y3, color='red', label='discovRE')

plt.plot(x1, y2, '.g:')
plt.plot(x1, y1, '.b:')
plt.plot(x1, y3, '.r:')
plt.ylabel('False Positives',fontsize=8)
plt.xlabel('Function instruction sizes',fontsize=8)

plt.legend()
plt.show()

以下是我現在得到的圖表。

在此處輸入圖片說明

也許你可以擬合一條曲線來“平滑”曲線

import matplotlib.pyplot as plt

x1 = [">1", ">10",">20"]

y1 = [18,8,3]
y2 = [22,15,10]
y3=[32,17,11]
fig = plt.figure()
ax1 = fig.add_subplot(111)

ax1.scatter(x1, y1, color='blue', label='Heuristic')
ax1.scatter(x1, y2, color='green', label='SAFE')
ax1.scatter(x1, y3, color='red', label='discovRE')

buff_x = np.linspace(0,2,100)
def reg_func(y):
    
    params = np.polyfit(range(len(y)),y,2)
    return np.polyval(params,buff_x)
    
plt.plot(buff_x, reg_func(y2), 'g',linestyle='dotted')
plt.plot(buff_x, reg_func(y1), 'b',linestyle='dotted')
plt.plot(buff_x, reg_func(y3), 'r',linestyle='dotted')
plt.ylabel('False Positives',fontsize=8)
plt.xlabel('Function instruction sizes',fontsize=8)

plt.legend()
plt.show()

如您所見,我使用函數reg_func來擬合您的數據,並繪制預測曲線

輸出

暫無
暫無

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

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