Pandas 带有数据帧打印功能的彩色地图

Pandas 带有数据帧打印功能的彩色地图,pandas,matplotlib,plot,colormap,Pandas,Matplotlib,Plot,Colormap,我有来自多个站点的数据,这些数据记录了监控参数的急剧变化。如何使用与值相关的颜色绘制所有这些站点的数据,以增强可视化效果 import numpy as np import pandas as pd import string # site names cols = string.ascii_uppercase # number of days ndays = 3 # index index = pd.date_range('2018-05-01', periods=3*24*60, fr

我有来自多个站点的数据,这些数据记录了监控参数的急剧变化。如何使用与值相关的颜色绘制所有这些站点的数据,以增强可视化效果

import numpy as np
import pandas as pd
import string

# site names
cols = string.ascii_uppercase

# number of days
ndays = 3

# index
index = pd.date_range('2018-05-01', periods=3*24*60, freq='T')

# simulated daily data
d1 = np.random.randn(len(index)//ndays, len(cols))
d2 = np.random.randn(len(index)//ndays, len(cols))+2
d3 = np.random.randn(len(index)//ndays, len(cols))-2
data=np.concatenate([d1, d2, d3]) 

# df = pd.DataFrame(data=data, index=index, columns=list(cols))
df.plot(legend=False)
在上面的代码中,每个站点(列)被分配一种颜色。有没有办法用不同的颜色表示参数值

我想另一种选择是使用散点图函数中的colormaps选项:

然而,如果我随着时间的推移进行绘图(即,
x=df.index
),事情似乎并没有按预期进行


还有其他选择吗?或者建议如何更好地可视化时间序列中的突然变化?

在下面的内容中,我将仅使用3列和每小时数据,以使绘图看起来不那么混乱。这些示例也适用于原始数据

cols = string.ascii_uppercase[:3]
ndays = 3
index = pd.date_range('2018-05-01', periods=3*24, freq='H')

# simulated daily data
d1 = np.random.randn(len(index)//ndays, len(cols))
d2 = np.random.randn(len(index)//ndays, len(cols))+2
d3 = np.random.randn(len(index)//ndays, len(cols))-2
data=np.concatenate([d1, d2, d3]) 

df = pd.DataFrame(data=data, index=index, columns=list(cols))
df.plot(legend=False)

熊猫之路 您运气不好,
DataFrame.plot.scatter
无法处理类似日期时间的数据,原因是长期存在

matplotlib方法 Matplotlib的
散点
可以处理类似于日期时间的数据,但x轴的缩放不符合预期

for col in df.columns:
    plt.scatter(df.index, df[col], c=df[col])
plt.gcf().autofmt_xdate()

这看起来像一个错误,但我找不到任何报告。您可以通过手动调整x限制来解决此问题

for col in df.columns:
    plt.scatter(df.index, df[col], c=df[col])

start, end = df.index[[0, -1]]
xmargin = (end - start) * plt.gca().margins()[0]
plt.xlim(start - xmargin, end + xmargin)
plt.gcf().autofmt_xdate()

不幸的是,x轴格式化程序没有熊猫格式化程序好

熊猫之路,重游 我偶然发现了这个把戏,我不明白为什么它会起作用。如果在调用matplotlib的
散点
之前绘制由相同日期时间数据索引的熊猫系列,则自动缩放问题将消失,您将获得漂亮的熊猫格式

所以我画了第一列的不可见图,然后是散点图

df.iloc[:, 0].plot(lw=0)  # invisible plot
for col in df.columns:
    plt.scatter(df.index, df[col], c=df[col])

你说的“不按预期工作”是什么意思?你希望它如何工作?时间序列的突然变化在你的情节中已经很明显了。@Goyo。。。使用DaytimeIndex时,绘图会缩小。它需要使用set_xlim进行手动调整。。。另外,使用
plt.scatter()
意味着时间轴的解释与内部函数不同。因此,我希望保持一致,只使用熊猫。
df.iloc[:, 0].plot(lw=0)  # invisible plot
for col in df.columns:
    plt.scatter(df.index, df[col], c=df[col])