【问题标题】:How can I make my plot smoother in Python?如何在 Python 中使我的情节更流畅?
【发布时间】:2015-08-07 14:07:35
【问题描述】:

我有一个名为 calculate_cost 的函数,它计算供应商在不同 S_range(库存水平)下的表现。该函数有效,但绘图不平滑,有没有办法在 Python 中平滑它?

import numpy
import scipy.stats
import scipy.integrate
import scipy.misc
import matplotlib
import math
import pylab
from scipy.stats import poisson

def calculate_cost(s, h, d, r, k, alphaR):
    cost = 0.0
    for i in range(0, alphaR + 1):
        #i = i-1
        binom = math.factorial(r) / ((math.factorial(i)) * (math.factorial(r - i)))
        func = scipy.stats.poisson.cdf(s, d)
        cost +=  ((k/r) * binom * (func ** i) * ((1.0-func) ** (r-i)))

    for p in range (s):
        cost += h*(s-p)*scipy.stats.poisson.pmf(p, d)  #This a formula

    return cost

graphs = []

class Graph:
    def __init__(self):
        self.label = ""
        self.h = 0
        self.d = 0
        self.r = 0
        self.k = 0
        self.alphaR = 0

graph = Graph()
graph.label = "A"
graph.h = 1.0
graph.d = 10
graph.r = 30
graph.k = 283.0
graph.alphaR = 23

graphs.append(graph)

graph = Graph()
graph.label = "B"
graph.h = 1.0
graph.d = 10
graph.r = 30
graph.k = 146.0
graph.alphaR = 24
#graph.LineStyle = '*-'


graphs.append(graph)

graph = Graph()
graph.label = "C"
graph.h = 1.0
graph.d = 10
graph.r = 30
graph.k = 92.0
graph.alphaR = 25
#graph.LineStyle = '*-'


graphs.append(graph)

graph = Graph()
graph.label = "D"
graph.h = 1.0
graph.d = 10
graph.r = 30
graph.k = 80.0
graph.alphaR = 26
#graph.LineStyle = '*-'


graphs.append(graph)

graph = Graph()
graph.label = "E"
graph.h = 1.0
graph.d = 10
graph.r = 30
graph.k = 77.0
graph.alphaR = 27
#graph.LineStyle = '*-'


graphs.append(graph)


s_range = numpy.arange(0,21,1)

for graph in graphs:
    cost = []

    for s in s_range:
        cost.append(calculate_cost(s, graph.h, graph.d, graph.r, graph.k, graph.alphaR))

    matplotlib.pyplot.plot(s_range, cost,  label = graph.label)

    pylab.legend()
    matplotlib.pyplot.xlabel(' S_range')
    matplotlib.pyplot.ylabel('Cost')


pylab.show()

【问题讨论】:

    标签: python numpy plot scipy smooth


    【解决方案1】:

    一种解决方案是将 scipy.itrp1D 函数与“立方”类型一起使用:

    from scipy import interpolate
    ....
    s_range = numpy.arange(0,21,1)
    
    for graph in graphs:
        cost = []
    
        for s in s_range:
            cost.append(calculate_cost(s, graph.h, graph.d, graph.r, graph.k, graph.alphaR))
    
        f = interpolate.interp1d(s_range, cost, kind='cubic')
    
        s_range_new = np.arange(0,20, 0.1)
        cost_new = f(s_range_new) 
    
        matplotlib.pyplot.plot(s_range_new, cost_new,  label = graph.label)
    
        pylab.legend()
        matplotlib.pyplot.xlabel(' S_range')
        matplotlib.pyplot.ylabel('Cost')
    
    
    pylab.show()
    

    这给了你:

    谨慎使用它,因为这只是插值点,而不是真实数据点。

    希望对你有帮助

    【讨论】:

    • 非常感谢你,是的,这就是我想要的,:),我不知道如何感谢你
    • 没问题,如果这是您要找的,请接受答案,以便其他有类似问题的人更容易找到它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多