简体   繁体   English

如何使用 matplotlib 中的 2 个列表在 plot 上绘制一条线和点?

[英]How to plot a line and dots on it using 2 lists in matplotlib?

I have 2 lists:我有 2 个列表:

losses = [12,13,15,10,9,8]
dots = [1,0,0,1,0,0]

How can I plot the main line from the losses list, and wherever there's a 1 in the dots list have a dot on that line?我怎样才能 plot losses列表中的主线,并且在dots列表中有1的地方在那条线上有一个点?

I currently have something like this (note that it's 2 different lists from the above example so the values are a bit different)我目前有这样的东西(请注意,它与上面的示例有 2 个不同的列表,因此值有点不同)

plt.plot(losses, c = 'b')
plt.plot(dots, c='r');

But I'm not sure how to make only dots on the main line但我不确定如何在主线上只画点

在此处输入图像描述

what about using a suitable marker ?使用合适的标记怎么样?

from matplotlib import pyplot as plt

losses = [12,13,15,10,9,8]
dots = [1,0,0,1,0,0]

# value from losses whenever corresponding dots value is 1:
d = [v if d else None for d, v in zip(dots, losses)]

plt.plot(losses, c='b')
plt.plot(d, c='b', marker='|', markersize=40)

在此处输入图像描述

Your diagram doesn't match your description, but here's how to do what you described.您的图表与您的描述不符,但这是您描述的操作方法。 You do the points as a scatter plot.您将这些点作为散点 plot。 You can use numpy's fancy indexing to pick the points from the x and y axes where "dots" is True.您可以使用 numpy 的精美索引从 x 和 y 轴中选择“dots”为 True 的点。 Note that it has to be True/False, so I used ==1 to convert your numbers to booleans.请注意,它必须是 True/False,所以我使用==1将您的数字转换为布尔值。

import matplotlib.pyplot as plt
import numpy as np

losses = np.array([12,13,15,10,9,8])
dots = np.array([1,0,0,1,0,0])==1
x = np.arange(6)*50000

xscat = x[dots]
yscat = losses[dots]

plt.plot(x, losses, c = 'b')
plt.scatter(xscat, yscat, c='r');
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM