Issue
How to draw vertical lines across multiple subplots? Whatever I try axvlines
are shown only in the bottom subplot:
import pandas as pd
import matplotlib.pyplot as plt
fig, zx = plt.subplots(5,1, gridspec_kw={'height_ratios':[1,1,1,1,1]})
fig.subplots_adjust(hspace=0.01)
pd.DataFrame({'A':[1,2,3,4,5]}).plot( grid = True, ax=zx[2])
for x_val in [2.25,3.25,4.25]:
plt.axvline(x_val, color = 'green')
pd.DataFrame({'B':[6,5,4,3,2]}).plot( grid = True, ax=zx[3])
for x_val in [2.25,3.25,4.25]:
plt.axvline(x_val, color = 'red')
plt.show()
Solution
You can use axes, zx like this to draw line:
import pandas as pd
import matplotlib.pyplot as plt
fig, zx = plt.subplots(5,1, gridspec_kw={'height_ratios':[1,1,1,1,1]})
fig.subplots_adjust(hspace=0.01)
pd.DataFrame({'A':[1,2,3,4,5]}).plot( grid = True, ax=zx[2])
for x_val in [2.25,3.25,4.25]:
zx[2].axvline(x_val, color = 'green')
pd.DataFrame({'B':[6,5,4,3,2]}).plot( grid = True, ax=zx[3])
for x_val in [2.25,3.25,4.25]:
zx[3].axvline(x_val, color = 'red')
plt.show()
Answered By - Scott Boston
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.