下面是我以前写的一个正态分布函数的例子Private Sub Form_Click() '描点 Me.Line (-3, 0)-(3, 0), vbBlue Me.Line (0, -2)-(0, 0), vbBlue Me.CurrentX = 0 Me.CurrentY = 0 Me.Print "0" For i = -3 To 3 Step 0.01 PSet (i, -1 * norm(i, 0, 1)), vbRed Next End SubPrivate Sub Form_Load() With Me .Move 1000, 1000, 6000, 4000 .ScaleLeft = -3 .ScaleTop = -2 .ScaleHeight = 2.5 .ScaleWidth = 6 End With End Sub Function norm(ByVal x As Double, ByVal avg As Double, ByVal sigma As Double) As Double '均值avg,标准差sigma的正态曲线函数 norm = 1 / sigma / Sqr(8 * Atn(1)) norm = Exp((x - avg) ^ 2 / -2 / sigma ^ 2) * norm End Function
'窗体一个大一些的图象框Image1,一个按钮:Private Sub Command1_Click() Image1.Stretch = True '需先引用Excel 9.0对象库 Dim xlsapp As New Excel.Application Dim xlsbook As Excel.Workbook Dim xlssheet As Excel.Worksheet Set xlsbook = xlsapp.Workbooks.Add Set xlssheet = xlsbook.Worksheets("sheet1") 'xlsapp.Visible = True '坐标值 For x = -5 To 5 'X的取值 xlssheet.Cells(x + 6, 1) = x Y = x '改为y=x^2为抛物线 xlssheet.Cells(x + 6, 2) = Y Next '插入图表 xlssheet.Range("A1:B11").Select xlsapp.Charts.Add xlsapp.ActiveChart.ChartType = xlXYScatterSmooth xlsapp.ActiveChart.SetSourceData Source:=xlssheet.Range("A1:B11"), PlotBy _ :=xlColumns xlsapp.ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" With xlsapp.ActiveChart .HasTitle = False .Axes(xlCategory, xlPrimary).HasTitle = False .Axes(xlValue, xlPrimary).HasTitle = False .ChartArea.Select .ChartArea.Copy End With xlsapp.DisplayAlerts = False xlsbook.Close xlsapp.Quit Set xlssheet = Nothing Set xlsapp = Nothing
'粘贴图表 Image1.Picture = Clipboard.GetData End Sub
不需要图例的代码:Private Sub Command1_Click() Image1.Stretch = True '需先引用Excel 9.0对象库 Dim xlsapp As New Excel.Application Dim xlsbook As Excel.Workbook Dim xlssheet As Excel.Worksheet Set xlsbook = xlsapp.Workbooks.Add Set xlssheet = xlsbook.Worksheets("sheet1") 'xlsapp.Visible = True '坐标值 For x = -5 To 5 xlssheet.Cells(x + 6, 1) = x Y = x ^ 2 '改为y=x^2为抛物线 xlssheet.Cells(x + 6, 2) = Y Next '插入图表 xlssheet.Range("A1:B11").Select xlsapp.Charts.Add xlsapp.ActiveChart.ChartType = xlXYScatterSmooth xlsapp.ActiveChart.SetSourceData Source:=xlssheet.Range("A1:B11"), PlotBy _ :=xlColumns xlsapp.ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" xlsapp.ActiveChart.HasLegend = False
With xlsapp.ActiveChart .HasTitle = False .Axes(xlCategory, xlPrimary).HasTitle = False .Axes(xlValue, xlPrimary).HasTitle = False .ChartArea.Select .ChartArea.Copy End With xlsapp.DisplayAlerts = False xlsbook.Close xlsapp.Quit Set xlssheet = Nothing Set xlsapp = Nothing
Me.Line (-3, 0)-(3, 0), vbBlue
Me.Line (0, -2)-(0, 0), vbBlue
Me.CurrentX = 0
Me.CurrentY = 0
Me.Print "0"
For i = -3 To 3 Step 0.01
PSet (i, -1 * norm(i, 0, 1)), vbRed
Next
End SubPrivate Sub Form_Load()
With Me
.Move 1000, 1000, 6000, 4000
.ScaleLeft = -3
.ScaleTop = -2
.ScaleHeight = 2.5
.ScaleWidth = 6
End With
End Sub
Function norm(ByVal x As Double, ByVal avg As Double, ByVal sigma As Double) As Double '均值avg,标准差sigma的正态曲线函数
norm = 1 / sigma / Sqr(8 * Atn(1))
norm = Exp((x - avg) ^ 2 / -2 / sigma ^ 2) * norm
End Function
Image1.Stretch = True
'需先引用Excel 9.0对象库
Dim xlsapp As New Excel.Application
Dim xlsbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet
Set xlsbook = xlsapp.Workbooks.Add
Set xlssheet = xlsbook.Worksheets("sheet1")
'xlsapp.Visible = True
'坐标值
For x = -5 To 5 'X的取值
xlssheet.Cells(x + 6, 1) = x
Y = x
'改为y=x^2为抛物线
xlssheet.Cells(x + 6, 2) = Y
Next
'插入图表
xlssheet.Range("A1:B11").Select
xlsapp.Charts.Add
xlsapp.ActiveChart.ChartType = xlXYScatterSmooth
xlsapp.ActiveChart.SetSourceData Source:=xlssheet.Range("A1:B11"), PlotBy _
:=xlColumns
xlsapp.ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
With xlsapp.ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
.ChartArea.Select
.ChartArea.Copy End With
xlsapp.DisplayAlerts = False
xlsbook.Close
xlsapp.Quit
Set xlssheet = Nothing
Set xlsapp = Nothing
'粘贴图表
Image1.Picture = Clipboard.GetData
End Sub
Image1.Stretch = True
'需先引用Excel 9.0对象库
Dim xlsapp As New Excel.Application
Dim xlsbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet
Set xlsbook = xlsapp.Workbooks.Add
Set xlssheet = xlsbook.Worksheets("sheet1")
'xlsapp.Visible = True
'坐标值
For x = -5 To 5
xlssheet.Cells(x + 6, 1) = x
Y = x ^ 2
'改为y=x^2为抛物线
xlssheet.Cells(x + 6, 2) = Y
Next
'插入图表
xlssheet.Range("A1:B11").Select
xlsapp.Charts.Add
xlsapp.ActiveChart.ChartType = xlXYScatterSmooth
xlsapp.ActiveChart.SetSourceData Source:=xlssheet.Range("A1:B11"), PlotBy _
:=xlColumns
xlsapp.ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
xlsapp.ActiveChart.HasLegend = False
With xlsapp.ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
.ChartArea.Select
.ChartArea.Copy End With
xlsapp.DisplayAlerts = False
xlsbook.Close
xlsapp.Quit
Set xlssheet = Nothing
Set xlsapp = Nothing
'粘贴图表
Image1.Picture = Clipboard.GetData
End Sub