![](/img/trans.png)
[英]How can I have a stacked plot with a shared X axis and multiple Y axis on one of the plots?
[英]Is there a way to create either stacked plots in one or lines in one plot with stacked Y-axes with matplot/seaborn?
我是數據幀可視化的新手,我正在探索這些方法,但我似乎無法找到一種方法來創建我需要的非常具體的 plot。
我有一個 dataframe 像下面這樣我有日期時間作為索引和 4 個配置文件(而可能有更多列對繪圖不感興趣):
datetime
2019-06-11 00:00:00 28.97 38.47 NaN 41.47
2019-06-11 01:00:00 28.83 38.42 NaN 41.48
2019-06-11 02:00:00 28.72 38.38 NaN 41.49
2019-06-11 03:00:00 28.56 38.33 NaN 41.49
2019-06-11 04:00:00 28.36 38.22 NaN 41.51
我的目標是創建具有所述規格的 plot:
線圖,每個配置文件一個,但都在一個 plot 中。
線條不應相交,因此輪廓應堆疊在另一個之上,最好將輪廓 1 放在最頂部。
這些值不應改變,因此 Y 軸可能應該堆疊。
圖例應該是 4 個配置文件的整個 plot 之一。
不應出現 plot 或具有 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.