1

When I try to plot a graph with matplotlib, I'm unable to plot the numpy array properly but the numpy matrix works, despite the data is the same.

The file is a .dat file. And part of it is like this. I'm sorry that some data with exponential isn't properly displayed:

   13.589515189014234       0.15604292500000005     
   13.735639008250946       0.15257472499999994     
   13.881762827487661       0.14755997499999998     
   14.027886646724372       0.14034605000000008     
   14.174010465961084       0.13002836249999997     
   14.320134285197797       0.11536830749999996     
   14.466258104434509       0.09470843482500003    
   15.343001019854782      -0.29482481522500004     
   15.489124839091494      -0.41008690550000004     
   15.635248658328205      -0.50755824292125018     
   15.781372477564917      -0.55349056577499989     
   15.927496296801632      -0.52813589407000028     
   16.073620116038342      -0.44291128949999964     
   16.219743935275055      -0.33041039448500015     
   16.365867754511768      -0.22063749432500010     
   16.511991573748478      -0.12956561664999997
   16.658115392985192      -6.2520931050000061E-002
   16.804239212221901      -2.0474291900000007E-002
   16.950363031458615      -3.3802861749999864E-003
   17.096486850695324      -1.1236421675000005E-002
   17.242610669932041      -4.4293935437500001E-002
   17.388734489168755      -0.10291197632249992     
   17.534858308405465      -0.18681497748249989     
   17.680982127642178      -0.29264704324999974     
   17.827105946878888      -0.40844271439000052     
   17.973229766115601      -0.50809938749999994     
   18.119353585352311      -0.55706159005000022     
   18.265477404589028      -0.53426514450000007     
   18.411601223825738      -0.44999058192999997     
   18.557725043062451      -0.33683265428499992     
   18.703848862299161      -0.22573322450000011     
   18.849972681535874      -0.13362568075000000     
   18.996096500772584      -6.6342932249999972E-002
   19.142220320009297      -2.4975045235000021E-002
   19.288344139246014      -9.3829022500000001E-003
   19.434467958482724      -1.9370208699999975E-002
   19.580591777719437      -5.4959120624999996E-002
   19.726715596956147      -0.11625987367499999     
   19.872839416192861      -0.20263722292500017     
   20.018963235429570      -0.30991265800000006     
   20.165087054666284      -0.42447163175000008     
   20.311210873902997      -0.51843669877499976     
   20.457334693139710      -0.55769086224999975     
   20.603458512376420      -0.52495092840000057     
   20.749582331613134      -0.43469907825000020     
   20.895706150849843      -0.32059742052500018     
   21.041829970086557      -0.21181538367500022     
   21.187953789323267      -0.12320815802500000     
   21.334077608559983      -5.9446244649999966E-002
   21.480201427796697      -2.1174214224999988E-002
   21.626325247033407      -8.1263172499999904E-003
   21.772449066270120      -2.0123068274999968E-002
   21.918572885506830      -5.7280081410249997E-002
   22.064696704743543      -0.11983124174999996     

with numpy array:

    data = []
    for line in open('profile_3.dat'):
        new = line.split()
        data.append(new)
    profile = np.array(data)

    plt.figure()
    
    x = profile[:,0]
    y = profile[:,1]
    
    plt.plot(x,y)
    plt.show()

plot with numpy array

with numpy matrix:

    data = []
    for line in open('profile_3.dat'):
        new = line.split()
        data.append(new)
    profile = np.matrix(data)
    
    plt.figure()
        
    x = profile[:,0]
    y = profile[:,1]
        
    plt.plot(x,y)
    plt.show()

plot with numpy matrix

It seems that when plot with numpy array, the graph is inverted and some data is misplaced.

3
  • 3
    In complément to the existing answer, numpy.matrix is deprecated, so you should stick with array. Commented Aug 8, 2021 at 13:24
  • just a simple question, what is the difference between a numpy matrix and a numpy array. I am a regular MATLAB user. So the data type in numpy is a bit confusing to me. Commented Aug 8, 2021 at 21:38
  • numpy matrix is a special case of array that has exactly 2D. It has some special methods to make it easier to work with 2D arrays (e.g. A matrix is a specialized 2-D array that retains its 2-D nature through operations. It has certain special operators, such as * (matrix multiplication) and ** (matrix power) cf. matrix documentation). From the docs: It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future. Commented Aug 9, 2021 at 2:28

1 Answer 1

1

Seems that problem is in strings vs numbers:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

data = []
for line in open('profile_3.dat'):
    new = line.split()
    data.append(new)
profile = np.array(data)

plt.figure()

x = profile[:,0].astype(np.float)
y = profile[:,1].astype(np.float)

plt.plot(x,y)
plt.show()

Plot for your data: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I thought it will automatically be float number when converting it to a numpy array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.