macaroni
macaroni

Reputation: 151

Python: Problem with plotting from one point in Y to many points on X axis

I have one point on Y axis and many on X axis and i want to do straight lines from this one Y point to each X (shape of triangle).

plt.plot([P], [TR], 'k')
plt.xlabel('CENTERLINE')
plt.ylabel('RADIUS')
plt.show()

Where TR is my single point declared earlier and P are float points (first i create P = np.zeros((n+1)) and after that using for loop i put there values).

plt.show() returns empty chart (without any plot)

Here is the example how it should look like

Upvotes: 0

Views: 586

Answers (2)

user8408080
user8408080

Reputation: 2468

When handling many lines at once it may be a good idea to use a LineCollection object:

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

import numpy as np

pos_fixed = np.array([0, 35])
lines = np.array([[[pos, 0], pos_fixed] for pos in np.arange(0, 50, 2)])
line_coll = LineCollection(lines)

fig, ax = plt.subplots()
ax.add_collection(line_coll)

plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

This way you can handle your plot more easily (and it's also faster if you have many lines).

Result:

enter image description here

Upvotes: 2

BlivetWidget
BlivetWidget

Reputation: 11063

Those lines are not a contiguous plot, so you'll need to plot them separately. You could make it a single line that goes up and down (and up and down and up and down...) but I think this makes more sense as separate lines. Consider:

import matplotlib.pyplot as plt
originpoint = (0, 5)
yfloor = 0
xvalues = [0, 1, 2, 3, 4]
for x in xvalues:
    plt.plot((originpoint[0], x), (originpoint[1], yfloor))
plt.show()

the plot

Upvotes: 1

Related Questions