从重叠打印matplotlib上的x轴删除的日期

2024-10-01 22:39:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用matplotlib和pandas来显示表示工作量的时间序列线。在

我已经将DF的toall覆盖在一个图中,但是当我这样做时,python似乎剥离了日期的x轴并输入了一些数字。(我不确定这些数据是从哪里来的,但可以猜测,并非所有的日子都包含相同的数据,因此python恢复使用索引id号)。如果我把其中的任何一个画出来,他们就会得出x轴上的日期。在

任何提示或解决方案,使x轴显示日期的多重绘图将不胜感激。在

这是带有时间轴的单图形图: single figure plot with time axis

我用的代码是

fig = pl.figure()
ax = fig.add_subplot(111)
ax.plot(b342,color='black')
ax.plot(b343,color='blue')
ax.plot(b344,color='red')
ax.plot(b345,color='green')
ax.plot(b346,color='pink')
ax.plot(fi,color='yellow')
plt.show()

这是带有奇怪x轴的多重绘图图: multiple plots without time axis


Tags: 数据绘图pandasdfplotmatplotlib时间fig
2条回答

我找到了一个我想要的答案,似乎plt.绘图并没有使用日期作为x轴,但是使用pandas文档调用它确实起到了作用。在

ax = b342.plot(label='342')
b343.plot(ax=ax, label='test')
b344.plot(ax=ax)
b345.plot(ax=ax)
b346.plot(ax=ax)
fi.plot(ax=ax)

enter image description here表演()

我想知道有没有人知道该怎么换标签?在

一种选择是根据DataFrame索引手动指定x轴,然后使用matplotlib直接绘图。在

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# make up some data
n = 100
dates = pd.date_range(start = "2015-01-01", periods = n, name = "yearDate")

dfs = []

for i in range(3):
    df = pd.DataFrame(data = np.random.random(n)*(i + 1), index = dates,
                      columns = ["FishEffort"] )
    df.df_name = str(i)
    dfs.append(df)

# plot it directly using matplotlib instead of through the DataFrame
fig = plt.figure()
ax = fig.add_subplot()

for df in dfs:
    plt.plot(df.index,df["FishEffort"], label = df.df_name)

plt.legend()
plt.show()

enter image description here

另一个选择是使用Pandas连接数据帧和绘图。如果在加载数据或通过DataFrame.rename为“FishEffort”字段指定正确的标签名称,则标签将自动指定。在

^{pr2}$

enter image description here

相关问题 更多 >

    热门问题