首页 > 解决方案 > Python - 基于其他列中的文本绘图

问题描述

我在 DataFrame df1 中有如下数据

time       duration status
10:45:00   12       Ok
11:45:00   22       Ok
12:45:00   10       Failed
13:45:00   42       Ok
14:45:00   32       Failed

使用 Matplotlib,我可以使用绘制前两列的图表

df1.plot(y='duration',figsize=(20,10))
plt.show()

现在,当状态为“失败”时,我想在红十字 (X) 中显示该点,而当状态为“正常”时则不显示任何内容

这可以使用matplotlib来实现吗?

标签: python-3.xmatplotlib

解决方案


这是一种方法

fig, ax = plt.subplots()
df1.plot(y='duration',figsize=(8,5), ax=ax)
df1[df1["status"]=="Failed"].plot(marker='x', linestyle='None', color='r', legend=False, markersize=20, ax=ax)

这是另一种方式

fig, ax = plt.subplots()

df1.plot(y='duration',figsize=(8,5), ax=ax)
xvals = np.where(df1["status"]=="Failed")[0] 

ax.scatter(xvals, df1[df1["status"]=="Failed"]["duration"].values, marker='x', s=100, c='r') 
plt.show()

在此处输入图像描述


推荐阅读