简体   繁体   English

不在显示两个带有网格序列的网格规格子图,一个是线图,另一个是条形图,时间序列在x上

[英]Two subplots of gridspec, one line plot and the other bar plot, with a time-series on x, are not displaying

I can't figure out what I'm doing wrong in my plotting. 我无法弄清楚自己在做错什么。 Plotting some data vs datetime on two subplots, one is a line plot, the other one should be a bar plot. 在两个子图上绘制一些数据与日期时间的关系,一个是线图,另一个是条形图。 I have tried several different ways of plotting it but without much success. 我尝试了几种不同的方式进行绘制,但没有成功。

UPDATE Minor progress - the first plot is displayed, the second one not. 更新小进度-显示第一个图,第二个不显示。

Data looks for example like this: 数据看起来像这样:

                    A  B    C
0 2014-10-23 15:44:00  1  1.5
1 2014-10-30 19:10:00  2  2.0
2 2014-11-05 21:30:00  3  3.3
3 2014-11-07 05:50:00  4  4.0
4 2014-11-12 12:20:00  5  5.8

The latest version of the code: 最新版本的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
import datetime

Test = pd.DataFrame({'A':[datetime.datetime(2014,10,23,15,44),datetime.datetime(2014,10,30,19,10),
datetime.datetime(2014,11,5,21,30),datetime.datetime(2014,11,7,5,50), 
datetime.datetime(2014,11,12,12,20)],'B':[1,2,3,4,5],'C':[1.5,2,3.3,4,5.8]})

fig = plt.figure(figsize=(8,8))

gs=gridspec.GridSpec(2,1, hspace=0.1, wspace=0.3, height_ratios=[1,1])

axes1 = plt.subplot(gs[0,0])
ax1 = plt.plot(Test["A"],Test["B"], 'dimgrey', linewidth = 2.5, label = "test")
#DepTime = DepTime.set_index("DATETIME")
#for l in log_times:
#    ax1 = plt.bar(l,DepTime.ix[l,"Depth"], width = 1.5, color='b')
ax1 = plt.xlim(Test.iloc[0,0],Test.iloc[-1,0])
xticks2 = pd.date_range(start=Test.iloc[0,0],end=Test.iloc[-1,0], freq='D', normalize=True) 
ax1 = plt.xticks(xticks2,xticks2)
ax1 = plt.setp(axes1.get_xticklabels(), visible=False)
ax1 = plt.ylim(1,5)
ax1 = plt.yticks(np.arange(0,5,1),rotation=0)
ax1 = plt.ylabel("B",fontsize = 12)

axes2 = plt.subplot(gs[1,0])
ax2 = Test.plot("A","C",kind='bar', ax=axes2) #
#ax2 = plt.bar(Test["A"],Test["C"],color='k')
#ax2 = plt.xlim(Test.iloc[0,0],Test.iloc[-1,0])
ax2 = plt.xlim(Test.iloc[0,0],Test.iloc[-1,0])
ax2 = plt.xlabel("A",fontsize=12)
#ax2 = plt.xaxis_date()
#ax2.xaxis.set_major_formatter(mdates.DateFormatter('%d))
ax2 = plt.xticks(xticks2,xticks2, rotation=45)
#axes2.xticklabels([x.strftime('%d/%m/%Y %H:%M') for x in xticks2])
#ax2.autofmt_xdate()
#ax2 = plt.yticks([0,1])
ax2 = plt.ylim(1,5)
ax2 = plt.setp(axes2.get_yticklabels(), visible=False)
fig = plt.show()

And the resulting figure: 结果图: 不完整的情节

I found out what the problem was!!! 我发现了问题所在!!! I don't understand why it worked fine with the line plot and not with the bar plot, but all that needed to be changed in the code is the actual one line for the bar plot. 我不明白为什么它在折线图而不是条形图上能正常工作,但是代码中需要更改的全部是条形图的实际一条线。 It should go like this: 它应该像这样:

ax2 = plt.bar(Test["A"].values,Test["C"],width=1)

So bar plot can't handle datetime? 那么条形图不能处理日期时间吗? (This answer helped) (此答案有所帮助)

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

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