How to plot logarithmic Y Scale?

Q&A for Visual Basic, Visual Basic .NET, and PowerBasic developers using DPlot

Moderator: DPlotAdmin

Post Reply
JetDeveloper
Posts: 2
Joined: Wed Feb 16, 2005 1:49 pm

How to plot logarithmic Y Scale?

Post by JetDeveloper »

I am trying to plot with SCALE_LINEARX_LOGY, and show powers of 10 on the Y-Axis, but I cannot get it to work. It is still using a linear scale for the Y-Axis of 0, 1000, 2000,etc.

I would like it to show 1, 10, 100, etc. on the Y-Axis and have them equally spaced.

Also is it safe to use the .NET Bitmap class with IntPtr and Dispose(), rather than mapping to the Windows API and DeleteObject().
That way I can use Bitmap.FromHBitmap() to create a .NET Bitmap struct.
and set the PictureBox's Image property directly to the Bitmap instead of having to use the ugly Windows API CreateCompatibleDC(), BitBlt(), etc to draw the plot in my form.

I was wondering why the example does not do it the .NET way.

Anyway, here is the command string (for the plot). Please tell me how I would change it to get the log scale working on Y-Axis.

Code: Select all

cmds = "[ManualScale(0,1,10,10000)]"
        cmds = cmds & "[Caption(" & Chr(34) & "DPLOTLIB XY Test" & Chr(34) & ")]"
        cmds = cmds & "[FontPoints(1,8)][FontPoints(2,12)][FontPoints(3,12)]"
        cmds = cmds & "[FontPoints(4,10)][FontPoints(5,10)][FontPoints(6,8)]"
        cmds = cmds & "[Legend(1," & Chr(34) & "x^2" & Chr(34) & ")]"
        cmds = cmds & "[Legend(2," & Chr(34) & "cos({\sp}x)" & Chr(34) & ")]"
        cmds = cmds & "[DocMaximize()][ClearEditFlag()]"
Thanks



Code: Select all

Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
        Dim d As DPLOT
        Dim i As Integer
        Dim N As Integer
        Dim x() As Double
        Dim y() As Double
        Dim PI As Double
        Dim cmds As String
        Dim ret As Integer

        ' Plot sin(PI*x) and cos(PI*x) from x = 0 to 4.
        ' In this case we've used DATA_XYYY (one X array for one or more Y arrays).
        '
        ' Since the X values are evenly spaced we could also use
        ' DATA_DXY  - X has only 2 elements, DX and X0
        '
        ' And you can ALWAYS use
        ' DATA_XYXY - Each curve uses its own X array. This is the only
        '             option available if the curves have different X values or
        '             a different number of points.

        PI = 4.0# * System.Math.Atan(1.0#)
        N = 1001
        ReDim x(N - 1)
        ReDim y(2 * N - 1)

        For i = 0 To N - 1
            x(i) = (4.0# * i) / (N - 1)
            y(i) = System.Math.Sin(PI * x(i))
            y(i + N) = System.Math.Cos(PI * x(i))
        Next i



        d.Initialize()
        d.Version = DPLOT_DDE_VERSION
        d.DataFormat = DATA_XYYY
        d.MaxCurves = 2 ' Must be >= number of curves we plot
        d.MaxPoints = N ' Anything >= N will do
        d.NumCurves = 2
        d.ScaleCode = SCALE_LINEARX_LOGY
        d.LegendX = 0.05
        d.LegendY = 0.05
        d.NP(0) = N
        d.LineType(0) = LINESTYLE_SOLID
        d.LineType(1) = LINESTYLE_LONGDASH
        d.Title1 = "Data sent to DPlot via DPLOTLIB.DLL"
        d.XAxis = "x"
        d.YAxis = "y"

        cmds = "[ManualScale(0,10,10,10000)]"
        cmds = cmds & "[Caption(" & Chr(34) & "DPLOTLIB XY Test" & Chr(34) & ")]"
        cmds = cmds & "[FontPoints(1,8)][FontPoints(2,12)][FontPoints(3,12)]"
        cmds = cmds & "[FontPoints(4,10)][FontPoints(5,10)][FontPoints(6,8)]"
        cmds = cmds & "[Legend(1," & Chr(34) & "x^2" & Chr(34) & ")]"
        cmds = cmds & "[Legend(2," & Chr(34) & "cos({\sp}x)" & Chr(34) & ")]"
        cmds = cmds & "[DocMaximize()][ClearEditFlag()]"

        ret = GetClientRect(Picture1.Handle.ToInt32, rcPic)
        Dim rect As Rectangle = Picture1.ClientRectangle




        If (Not (myBmp Is Nothing)) Then
            myBmp.Dispose()
            myBmp = Nothing
        End If

        DocNum = DPlot_Plot8(d, x(0), y(0), cmds)
        If (DocNum > 0) Then
            DPM.size = Len(DPM)
            hBitmap2 = DPlot_GetBitmapEx(DocNum, rect.Right - rect.Left, rect.Bottom - rect.Top, DPM)
            myBmp = Bitmap.FromHbitmap(hBitmap2)
            HasMetrics = 1
            Picture1.Image = myBmp
            ' Be sure to do a FileClose after DPlot_GetBitmapEx (if you're done with the document)
            ret = DPlot_Command(DocNum, "[FileClose()]")
        End If

        
    End Sub
User avatar
DPlotAdmin
Posts: 2312
Joined: Tue Jun 24, 2003 9:34 pm
Location: Vicksburg, Mississippi
Contact:

Post by DPlotAdmin »

I am trying to plot with SCALE_LINEARX_LOGY, and show powers of 10 on the Y-Axis, but I cannot get it to work. It is still using a linear scale for the Y-Axis of 0, 1000, 2000,etc.
That's only because you're plotting cos(pi*x) and sin(pi*x). You can't plot negative values on a logarithmic scale.
Also is it safe to use the .NET Bitmap class with IntPtr and Dispose(), rather than mapping to the Windows API and DeleteObject().
That way I can use Bitmap.FromHBitmap() to create a .NET Bitmap struct.
and set the PictureBox's Image property directly to the Bitmap instead of having to use the ugly Windows API CreateCompatibleDC(), BitBlt(), etc to draw the plot in my form.

I was wondering why the example does not do it the .NET way.
Simple: the example author (me) doesn't speak .NET. The examples are just conversions of the standard VB source. I've no idea about the answer to your first question. If you rework the example "doing it the .NET way" I'd appreciate your sending me a copy.
Visualize Your Data
support@dplot.com
Post Reply