1

For example, I have a few lines with similar coordinates:

import matplotlib.pyplot as plt

x1 = [-1, 0, 1, 1, 1, 0, -1, -1, 0, 0, 1]
x2 = x1[:]

plt.pyplot(x1, color='red')
plt.pyplot(x2, color='green')
plt.show()

Of course it will show graph with only one orange line combined colors. Is there any way (matplotlib function or some approach) to make a small shift of second line to get nice graph(lines close to each other)?

P.S. In my real problem I need to draw graph 10 lines with y-values(0, -1, 1) so the lines frequently overlapped. And I want to add some space between them.

Thanks in advance.

Igor Korolev
  • 101
  • 2
  • 10

1 Answers1

4

You may add a small amount to one of the lines, e.g. by using numpy arrays and adding some number, x2 = x1 + 0.1.

import matplotlib.pyplot as plt
import numpy as np

x1 = np.array([-1, 0, 1, 1, 1, 0, -1, -1, 0, 0, 1])
x2 = x1 + 0.1

plt.plot(x1, color='red')
plt.plot(x2, color='green')
plt.show()

enter image description here

This solution is of course not ideal. In order to make the lines fit nicely against each other, you may therefore choose to use a solution similar to the one discussed in this question: In matplotlib, how can I plot a multi-colored line, like a rainbow

The result would then look more pleasant like

enter image description here

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712