简体   繁体   中英

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

I have 2 lists:

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?

I currently have something like this (note that it's 2 different lists from the above example so the values are a bit different)

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. You can use numpy's fancy indexing to pick the points from the x and y axes where "dots" is True. Note that it has to be True/False, so I used ==1 to convert your numbers to booleans.

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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