Karl Anka
Karl Anka

Reputation: 2849

Prevent xticks from overlapping table in plot

How could i prevent the date-ticks from overlapping the table below the plot? My code simplified looks like this:

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

date_list = [datetime.datetime.today() + datetime.timedelta(days=x) for x in range(0, 10)]

df = pd.DataFrame({'date_':date_list,
      'y_line':np.random.randint(0,10,(1,10))[0],
      'y1': [str(x) for x in np.random.randint(0,10,(1,10))[0]],
      'y2':[str(x) for x in np.random.randint(0,10,(1,10))[0]]})

plt.plot(df.date_, df.y_line)
plt.table(cellText = [df.y1, df.y2], rowLabels = ['y1', 'y2'])
plt.xticks(rotation=90)
plt.show()

Which yields this: enter image description here

Upvotes: 0

Views: 1657

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

You can add some padding between the axes and the ticklabels using

plt.tick_params(axis='x', pad=20)

You might then use plt.tight_layout() or something like plt.subplots_adjust(bottom=0.25, top=0.95) to make the ticklabels still fit into the canvas.

Upvotes: 3

Related Questions