What is the issue with log scales and plotting values less than 1?

In summary: How can that be? Can you help me debug this code?In summary, the code is working, but the scale is incorrect.
  • #1
lgarcia12
3
0
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!
 
Physics news on Phys.org
  • #2
lgarcia12 said:
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!
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.
 
  • #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:
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?
 
  • #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
 
  • #5
lgarcia12 said:
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

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.
 
  • #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
 
  • #7
SGT said:
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.
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.
 

Attachments

  • Logplot.jpg
    Logplot.jpg
    41.8 KB · Views: 474

1. What is a logarithmic scale problem?

A logarithmic scale problem is a mathematical problem that involves using logarithms to solve for a given variable. Logarithms are mathematical functions that are the inverse of exponential functions, and are used to represent numbers in a more condensed way.

2. Why is a logarithmic scale used?

A logarithmic scale is used to represent a large range of numbers in a more compact and manageable way. It allows for easier visualization and comparison of data that spans several orders of magnitude, making it useful in various scientific fields such as astronomy and biology.

3. How do you solve a logarithmic scale problem?

To solve a logarithmic scale problem, you need to first identify the base of the logarithm and the given values of the problem. Then, use the properties of logarithms to simplify the equation and solve for the unknown variable. Finally, check your answer by plugging it back into the original equation.

4. What are some common applications of logarithmic scale?

Logarithmic scales are commonly used in various scientific fields such as economics, biology, and chemistry. They are also used in engineering for measuring sound intensity, earthquake magnitude, and electrical signals. In finance, logarithmic scales are used to track stock prices and market trends.

5. What are the benefits of using a logarithmic scale?

The benefits of using a logarithmic scale include the ability to represent large and small numbers on the same scale, making it easier to compare data. It also helps to visualize exponential growth or decay, which can be difficult to see on a linear scale. Additionally, logarithmic scales allow for easier data analysis and interpretation.

Similar threads

  • General Math
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • Classical Physics
Replies
7
Views
721
  • Engineering and Comp Sci Homework Help
Replies
0
Views
383
Replies
21
Views
3K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Replies
7
Views
754
  • General Math
Replies
15
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
9
Views
1K
Back
Top