我认为您需要更好地了解彼此之间的交互方式pandas
和交互方式,而不是编制索引。matplotlib
让我们为您的案例逐步进行:
正如pandas.DataFrame.plot文档所说,绘制的系列是一列。您在行中有系列,因此您需要转置您的数据框。
要创建散点图,您需要不同列中的 x 和 y 坐标,但您缺少 x 列,因此您还需要在转置数据框中创建一个包含 x 值的列。
显然pandas
默认情况下不会通过连续调用plot
( matplotlib
do it) 来改变颜色,因此您需要选择一个颜色图并传递一个颜色参数,否则所有点都将具有相同的颜色。
这是一个工作示例:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Here I copied you data in a data.txt text file and import it in pandas as a csv.
#You may have a different way to get your data.
df = pd.read_csv('data.txt', sep='\s+', engine='python')
#I assume to have a column named 'time' which is set as the index, as you show in your post.
df.set_index('time')
tdf = df.transpose() #transpose the dataframe
#Drop the time column from the trasponsed dataframe. time is not a data to be plotted.
tdf = tdf.drop('time')
#Creating x values, I go for 1 to 5 but they can be different.
tdf['xval'] = np.arange(1, len(tdf)+1)
#Choose a colormap and making a list of colors to be used.
colormap = plt.cm.rainbow
colors = [colormap(i) for i in np.linspace(0, 1, len(tdf))]
#Make an empty plot, the columns will be added to the axes in the loop.
fig, axes = plt.subplots(1, 1)
for i, cl in enumerate([datacol for datacol in tdf.columns if datacol != 'xval']):
tdf.plot(x='xval', y=cl, kind="scatter", ax=axes, color=colors[i])
plt.show()
这绘制了以下图像:
这是关于在 matplotlib 中选择颜色的教程。