Reputation: 7952
When I plot my data with linear Y-axis (not logarithmic), chart is good, as below:
But when I use logarithmic scale for Y-axis, my chart looks upside-down:
chart1.ChartAreas[0].CursorY.IsUserEnabled = true;
chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = true;
chart1.ChartAreas[0].AxisY.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].CursorY.Interval = 1e-10;//zoom resolution threshold
/////////////////when I added following line, chart becomes upside-down:
chart1.ChartAreas[0].AxisY.IsLogarithmic = true;
using (StreamReader reader = new StreamReader(
"Frequencies(Hz)_and_corresponding_SingularValues.txt"
))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] tokens = line.Split(new char[] { '\t' });
x = Convert.ToDouble(tokens[0]);
y = Convert.ToDouble(tokens[1]);
chart1.Series["Series1"].Points.AddXY(x, y);
}
}
Upside-down chart is shown below, does anybody know why it gets flipped:
Another question: when plotting with logarithmic scale, what is the best way to avoid exceptions due to zero/negative data without modifying original data?
EDIT: I thought maybe color of background and color of chart series are switched, therefore I added following line to test it, but chart is still upside-down:
chart1.Series["Series1"].Color = Color.Blue;
Upvotes: 0
Views: 5218
Reputation: 7952
As mentioned in comments, the reason was math: if y<1
then Ln(y)<0
. Eventually, I solved the problem with changing ChartType
as below:
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
Also, to avoid any future possible exception when having zero/negative data points in logarithmic scale, I modified code like this:
using (StreamReader reader = new StreamReader(
"Frequencies(Hz)_and_corresponding_SingularValues.txt"
))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] tokens = line.Split(new char[] { '\t' });
x = Convert.ToDouble(tokens[0]);
y = Convert.ToDouble(tokens[1]);
/////////////////to skip zero/negative data points,
/////////////////to avoid exceptions in logarithmic scale:
/////////////////singular values look like to be positive, but we add this just in case:
//chart1.Series["Series1"].Points.AddXY(x, y);
if(y>0){
chart1.Series["Series1"].Points.AddXY(x,y);
}
}
}
Now chart looks good:
Upvotes: 2