Windows 形成图表 Y 轴:对数刻度:图表看起来像颠倒的
Windows forms chart Y axis: logarithmic scale: chart looks like flipped upside-down
当我用线性 Y 轴(不是对数)绘制数据时,图表很好,如下所示:
但是当我对 Y 轴使用对数刻度时,我的图表看起来是颠倒的:
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);
}
}
下面是颠倒的图表,有谁知道为什么会翻转:
另一个问题:在用对数刻度绘图时,在不修改原始数据的情况下,如何避免因zero/negative数据引起的异常的最佳方法是什么?
编辑:
我想也许背景颜色和图表系列的颜色调换了,所以我添加了以下行来测试它,但图表仍然是颠倒的:
chart1.Series["Series1"].Color = Color.Blue;
如评论中所述,原因是数学:如果 y<1
则 Ln(y)<0
。最终,我通过如下更改 ChartType
解决了问题:
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
此外,为了避免在对数刻度中有 zero/negative 个数据点时出现任何未来可能的异常,我修改了如下代码:
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);
}
}
}
现在图表看起来不错:
当我用线性 Y 轴(不是对数)绘制数据时,图表很好,如下所示:
但是当我对 Y 轴使用对数刻度时,我的图表看起来是颠倒的:
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);
}
}
下面是颠倒的图表,有谁知道为什么会翻转:
另一个问题:在用对数刻度绘图时,在不修改原始数据的情况下,如何避免因zero/negative数据引起的异常的最佳方法是什么?
编辑: 我想也许背景颜色和图表系列的颜色调换了,所以我添加了以下行来测试它,但图表仍然是颠倒的:
chart1.Series["Series1"].Color = Color.Blue;
如评论中所述,原因是数学:如果 y<1
则 Ln(y)<0
。最终,我通过如下更改 ChartType
解决了问题:
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
此外,为了避免在对数刻度中有 zero/negative 个数据点时出现任何未来可能的异常,我修改了如下代码:
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);
}
}
}
现在图表看起来不错: