简体   繁体   English

Python - Pandas - 绘制没有标题的数据框

[英]Python - Pandas - Plot Data frame without headers

I am trying to use the .plot() function in pandas to plot data into a line graph.我正在尝试使用 pandas 中的 .plot() 函数将数据绘制成折线图。

The data sorted by date with 48 rows after each date.数据按日期排序,每个日期后有 48 行。 Sample below:下面的示例:

                       1         2  ...        46        47        48
18  2018-02-19  1.317956  1.192840  ...  1.959250  1.782985  1.418093
19  2018-02-20  1.356267  1.192248  ...  2.123432  1.760629  1.569340
20  2018-02-21  1.417181  1.288694  ...  2.086715  1.823581  1.612062
21  2018-02-22  1.431536  1.279514  ...  2.201972  1.878109  1.694159

etc until row 346.等到第 346 行。

I tried the below but .plot does not seem to take positional arguments:我尝试了以下方法,但 .plot 似乎没有采用位置参数:

df.plot(x=df.iloc[0:346,0],y=[0:346,1:49]

How would I go about plotting my rows by date (the 1st column) on a line graph and can I expand this to include multiple axis?我将如何在折线图上按日期(第一列)绘制我的行,我可以扩展它以包含多个轴吗?

There are multiple ways to do this, some of which are directly through the pandas dataframe.有多种方法可以做到这一点,其中一些直接通过 pandas 数据框。 However, given your sample plotting line, I think the easiest might be to just use matplotlib directly:但是,鉴于您的示例绘图线,我认为最简单的可能是直接使用 matplotlib:

import matplotlib.pyplot as plt
plt.plot(df.iloc[0:346,0],df.iloc[0:346,1:49])

For multiple axes you can add a few lines to make subplots.对于多个轴,您可以添加几条线来制作子图。 For example:例如:

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = plt.subplot(1,2,1)
plt.plot(df.iloc[0:346,0],df.iloc[0:346,1:10],ax=ax1)
ax2 = plt.subplot(1,2,2)
plt.plot(df.iloc[0:346,0],df.iloc[0:346,20:30],ax=ax2)

You can also do this using the pandas plot() function that you were trying to use - it also takes an ax argument the same way as above, where you can provide the axis on which to plot.您也可以使用您尝试使用的 pandas plot()函数来执行此操作 - 它还采用与上述相同的方式使用ax参数,您可以在其中提供要绘制的轴。 If you want to stick to pandas, I think you'd be best off setting the index to be a datetime index (see this link as an example: https://stackoverflow.com/a/27051371/12133280 ) and then using df.plot('column_name',ax=ax1) .如果您想坚持使用熊猫,我认为您最好将索引设置为日期时间索引(请参阅此链接作为示例: https ://stackoverflow.com/a/27051371/12133280)然后使用df.plot('column_name',ax=ax1) The x axis will be the index, which you would have set to be the date. x 轴将是索引,您将其设置为日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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