0

I am trying to plot certain discrete scores based on a timestamp at which they were recorded. There are 4 separate dataframes(pandas) called lower-back-Now, lower-back-Worst, Neck-Now, and Neck-Worst. And also, at some particular days offset from the initial timestamp index I plot axvlines to denote some interventions with text. The dataframes look like these..

lbNow
                    Answer
StartUnixTime             
1970-01-01 16:45:55      4
1970-01-01 19:30:53      5
1970-01-02 05:00:11      6
1970-01-02 19:31:01      3
1970-01-03 05:06:29      5

lbWst
                    Answer
StartUnixTime             
1970-01-01 16:46:08      6
1970-01-01 19:30:53      5
1970-01-02 05:00:18      6
1970-01-02 19:31:10      7
1970-01-03 05:06:29      5

neckNow
                    Answer
StartUnixTime             
1970-01-01 16:46:26      4
1970-01-01 19:31:39      6
1970-01-02 05:00:27      6
1970-01-02 19:31:17      2
1970-01-03 05:07:04      4

neckWst       
                    Answer
StartUnixTime             
1970-01-01 16:46:31      5
1970-01-01 19:31:47      6
1970-01-02 05:00:28      6
1970-01-02 19:31:22      7
1970-01-03 05:07:08      4

and the code for the plot for plottin using matplotlib in subplots is as follows:

fig = plt.figure(figsize=(15,15))
ax1 = fig.add_subplot(211)

myfmt = mdates.DateFormatter('%m-%d')

ax1.plot(lbNow,color='#055F65',label='Lower Back Now')
ax1.plot(lbWst,color='#c50d63',label='Lower Back worst',linestyle='--')
ax1.xaxis.set_major_formatter(myfmt)
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)
# fig.autofmt_xdate()

ax1.legend(loc='best')
ax1.set(title='Lower Back Pain Dairy',xlabel='Timestamps since start date',ylabel='Pain Scores')

ax1.axvline(x=lbNow.index[0] + pd.DateOffset(days=1),color = 'r',linestyle='-.')
ax1.text(lbNow.index[0] + pd.DateOffset(days=2),8.1,'MBB#1',rotation=45)

ax1.axvline(x=lbNow.index[0] + pd.DateOffset(days=22),color = 'b',linestyle='-.')
ax1.text(lbNow.index[0] + pd.DateOffset(days=23),8.1,'MBB#2',rotation=45)

ax1.axvline(x=lbNow.index[0] + pd.DateOffset(days=43),color = 'k',linestyle='-.')
ax1.text(lbNow.index[0] + pd.DateOffset(days=44),8.1,'RFA',rotation=45)
# ax1.set_ylim((0,10))
ax1.grid(True)

ax3 = fig.add_subplot(212)

ax3.plot(neckNow,color='#4b0d2b',label='Neck Now',linestyle='-')
ax3.plot(neckWst,color='#c50d63',label='Neck Worst',linestyle='--')

ax3.xaxis.set_major_formatter(myfmt)
for label in ax3.xaxis.get_ticklabels():
    label.set_rotation(45)
fig.autofmt_xdate()

ax3.legend(loc='lower center')
ax3.set(title='Neck Pain Dairy',xlabel='Timestamps since start date', ylabel='Pain Scores')

ax3.axvline(x=neckNow.index[0] + pd.DateOffset(days=1),color = 'r',linestyle='-.',clip_on=False)
ax3.text(neckNow.index[0] + pd.DateOffset(days=2),8,'MBB#1',rotation=45)

ax3.axvline(x=neckNow.index[0] + pd.DateOffset(days=22),color = 'b',linestyle='-.')
ax3.text(neckNow.index[0] + pd.DateOffset(days=23),8,'MBB#2',rotation=45)

ax3.axvline(x=neckNow.index[0] + pd.DateOffset(days=43),color = 'k',linestyle='-.')
ax3.text(neckNow.index[0] + pd.DateOffset(days=44),8,'RFA',rotation=45)

ax3.grid(True)
fig.tight_layout()
plt.show()

The problem is with the second subplot with neckNow and neckWst plotting. If I plot both df then the ax3.axvline do not appear as shown enter image description here

But then if Comment the neckNow plot then the lines appear as shown. enter image description here

Is there any problem with the y limits or is the timestamp causing this weird error ?

lamo_738
  • 440
  • 1
  • 5
  • 15
  • You can look into [z-order](https://matplotlib.org/2.0.2/examples/pylab_examples/zorder_demo.html) – harvpan Jun 13 '18 at 16:09
  • Hey thanks for the suggestion, but no luck. The first subplot is working without z-order, so i dont think zorder is the issue. Nevertheless i did give a try but not change – lamo_738 Jun 13 '18 at 16:38
  • Which version of matplotlib (and other relevant libraries) are you using? I've tested your code with matplotlib v.2.2.2 and pandas v.0.23.1 all seems to work. – PieCot Jun 13 '18 at 18:57
  • I am guessing the clip_on=False found in the line `ax3.axvline(x=neckNow.index[0] + pd.DateOffset(days=1),color = 'r',linestyle='-.',clip_on=False)` ,should be added to all the `ax.axvline` plotting lines. Or try removing them from all the `ax.axvline` plotting line....Take a look at [https://stackoverflow.com/questions/6146290/plotting-a-line-over-several-graphs?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa] for a similar use-case – neutralCreep Jun 13 '18 at 19:07
  • @PieCot I am using matplotlib v2.1.2 and pandas v0.22.0 . I am using it under anaconda distribution. Should I need to update those ? .. so the code is working fine right ? – lamo_738 Jun 13 '18 at 19:54
  • @neutralCreep Hey, I did go through that post, and I did play around with clip_on param but there was no change. And also, in my problem statement, i dont want it across the two plots as done in the link you shared. – lamo_738 Jun 13 '18 at 19:55
  • @lamo_738 I'm running your code in a conda virtual environment too. If updating your libraries does not solve the problem, maybe you should check your data: it could contain some weird or unexpected value. – PieCot Jun 13 '18 at 21:04
  • Hey .. I did update the lib, but there was no luck. But i did find out why ! apparently the 'Answer' pandas column was in string rather than integer. Hence, the y axis was a string rather than int between 0-10. So the y axis was screwing up the plots and axvline. Anyways Thanks for the help Guys ! – lamo_738 Jun 15 '18 at 17:29

0 Answers0