I have been trying to add a vertical line across subplots that have the same DateTime x axis. The code i am using is:
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pickle
# Import data for minimum reproducible example #
file_to_read = open("example.dat", "rb")
B = pickle.load(file_to_read)
file_to_read.close()
## I would like to use something like this to locate the
## point that correspond to that datetime and plot the vertical line
dt2 = pd.to_datetime("2018-10-31 06:43:10.000000")
dt3 = pd.to_datetime("2018-10-31 06:43:35.600000")
s2 = B.index.unique().get_loc(dt2, method='nearest')
s2a = B.index.unique().get_loc(dt3, method='nearest')
size=21
int1=1
fig, axs = plt.subplots(2, sharex=True)
axs[1].plot(B.L,linewidth=3,label='$B_{z}$')
axs[1].plot(B.M,linewidth=3,label='$B_{y}$')
axs[1].plot(B.N,linewidth=3,label='$B_{x}$')
axs[1].tick_params(axis='both',labelsize=size-3)
axs[1].legend( fontsize=size)
axs[1].legend(frameon=False, loc='upper left', ncol=3, fontsize=size)
axs[1].set_ylim([-80,50])
axs[1].set_ylabel(r'$B_{NML} \ (nT)$', fontsize=size)
axs[0].plot(B.Total,linewidth=3,color='black',label='$|B|$')
axs[0].legend(loc=4, fontsize=size)
axs[0].legend(frameon=False, loc='lower left', ncol=1, fontsize=size)
axs[0].tick_params(axis='both',labelsize=size-3)
axs[0].set_ylim([0,90])
axs[0].set_ylabel(r'$B_{tot} \ (nT)$', fontsize=size)
## axis limits ##
axs[0].set_xlim([dt2, dt3])
## Add title ##
fig.text(0.5, 0.9, '2018 Oct 31', ha='center', va='center',fontsize=size)
## Gap between subplots
plt.subplots_adjust(wspace=0.01, hspace=0.12)
## add vertical line ##
line = plt.Line2D((.42,.42),(.125,.88),color='b',linewidth=1)
line2 = plt.Line2D((.55,.55),(.125,.88),color='b',linewidth=1)
fig.add_artist(line)
fig.add_artist(line2)
plt.plot()
I was able to add a vertical line but I would like to automate the process. Inserting for example the DateTime and plot this instead of guessing the point in the graph that corresponds to this datetime. For example using something like this:
dt2 = pd.to_datetime("2018-10-31 06:43:10.000000")
dt3 = pd.to_datetime("2018-10-31 06:43:35.600000")
s2 = B.index.unique().get_loc(dt2, method='nearest')
s2a = B.index.unique().get_loc(dt3, method='nearest')
The data required for minimum reproducible example can be found here: https://www.dropbox.com/s/w9gblp3dc361nw6/example.dat?dl=0