Python 是否有一种方法可以使用matplot/seaborn在一个图中创建堆叠的图或在一个图中使用堆叠的Y轴创建线?
我对数据帧的可视化是新手,我正在探索方法,但我似乎无法找到一种方法来创建我需要的非常具体的图 我有一个像下面这样的数据框,其中我有datetime作为索引和4个配置文件(而可能有更多的列对绘图不感兴趣): 我的目标是创建具有所述规格的绘图:Python 是否有一种方法可以使用matplot/seaborn在一个图中创建堆叠的图或在一个图中使用堆叠的Y轴创建线?,python,pandas,dataframe,matplotlib,seaborn,Python,Pandas,Dataframe,Matplotlib,Seaborn,我对数据帧的可视化是新手,我正在探索方法,但我似乎无法找到一种方法来创建我需要的非常具体的图 我有一个像下面这样的数据框,其中我有datetime作为索引和4个配置文件(而可能有更多的列对绘图不感兴趣): 我的目标是创建具有所述规格的绘图: 线形图,每个纵断面一个,但全部在一个图中 线应该而不是截距,因此剖面应该一个叠在另一个之上,最好剖面1位于最顶部 值不应更改,因此Y轴可能应堆叠 图例应为4个剖面的整个绘图的图例 不应显示具有NaN值的纵断面图或纵断面线 如果您能提出一些建议或指导我解决问题
感谢您的输入:-)可能会对您有所帮助:
df[3] = pd.to_numeric(df[3], errors = 'coerce').fillna(0)
df = df.rename(columns = {1:'A',2:'B',3:'C',4:'D'})
df = df.set_index(0)
df
Out[1]:
A B C D
0
2019-06-11 00:00:00 28.97 38.47 0.0 41.47
2019-06-11 01:00:00 28.83 38.42 0.0 41.48
2019-06-11 02:00:00 28.72 38.38 0.0 41.49
2019-06-11 03:00:00 28.56 38.33 0.0 41.49
2019-06-11 04:00:00 28.36 38.22 0.0 41.51
import matplotlib.dates as mdates
ax = [None]*4
fig = plt.figure()
f, (ax[0], ax[1], ax[2], ax[3]) = plt.subplots(4, 1, sharex=True)
ax[3].set_xlabel('Hours')
color = ['r','g','b','y']
for i in range(4):
col = df.columns[i]
_ = ax[i].plot(df.index,df[col], label = col, c = color[i])
xfmt = mdates.DateFormatter('%H:%M')
ax[3].xaxis.set_major_formatter(xfmt)
ax[3].xaxis.set_major_locator(mdates.HourLocator(byhour=range(24)))
_ = f.legend()
谢谢@Alexey,我需要花一些时间来理解您的解决方案,因为它看起来很像我需要的。我印象深刻:-)
df[3] = pd.to_numeric(df[3], errors = 'coerce').fillna(0)
df = df.rename(columns = {1:'A',2:'B',3:'C',4:'D'})
df = df.set_index(0)
df
Out[1]:
A B C D
0
2019-06-11 00:00:00 28.97 38.47 0.0 41.47
2019-06-11 01:00:00 28.83 38.42 0.0 41.48
2019-06-11 02:00:00 28.72 38.38 0.0 41.49
2019-06-11 03:00:00 28.56 38.33 0.0 41.49
2019-06-11 04:00:00 28.36 38.22 0.0 41.51
import matplotlib.dates as mdates
ax = [None]*4
fig = plt.figure()
f, (ax[0], ax[1], ax[2], ax[3]) = plt.subplots(4, 1, sharex=True)
ax[3].set_xlabel('Hours')
color = ['r','g','b','y']
for i in range(4):
col = df.columns[i]
_ = ax[i].plot(df.index,df[col], label = col, c = color[i])
xfmt = mdates.DateFormatter('%H:%M')
ax[3].xaxis.set_major_formatter(xfmt)
ax[3].xaxis.set_major_locator(mdates.HourLocator(byhour=range(24)))
_ = f.legend()