Pandas Pyplot多个标记,同一行

时间:2017-04-02 09:31:57

标签: python python-2.7 pandas matplotlib

我有一个df:

time    c_1    c_2   c_3
 t1      v1    NaN    t1
 t2      v2    NaN    NaN   
 t3      v3    t3     NaN 
 t4      v4    NaN    NaN 
 t5      v5    t5     NaN 
 t6      v6    NaN    t6

你好:

  1. 使用matplotlib.pyplot绘制一条线(t1,c_1)
  2. 并在特定样式(例如,绿色)
  3. 的行上标记(c_2)中的每个对应点
  4. 并且还将(c_3)中的每个点标记在另一个样式(例如,蓝色)的同一行上
  5. 然后画一条虚线连接标记(t1,t3),t3,t6)和(t5,t6)
  6. 我把问题分解为分,以便更容易阅读,但基本上我对pyplot来说是一个新手,而且我找不到用同一种语法设置2种不同标记的方法。如果可能的话,我也不太确定如何操纵标记。

    '通常'如何实现这个目标?

1 个答案:

答案 0 :(得分:1)

假设您拥有此数据框:

        c_1  c_2  c_3  time
0  0.548814  NaN  1.0     1
1  0.715189  NaN  NaN     2
2  0.602763  3.0  NaN     3
3  0.544883  NaN  NaN     4
4  0.423655  5.0  NaN     5
5  0.645894  NaN  6.0     6

如果以下情节是您之后的

enter image description here

可以使用以下代码生成:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)

time = np.arange(1,7)
c_1 = np.random.rand(6)
c_2 = time*np.array([np.nan, np.nan, 1, np.nan, 1, np.nan])
c_3 = time*np.array([1, np.nan, np.nan, np.nan, np.nan, 1])

df = pd.DataFrame({"time":time, "c_1":c_1,"c_2":c_2,"c_3":c_3 })

ax = df.plot("time", "c_1")
ax.plot(df["c_2"], df["c_1"], marker="s", color="limegreen",  linestyle="")
ax.plot(df["c_3"], df["c_1"], marker="o", color="crimson", linestyle="")

# to be able to draw a line with coordinates 
# from two different columns, we need to join them
df2 = df[["c_1","c_2"]].dropna()
df3 = df[["c_1","c_3"]].dropna().rename(columns = {'c_3':'c_2'}, inplace = False)
df4 = pd.concat([df2, df3]).sort_values(by=["c_2"])

ax.plot(df4["c_2"], df4["c_1"], color="burlywood",  linestyle=":", lw=2.5)

ax.set_xlim(0,7)
plt.show()