python - Python:从 x 轴上的点 (X1,0) 到点 (X2,Y2) 的绘图问题
问题描述
我在 X 轴上有点,我想从每个点到另一个点(X2,Y2)做直线。
此代码适用于从 Y=35 到 X 点 ( P
) 的蓝线,它可以工作
pos_fixed = np.array([0, TR])
lines = np.array([[[pos, 0], pos_fixed] for pos in P])
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()])
plt.xlabel('Oś')
plt.ylabel('Promień')
plt.show()
这里是应该做我在开头描述的代码:
for s in range(len(P)-1):
position = np.array([P[s], 0])
lines = np.array([[position, [x2[s], y2[s]]]])
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()])
plt.show()
我的期望在附图上(我有红色和紫色的点,我不知道如何做绿线)。
这段代码(第二个)显示了几十个图表(每条绿线分别),不包括(我希望)之前的代码/之前的图表。
解决方案
您正在每个循环上创建一个图形。您可以先设置您的图形,然后在for
循环中添加线条。像这样重新排列您的代码:
# create the figure
fig, ax = plt.subplots()
plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])
# lines from the first part of the question
pos_fixed = np.array([0, TR])
lines = np.array([[[pos, 0], pos_fixed] for pos in P])
line_coll = LineCollection(lines,colors='blue')
ax.add_collection(line_coll)
plt.xlabel('Oś')
plt.ylabel('Promień')
# lines for the second part
for s in range(len(P)-1):
position = np.array([P[s], 0])
lines = np.array([[position, [x2[s], y2[s]]]])
line_coll = LineCollection(lines,colors='green')
ax.add_collection(line_coll)
plt.show()
推荐阅读
- r - 有没有办法使用 tidyverse R 通过升序更改多个列名?
- python - 对于 python 数据帧中的循环替换
- bluetooth-lowenergy - 关于低功耗蓝牙连接事件的问题
- sql - Postgres 按相似日期将数据分组
- c# - 如何将 YieldAwaitable 转换为任务
- php - 此路由不支持 GET 方法。支持:POST
- python - 用漂亮的汤刮一张 WP 桌子
- optimization - 如何知道 Apache Flink 中可以链接哪些算子
- node.js - 我面临使用 Highcharts Node.js 导出服务器生成空白图像的问题
- dart - 如何从 Dart 中的映射中过滤空值