我有一定数量的numpy数组。我希望使用这个数组并使用填充和插值来改变点的数量,但是在MatPlotLib之类的绘图上使数组看起来相同。
我尝试了np.repeat
,np.kron
,np.lib.pad
等功能但收效甚微。
例如)
lista = [1,2,3,4,5]
包含5分
我希望将此重塑为10分
listb = [1,1.5,2,2.5,3,3.4,4,4.5,5,5.5]
包含10个点,但在绘制时仍然看起来相同
答案 0 :(得分:0)
看看http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.interp1d.html#scipy.interpolate.interp1d
然后尝试不同的样条线Private Sub UserForm_Initialize()
LoadTreeViewManager
End Sub
Private Sub LoadTreeViewManager()
Dim MyTreeView As TreeView
Set MyTreeView = Me.TreeView1
MyTreeView.Nodes.Clear
Call LoadTreeViewAddParentNode(MyTreeView)
End Sub
Private Sub LoadTreeViewAddParentNode(MyTreeView As TreeView)
Dim MyTreeNode As node
Dim MyRoot As String
MyRoot = "Territory"
Set MyTreeNode = MyTreeView.Nodes.Add(Key:="Key0", Text:=MyRoot)
Call LoadTreeViewAddChildNode(MyTreeView, MyTreeNode, 1)
End Sub
Private Sub LoadTreeViewAddChildNode(MyTreeView As TreeView, MyTreeNode As node, MyLevel)
Dim MyTreeNodeChild As node, MyTreeNodeName As String, MyTreeNodeText As String
Dim s() As String, i As Integer
Dim MyColRange As String
'This is the problem area.
MyLevel = MyLevel + 1
If MyLevel > Range("A1:E10").Columns.Count - 1 Then Exit Sub
MyColRange = ReturnRange(Range("A1:E10"), MyTreeNode.Text, MyLevel, MyLevel + 1)
If MyColRange = "False" Then Exit Sub
s = Split(SetUniqueValues(Range(MyColRange)), ",")
For i = 0 To UBound(s)
MyTreeNodeName = s(i) & i
MyTreeNodeText = s(i)
Set MyTreeNodeChild = MyTreeView.Nodes.Add(Relative:=MyTreeNode, Relationship:=tvwChild, Key:=MyTreeNodeName, Text:=MyTreeNodeText)
Call LoadTreeViewAddChildNode(MyTreeView, MyTreeNodeChild, MyLevel)
Next
End Sub
Public Function SetUniqueValues(RawData As Range) As String
Dim r As Range, c As String
c = ""
For Each r In RawData
If r.Value <> c Then
SetUniqueValues = SetUniqueValues & "," & r.Value
End If
c = r.Value
Next
SetUniqueValues = Right(SetUniqueValues, Len(SetUniqueValues) - 1)
End Function
Public Function ReturnRange(MyDataSet As Range, MyLookupItem, MyLookupCol, MyReturnCol) As String
Dim MyData As Range, MyColumn As Range
Dim MyRow1, MyRow2
ReturnRange = False
Set MyData = MyDataSet
Set MyColumn = MyData.Columns(MyLookupCol).Cells
MyRow1 = Evaluate("=IFERROR(MATCH(""" & MyLookupItem & """," & MyColumn.Address & ",0),0)")
If MyRow1 = 0 Then Exit Function
MyRow2 = MyRow1 + Evaluate("=COUNTIF(" & MyColumn.Address & ",""" & MyLookupItem & """)") - 1
ReturnRange = Range(Cells(MyRow1, MyReturnCol), Cells(MyRow2, MyReturnCol)).Address
End Function
。如果您的数据不是很平滑,则较高阶样条可能会产生不希望的波动,因为它们会尝试使用平滑曲线连接数据(匹配第一阶甚至二阶导数)。
答案 1 :(得分:0)
如果你只是一个线性函数拟合线性函数并在想要的值上进行评估:
import numpy as np
x_axis = np.arange(5) # These are your x-values for plotting with matplotlib
lista = [1,2,3,4,5]
所以现在拟合线性多项式:
linear_fit = np.poly1d(np.polyfit(x_axis, lista, 1))
并在同一网格上评估它,但步骤更多:
linear_fit(np.linspace(x_axis[0], x_axis[-1], 9))
# array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])
绘图时确保使用新的x-grid:
import matplotlib.pyplot as plt
new_x = np.linspace(x_axis[0], x_axis[-1], 9)
plt.scatter(new_x, linear_fit(new_x), color='b')
plt.plot(x_axis, lista, color='r')
plt.show()