如何在python中使用

如何在python中使用,python,matplotlib,plot,line-plot,Python,Matplotlib,Plot,Line Plot,这对外面的人来说应该是一个非常简单的问题。我需要python中的一个线条图,其中自变量x轴是日期。y轴是从属值数据,将有多行:每个名称一行,描述值随时间的变化。除了使用matplotlib,我不知道如何执行此操作 这就是我在df中组织数据的方式,它从csv文件中提取数据 Name = df['Name'] Value = df['expected harvest (Value)'] Date = df['Date'] result = pd.concat([Name, Value, Date],

这对外面的人来说应该是一个非常简单的问题。我需要python中的一个线条图,其中自变量x轴是日期。y轴是从属值数据,将有多行:每个名称一行,描述值随时间的变化。除了使用matplotlib,我不知道如何执行此操作

这就是我在df中组织数据的方式,它从csv文件中提取数据

Name = df['Name']
Value = df['expected harvest (Value)']
Date = df['Date']
result = pd.concat([Name, Value, Date], axis=1)

>>> result
                       Name                     Value      Date
1                       189                       9.0  11/14/15
2                       191                      10.0  11/14/15
3                       192                       1.0  11/14/15
4                       193                       4.0  11/14/15
...                     ...                       ...       ...
2948                    189                       7.0   2/20/16
2950                    190                       1.0   2/20/16
2952                    191                       3.0   2/20/16
2953                    192                       3.0   2/20/16
2954                    193                       0.0   2/20/16
到目前为止,我已经尝试过这个方法,但是我需要将行设置为水平而不是垂直,并且每个名称都有单独的行。不知何故,我不知道如何按名称对数据进行分组,然后将其绘制为单独的线条

fig = plt.figure()
ax = fig.add_subplot(111)
x_points = df['Date']
x_points = pd.to_datetime(x_points)
y_points = df['expected harvest (Value)']

p = ax.plot(x_points, y_points, 'b')
ax.set_xlabel('x-points')
ax.set_ylabel('y-points')
ax.set_title('Simple XY point plot')
fig.show()

首先,我们创建样本数据

sample_dates = ['1/1/15', '1/2/15', '1/3/15', '1/4/15']
dates = []
for date in sample_dates:
    [dates.append(date) for _ in range(4)]
values = np.random.randint(1, 8, size=15)

names = [1, 2, 3, 4] * 4

我们在中间删除了一些,如在示例数据中,190不是。我们将其转换为数据帧:

names.pop(6)
dates.pop(6)

x_date = pd.to_datetime(dates)
df = pd.DataFrame()
df['names'] = names
df['values'] = values
df['dates'] = x_date
现在我们按名字走,然后画出来

for i in df.names.unique():
    x = df[df.names==i]['dates']
    y = df[df.names==i]['values']
    plt.plot(x, y)
plt.show()

这听起来不是一个问题,而是一项需要做的工作。你试过什么?我会加上我试过的。它没有得到任何接近我想要的东西。你的数据帧不可复制。你可以更具体地说:但这不是我需要的情节。你在同一天有几点,我觉得情节不是很清楚。你不需要每天添加它们吗?@vaishaligarg但数据框架非常简单,应该有人能够提供指导,不是吗?