Home>

Obtain the number of lines of data in the csv file that measured the data of the motion sensor as shown in the table below, and use that value to

Domain of x (0<= x<= 64), range of y (0<= Yn<= n -1)
Yn = Kn
I want to draw a straight line parallel to the x-axis of.

Here, n represents the serial number ({n | n = 0,1,2, ...., n-1) of the number of rows of the acquired data, and Kn is the serial number of the row number. The value is {Kn | 0<= Kn<= n-1}.

What we want to achieve this time is, assuming that this straight line represents a 64-bit floating point number, x is a domain, (0 to 1) is the sign part, and (1 to 11) is the exponent part. Finally, I want to change the color of the straight line with (11 to 63) as the mantissa.

I tried to plot a graph that was parallel to the x-axis and divided into three colors for each y value (number of rows) ... but it didn't work.
Error message
x and y must have same first dimension, but have shapes (2,) and (1,)

Corresponding source code
root_path = "# path to the folder containing the desired files"
file_path = pathlib.Path (root_path)
csv_file_path = list (file_path.glob ("* .csv"))
df_list = []
index_list = []
for d in csv_file_path:
    df = pd.read_csv (d, dtype = str, header = None)
    df_list.append (df)
    #Get the number of matrices
    index = len (df)
    index_list.append (index)
fig = plt.figure ()
# 0th value of list number
#y axis
y = np.arange (0, index_list [0], 1)
ax = fig.add_subplot (1,1,1, title = "64bit_graph", ylim = (index_list [0], 0), xlim = (0,64))
for s in y:
    plt.axhline (y = int (y [s]), xmin = 0.0, xmax = 0.016, color = "y")
    plt.axhline (y = int (y [s]), xmin = 0.016, xmax = 0.17, color = "g")
    plt.axhline (y = int (y [s]), xmin = 0.17, xmax = 1.0, color = "b")
plt.show ()
Example of read csv file
x y z
-0.0021822779404043102 -0.011354400497599271 -0.021566487675835376
-0.001787169969495153 0.0033697903852851594 -0.015884067258401774
0.006165254335445933 -0.005569805768012884 -0.006603589812139398
0.008625260373964556 0.002838294734545343 -0.02680668760096887
-0.005034634878809447 -0.006784316103084711 -0.0372773035594495
Ideal graph (handwritten)


An example of a straight line that is parallel to the x-axis and whose color changes for each domain when the number of acquired lines is n = 3.

What I tried

I also tried plt.axhline for horizontal graph display, but it didn't work.

Sorry for the unclean handwritten graphs, and also the math ...
I'm still a beginner and there are many things I don't understand, but thank you.

Supplementary information (FW/tool version, etc.)

macbook air 2013
python 3.8.0
matplotlib 3.3.2
numpy 1.19.4
pandas 1.1.4

  • Answer # 1

    #y axis
    y = np.arange (0, index_list [0], 1)
    ax = fig.add_subplot (1,1,1, title = "64bit_graph", ylim = (index_list [0], 0), xlim = (0,64))
    for s in y:
        #Draw a parallel straight line
        plt.axhline (y = int (y [s]), xmin = 0.0, xmax = 0.016, color = "y")
        plt.axhline (y = int (y [s]), xmin = 0.016, xmax = 0.17, color = "g")
        plt.axhline (y = int (y [s]), xmin = 0.17, xmax = 1.0, color = "b")
    Graph drawn


    A straight line parallel to the x-axis

    Summary

    I was able to draw parallel straight lines using matplotlib.pyplot.axhline.
    The corresponding source code has also been modified.