0

Is there any possible way to plot a encoding graph using matplotlib in python? Suppose I do have a Data String of "01001100011". I want to plot a encoding plot like the picture using matplotlib in python.enter image description here

1 Answer 1

2

You could do something like this.

import numpy as np
import matplotlib.pyplot as plt


s = "01001100011" # string to be encoded

x_max = len(s)+1

# generate waveform encoding scheme
zero_form = np.array([[0,1],[0.5,1],[0.5,0],[1,0]])
one_form = zero_form.copy()
one_form[:,1] = 1 - zero_form[:,1]
encoding = [zero_form,one_form]

# encode the string s
waveform = np.vstack([x + np.array([i,0]) for i,c in enumerate(s) 
                                          for x in encoding[int(c)]])

plt.figure(figsize = (15,3))

# plot grid
for y in [0,.5,1]:
    plt.plot([0,x_max],[y,y],'k--')
for x in range(1,x_max):
    plt.plot([x,x],[0,1.5],'k--')

# plot 0,1 labels
for i,c in enumerate(s):
    plt.text(i+.3,1.2,c, fontsize = 30)

plt.plot(*waveform.T,'r', lw = 10, alpha = .5) # plot waveform
plt.axis("off")                               # turn of axes/labels
plt.show()

The resulting figure:

enter image description here

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

1 Comment

@Damien If my answer was useful to you, I would appreciate it if you could "accept" it by clicking the checkmark (✓) beneath the vote arrows on my answer.

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.