0

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

hEShaN
  • 551
  • 1
  • 9
  • 27
  • What do you mean smooth? You mean solid, or you mean curved? – Chris Nov 24 '20 at 02:45
  • @Chris right now as you could see lines are straight. I want to make the dotted line smooth. something like the graph in this answer https://stackoverflow.com/a/5284038/5901897 – hEShaN Nov 24 '20 at 02:51
  • 2
    What is the reasoning to "fit" three points? Just because the script allows you to do this, does not mean, it is a good idea. – Mr. T Nov 24 '20 at 03:26

1 Answers1

2

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

meTchaikovsky
  • 7,478
  • 2
  • 15
  • 34