Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Logarithmic scale problem

  1. Dec 26, 2005 #1
    I am working on a homework for a programming class. We have to create a Logarithmic plot and add to it a marker when the program is running on the click of the mouse. That is NOT the problem :) , in fact, that's very simple!
    My problem, however, is with the scale. When my plot is in linear scale it adds the marker right where it should. By that I mean that if I click on point (1,2) it adds my marker on (1,2). Now, when I switch to logarithmic scale, if I click on (0,0) it adds the marker on (1,1). If I click on values greater than 10, it adds the marker at the place where I clicked. But when my values are lower than 10, the marker is shifted to the right. How do I solve that problem? I already tried converting the values I get from my mouse-click event to logarithmic values and they are wrong. In fact I get negative numbers when the values are lower than 1; so the marker is shifted to the left. I am not good at all with log scales; so, please help me!!
     
  2. jcsd
  3. Dec 26, 2005 #2

    SGT

    User Avatar

    It's difficult to know what is happening without seeing your code. Some ideas:
    The logarithm of 1 is 0, so when you click on (0,0) you are really clicking in (log 1, log 1). Anyway, you cannot have the poin (0,0) in a log scale, since log 0 = - infinity.
    The logarithm of a number between 0 and 1 is negative.
     
  4. Dec 26, 2005 #3
    The code is in Java. We are using a library called JFreeChart to create the chart. Here is the click event where everything happens:

    Code (Text):

    public void mouseClicked (MouseEvent e)

      {

        if (SwingUtilities.isRightMouseButton (e))

          return;

        if(pointerAdded)

            return;



        //These return the x,y position on the screen or screen location
        int x = e.getX ();

        int y = e.getY ();


        // Translates a screen location to a Java2D point.
        Point2D p = translateScreenToJava2D (new Point (x, y));


        //create Plot object
        XYPlot plot = getChart ().getXYPlot ();

       
        //get the chart renderer

        ChartRenderingInfo info = getChartRenderingInfo();


        //The area where the clicked occured
        Rectangle2D dataArea = info.getPlotInfo().getDataArea();




        //Get the plot coordinates of where the event ocurrs
        double xx = plot.getDomainAxis ().java2DToValue (p.getX (), dataArea, plot.getDomainAxisEdge ());


        double yy = plot.getRangeAxis ().java2DToValue (p.getY (), dataArea, plot.getRangeAxisEdge ());


       
     

        //Add the custom annotation

         CircleDrawer cd = new CircleDrawer(

                Color.RED, Color.BLACK , new BasicStroke(1.0f), null);

       
     

        XYAnnotation bestBid = new XYDrawableAnnotation(

                xx, yy, 11, 11, cd

            );

       


        this.renderer.addAnnotation(bestBid);

        pointerAdded = true;

        repaint ();

      }
     
    As you can see, the points come out straight from the plot.
    About what you say, you're right and I had noticed that before. Now, my question is, how do I go from (log 1, log 1) to my linear numbers so that I can get the right position?
     
  5. Dec 26, 2005 #4
    I just did a quick test to see what the event returns. As I click closer to 0, it results in a number sifted more and more to the right. I am only showing the x coordinate since the y have exactly the same results. Also, the resulting values are approximate since I did not zoom in close enough in the plot to click exactly on the number.

    Click on Result
    10 10
    9 9.118812375
    8 8.223735874
    7 7.307858871
    6 6.444623616
    5 5.50047321
    4 4.624704838
    3 3.70626008
    2 2.820675981
    1 1.90761842
    0 1.003834079

    it looks like a function, I just don't know how to find it. I think that if I find it, I can solve my problem.
    Thanks
     
  6. Dec 27, 2005 #5

    SGT

    User Avatar

    This is really weird. It is not a logarithmic function. if you call Y the vector of clicked points and X the vector of results, you obtain:
    y = 1.1104x - 1.1234.
     
  7. Dec 28, 2005 #6
    can you post the code? I have an idea, but don't know if it defeats the purpose or not.

    Would flooring the result be a work around?

    Matt
     
  8. Dec 29, 2005 #7

    SGT

    User Avatar

    The problem is with your labelling. There is no point (0,0) in a log plot. If you must plot numbers that are less then 1, you should put your origin at (o.1, 0.1) or (0.01, 0.01). Remember that the distance between 0.1 and 1 is the same as from 1 and 10. See anex graph. That is what makes the linear relationship between clicked point and result skewed.
     

    Attached Files:

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook