【问题标题】:Making the lines of the scatter plot smooth in MatPlotlib在 MatPlotlib 中使散点图的线条平滑
【发布时间】:2020-11-24 02:37:23
【问题描述】:

我想让下图的线条平滑。我试图搜索,似乎我们必须用浮点数或某种类型(如日期时间)来表示 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()

以下是我现在得到的图表。

【问题讨论】:

  • 平滑是什么意思?你的意思是实心的,还是弯曲的?
  • @Chris 现在你可以看到线条是直的。我想让虚线平滑。类似于此答案中的图表stackoverflow.com/a/5284038/5901897
  • “适合”三点的理由是什么?仅仅因为脚本允许您这样做,并不意味着这是一个好主意。

标签: python matplotlib scatter-plot


【解决方案1】:

也许你可以拟合曲线来“平滑”曲线

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 来拟合您的数据,并绘制预测曲线

【讨论】:

  • 我可以在第三点之后停止曲线而不是进一步向上吗?
  • @hEShan 只需修改buff_x
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 2021-09-30
  • 2014-08-17
  • 1970-01-01
相关资源
最近更新 更多