hEShaN
hEShaN

Reputation: 581

Making the lines of the scatter plot smooth in MatPlotlib

I want to make the lines of the following graph smooth. I tried to search and it seems that we have to represent the x-axis in terms of a float or some type such as date time. Here since the x-axis are just labels, I could not figure out how I should change my code. Any help is appreciated.

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()

Following is the graph that I get right now.

enter image description here

Upvotes: 0

Views: 568

Answers (1)

meTchaikovsky
meTchaikovsky

Reputation: 7666

Maybe you can fit a curve to 'smooth' the curve

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()

as you can see, I use a function reg_func to fit your data, and plot the predicted curves

output

Upvotes: 2

Related Questions