skip to Main Content

I would like to make a plot with matplotlib in python with rectangle lines likes this one:

Rectangle line plot example

Sadly I have no idea how to call this kind of plot (I photoshoped this one together, it’s kind of like a bar plot but rotated by 90 degrees), therefore my google search is a lost case. My dataset is like this:

   y   x
  1.0 1.0
  0.6 2.3
  0.3 3.3
  0.1 1.4
 -0.4 2.1
 -0.6 3.8
 -0.8 0.9
 -1.0

I already put x and y into two lists of variables. Basically the x value should be drawn in between the current and the next y value (compare the picture and the values in the example).

3

Answers


  1. You should try matplolib.path, Try the following code

    import matplotlib.path as mpath
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    
    Path = mpath.Path
    path_data = [
        (Path.MOVETO, (1.58, -2.57)),
        (Path.CURVE4, (0.35, -1.1)),
        (Path.CURVE4, (-1.75, 2.0)),
        (Path.CURVE4, (0.375, 2.0)),
        (Path.LINETO, (0.85, 1.15)),
        (Path.CURVE4, (2.2, 3.2)),
        (Path.CURVE4, (3, 0.05)),
        (Path.CURVE4, (2.0, -0.5)),
        (Path.CLOSEPOLY, (1.58, -2.57)),
        ]
    
    codes, verts = zip(*path_data)
    path = mpath.Path(verts, codes)
    
    # plot control points and connecting lines
    x, y = zip(*path.vertices)
    line, = ax.plot(x, y, 'go-')
    
    ax.grid()
    ax.axis('equal')
    plt.show()
    

    You will get a basic understanding of how path works and then modify your vertices to plot your own graph. For more info, please refer to the following link:
    https://matplotlib.org/api/path_api.html#module-matplotlib.path

    Login or Signup to reply.
  2. If you want to use plt.plot, you could also add extra vertices. So instead of:

    x = [1.0, 2.3, 3.3, 1.4, 2.1, 3.8, 0.9]
    y = [1.0, 0.6, 0.3, 0.1, -0.4, -0.6, -0.8]
    

    Do

    new_x = [1.0, 1.0, 2.3, 2.3, 3.3, 3.3, 1.4, 1.4, 2.1, 2.1, 3.8, 3.8, 0.9]
    new_y = [1.0, 0.6, 0.6, 0.3, 0.3, 0.1, 0.1, -0.4, -0.4, -0.6, -0.6, -0.8, -0.8]
    

    giving you:

    import matplotlib.pyplot as plt
    plt.plot(new_x, new_y)
    

    Plot

    Of course, you might notice that we’re just repeating the values of x twice (with the last value removed) and y twice (with the first value removed), so an easy way to convert x to new_x and y to new_y with pure Python is:

    new_x = [a for a in x for _ in range(2)][:-1]
    new_y = [a for a in y for _ in range(2)][1:]
    

    It would be even cleaner using itertools or np.repeat.

    Login or Signup to reply.
  3. You can use the "steps-" linestyle for plotting, ("steps-pre", "steps-post", "steps-mid"). Those determine whether the coordinates are meant to denote the beginning, the middle and the end of the line.

    step = plt.plot(y,x, linestyle="steps-pre")
    

    Some complete example:

    u = u"""y   x
      1.0 1.0
      0.6 2.3
      0.3 3.3
      0.1 1.4
     -0.4 2.1
     -0.6 3.8
     -0.8 0.9
     -1.0 0"""
    
    import io
    import matplotlib.pyplot as plt
    import numpy as np
    
    y,x = np.genfromtxt(io.StringIO(u),skip_header=1, unpack=True)
    
    step = plt.plot(x,y, linestyle="steps-pre")
    
    plt.show()
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search