Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to square off my plot

I'm trying to draw a plot of a list of values in Python:

import matplotlib.pyplot as plt
import control
import numpy

array = [0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1]

plt.plot(array)

plt.xlabel('Time')
plt.ylabel('ALE')

plt.show()

Currently, I get this plot: enter image description here

How can I make the transitions square-wave edges (i.e. vertical lines) instead of inclined lines?

like image 763
Jakub Nowacki Avatar asked Jan 24 '26 09:01

Jakub Nowacki


2 Answers

You may use a step drawstyle, e.g.

plt.plot(array, drawstyle="steps-mid")

enter image description here

Depending on how time steps are defined here, you may also use "steps-pre" or "steps-post" and if your timesteps are not equidistant you still need to supply some x values to the plot.

like image 94
ImportanceOfBeingErnest Avatar answered Jan 25 '26 22:01

ImportanceOfBeingErnest


I would recommend using plt.step() to make lines vertical

import matplotlib.pyplot as plt

ys = [0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1]
xs = range(array.size)

plt.step(xs, ys)
plt.show()

enter image description here

like image 34
Max Collier Avatar answered Jan 25 '26 23:01

Max Collier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!