Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:14:11+00:00 2026-06-10T00:14:11+00:00

I’m having problem with plotting a graph using data of a .dat file. Can

  • 0

I’m having problem with plotting a graph using data of a .dat file. Can someone help me out here?

The file is saved as /My Documents/filename.dat and has three columns of numbers representing variables X, Y and Z.

(X is from 45 to 90 with variation of .5
Z is from .4 to .95 with variation of .05
Y is the result of these 2 variables.)

Example: (a part of data in .dat file)

48.000000 -0.000010 0.400000
48.500000 -0.000004 0.400000
49.000000 0.000003 0.400000
49.500000 0.000010 0.400000
50.000000 0.000016 0.400000

I want to plot a 2d graph of X and Y, and color should change according to the Z variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:14:13+00:00Added an answer on June 10, 2026 at 12:14 am

    Use textscan to read your data:

    fid = fopen('/My Documents/filename.dat');
    data = textscan(fid, '%f %f %f');
    fclose(fid);
    X=data{1};
    Y=data{2};
    Z=data{3};
    

    Plot method #1

    Then if you plot with

    plot(X,Y)
    

    you’ll get an ordinary plot consisting of lines. However these all have the same color. Varying them is not possible unless you split them up in separate lines.

    Luckily, there is also the scatter function which allows you to do this:

    scatter(X,Y,[],Z)
    

    This plots the points, with color based on Z.

    Plot method #2

    If you want the points to be connected with lines also having varying color, you’ll have to plot them as separate lines, and providing color to each line separately:

    plot([X(1:end-1)' ; X(2:end)'], [Y(1:end-1)' ; Y(2:end)']);
    

    The lines now have default coloring, it becomes a bit a hussle to get the right colors in however, next up is an example. Unfortunately I don’t now of any way to input the colors also in such a one-liner, so we’ll have to loop.

    Ncolors=10;
    zmin=min(Z);zmax=max(Z);
    dz=max((zmax-zmin)/Ncolors,eps);
    clr_map=jet(Ncolors);
    clr_ids=min(floor((Z(1:end-1)-zmin)/dz)+1,Ncolors);
    
    figure;hold on;
    for ii=1:numel(X)-1
        plot([X(ii) X(ii+1)], [Y(ii) Y(ii+1)],'color',clr_map(clr_ids(ii),:))
    end
    

    All lines now have colors based on one of their end points.

    To add a colorbar, use colorbar, weird huh? But of course, the labels of that bar are referring to the colororder. Luckily, we can change them:

    colormap(clr_map);
    h_cb=colorbar;
    set(h_cb,'yticklabel',arrayfun(@num2str,linspace(zmin,zmax,numel(get(h_cb,'ytick'))),'uni',false));
    

    Change Ncolors to use more/less resolution in coloring the lines.
    Probably overkill: you can also change the number of labels on the colorbar, the following changes it to 10:

    colormap(clr_map);
    h_cb=colorbar;
    set(h_cb,'ytick',linspace(1,Ncolors,10));
    set(h_cb,'yticklabel',arrayfun(@num2str,linspace(zmin,zmax,10),'uni',false));
    

    or now with the labels having only 2 decimals:

    set(h_cb,'yticklabel',arrayfun(@(yi) sprintf('%.2g',yi),linspace(zmin,zmax,10),'uni',false));
    

    Plot method #3

    Another last method is to use patches (which are slower), which is explained for the 3d case here, so you can get started there if you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.