【问题标题】:How to plot using a text data column from an Excel file as x axis?如何使用 Excel 文件中的文本数据列作为 x 轴进行绘图?
【发布时间】:2013-12-06 22:25:35
【问题描述】:

我在 Matlab 中导入了一个 Excel 文件,该文件包含两列,一列是数值,另一列是文本值。前 5 行如下所示:

ABF-E      0.34

HJK-D      -0.54

GHKL-I      1.34

FPLO-5      2.3

KKJLL-T     0.98

我需要在 Y 轴上绘制数字列,在 X 轴上绘制文本列。我可以使用 xlsread 和 plot 轻松处理数字列,但我无法管理绘制文本列。我该怎么办?

我已经写了下面的代码,但是我不知道x轴要做什么:

filename = 'MyData.xlsx';

Sheet = 2

xlRange = 'B1:B60';

Yaxis = xlsread (filename,Sheet,xlRange);

Xaxis = ????????;

plot(xAxis,Yaxis)

如果有人可以帮助我,我将非常感激。

【问题讨论】:

    标签: excel matlab text plot


    【解决方案1】:

    使用您的xAxis 文本设置轴的xticklabel property。你可以沿着这些路线走:

    [~,xAxis] = xlsread(filename,Sheet,'A1:A60'); %// read in text data. Use second output 
    yAxis = xlsread(filename,Sheet,'B1:B60'); %// read in numeric data
    plot(yAxis) %// This uses 1, 2, 3... as x axis values
    set(gca,'xtick',1:numel(xAxis)) %// set ticks
    set(gca,'xticklabel',xAxis)  %// set ticklabels with your xAxis
    xlim([0 numel(xAxis)+1]) %// adjust x axis span
    

    在您的示例中,这会产生:

    如果您想跳过 x 轴上的一些标签以减少混乱:定义所需的 tickStep,然后在设置刻度和刻度标签时使用它(在两个 set 行中):

    tickStep = 6; 
    [~,xAxis] = xlsread(filename,Sheet,'A1:A60'); %// read in text data. Use second output 
    yAxis = xlsread(filename,Sheet,'B1:B60'); %// read in numeric data
    plot(yAxis) %// This uses 1, 2, 3... as x axis values
    set(gca,'xtick',1:tickStep:numel(xAxis)) %// set ticks
    set(gca,'xticklabel',xAxis(1:tickStep:numel(xAxis)))  %// set ticklabels with your xAxis
    xlim([0 numel(xAxis)+1]) %// adjust x axis span
    

    【讨论】:

    • 你好 Luis,非常感谢你的回答,我会尝试一下,我会尽快回复你。
    • @Sarah 请注意,如果要绘制 60 个值,则 x 轴上的名称可能会过于压缩。在这种情况下,您可能希望跳过一些值
    • 是的,你是对的,它看起来非常聚集,但我不能减少值的数量,因为它会降低图形的准确性。有没有办法跳过标签而不是值?,我的意思是,例如,每 6 个值和一个垂直位置绘制一个标签,以便它们占用更少的空间?我一直在读到可以使用 Xticklabel_rotate,但是我必须做一个 for 循环来每 6 个值绘制一个标签?目前我的 Matlab 知识还很低。
    • @Sarah 可以跳过一些标签而不减少值的数量;请参阅更新答案的末尾(不需要for 循环)。至于垂直位置,我真的不知道,我从来没有这样做过
    • 你好 Luis,我已经尝试过你的代码,它工作得很好。我仍在将标签旋转到垂直位置,一旦找到方法,我将在此处发布代码。有些事情让我有点困惑。您在代码中的哪个位置要求绘制 y 轴的值?在 plot(yAxis) 命令中?但那是为了使用数值绘制 x 轴?,我很困惑:-(。跨度>
    【解决方案2】:

    在这里,我发布了回答我在这篇文章中的所有问题的代码。我已经粘贴了图表前 5 行 Excel 的代码。我用来旋转x轴标签的函数可以在这里下载:http://www.mathworks.co.uk/matlabcentral/fileexchange/3486-xticklabelrotate

    filename='MyData.xlsx'; %// Name of the file
    tickStep=1; %// Sets the tick steps
    Sheet=2; %// Excel sheet number
    [~,xAxis]=xlsread(filename,Sheet,'A1:A5'); %//Reads text data from Excel file. Uses   second output
    yAxis=xlsread(filename,Sheet,'B1:B5'); %// Reads numeric data from Excel file.
    plot(1:numel(xAxis),yAxis) %// Plots x and y-Axis in numeric format
    set(gca,'xtick',1:tickStep:numel(xAxis)) %// Sets the ticks of the x-Axis
    set(gca,'xticklabel',xAxis(1:tickStep:numel(xAxis))); %// Plots the text data previously read from the Excel file as x-Axis labels
    xlim([0 numel(xAxis)+1]) %//Adjusts x-Axis span
    xticklabel_rotate([],90,[],'Fontsize',10) %// Calls and executes the function that rotates the x-Axis labels into a vertical position with a font size of 10
    

    【讨论】:

    • +1 用于在弄清楚一切后分享解决方案。 -- 旁注,这看起来像是@Luis 答案的扩展,很高兴提及这些事情以防止误解。
    • 我认为很明显我的代码是@Luis 答案的扩展。我只是想一次收集这篇文章中讨论的所有答案和代码,因为其他有相同问题的人可能更容易理解它。下次我会指定的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多