0

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: The incomplete plot

durbachit
  • 4,626
  • 10
  • 36
  • 49
  • What do you mean with *"are not displaying"*? The entire figure? One of the subplots? – Bart Sep 08 '16 at 04:55
  • At this point it's not the data. Basically I have two empty subplots... – durbachit Sep 08 '16 at 05:15
  • plot against and modify the axes. e.g., `test.plot(...ax=ax2); ax2.set_xlim(...)` – Paul H Sep 08 '16 at 05:20
  • Slightly updated (Modifying axes didn't work, unfortunately...kernel crushed) – durbachit Sep 08 '16 at 05:37
  • If I use `ax2 = plt.bar(Test["A"],Test["C"],color='k')` instead of `ax2 = Test.plot(kind='bar', ax=axes2)`, I get `TypeError: float() argument must be a string or a number`. I am confused... – durbachit Sep 08 '16 at 07:41

1 Answers1

0

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)

Community
  • 1
  • 1
durbachit
  • 4,626
  • 10
  • 36
  • 49