Python, x-axis title is overlapping the tick labels in matplotlib

3.4k views Asked by At

I'm plotting a graph and the x-axis label is not visible in the graph.

enter image description here

I have tried to solve it by adding the

ax.xaxis.labelpad = -10 # Adjust x-axis label position

Instead the x-label will overlap the ticker label

enter image description here

How can this be adjusted to show both x-axis label and x-ticker labels within the plot figure?


Full Code to replicate graph:

#################################
###   Modules imported used   ###
#################################

import pandas as pd
import numpy as np
from datetime import datetime
from datetime import date
import time


import matplotlib.pyplot as plt
import matplotlib
import matplotlib.dates as mdates



# file_path_setup = 'G:/Stocks/PowerPivotApps/Price download/'
# Performance_History = pd.read_csv(file_path_setup + 'Performance.txt', dtype=str, sep=',')


# Portfolio = Performance_History.loc[Performance_History['ExecutionType'] == 'All Portfolios']
# Portfolio = Performance_History.loc[Performance_History['ExecutionType'] == 'Selected Portfolios'] # remove "# set minimum level for performance time"


#Portfolios_Nr_of_Stocks = Portfolio['NrOfStocks']
#Portfolio_Performance_Time = Portfolio['PerformanceTime']
#Portfolio_Date = Portfolio['Date']


Portfolio_Date =  ['2020-08-31','2020-09-01','2020-09-02','2020-09-02','2020-09-03','2020-09-04','2020-09-07','2020-09-08','2020-09-09','2020-09-09','2020-09-10','2020-09-11','2020-09-14','2020-09-15','2020-09-16','2020-09-17','2020-09-18','2020-09-21','2020-09-22','2020-09-22','2020-09-23','2020-09-24','2020-09-25','2020-09-28','2020-09-29','2020-09-30','2020-10-01','2020-10-02','2020-10-05','2020-10-06','2020-10-07','2020-10-08','2020-10-08','2020-10-09','2020-10-12','2020-10-13','2020-10-14','2020-10-15','2020-10-16']
Portfolio_Performance_Time =['00:11:11','00:11:07','00:11:16','00:10:42','00:10:54','00:10:46','00:10:27','00:11:23','00:11:35','00:10:23','00:10:51','00:41:22','00:11:05','00:11:15','00:10:50','00:10:41','00:19:47','00:10:43','00:10:48','00:11:12','00:11:05','00:10:45','00:11:02','00:10:57','00:11:01','00:15:17','00:14:33','00:18:49','00:14:28','00:20:45','00:14:29','00:14:45','00:17:52','00:14:37','00:14:08','00:15:05','00:14:46','00:14:39','00:14:40']
Portfolios_Nr_of_Stocks = ['621','619','617','619','622','622','622','621','622','622','622','613','622','621','621','607','621','622','621','622','620','620','622','620','620','680','679','680','681','488','681','681','680','678','678','676','678','676','676']

# Convert To integer
numberofstocks = [int(stock) for stock in Portfolios_Nr_of_Stocks]

# Convert to time
def get_sec(time_str):
    """Get Seconds from time."""
    h, m, s = time_str.split(':')
    return int(h) * 3600 + int(m) * 60 + int(s)

PerformanceTime = [get_sec(t) for t in Portfolio_Performance_Time]

# print(type(numberofstocks)) # print type

# convert to date series
date_portfolio = [datetime.strptime(d, '%Y-%m-%d') for d in Portfolio_Date]


# https://matplotlib.org/gallery/api/two_scales.html
# https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/


# create figure and axis objects with subplots()
fig,ax = plt.subplots(figsize=(12, 8)) # figsize -> size of the plot window

# make a plot
ax.plot(date_portfolio, PerformanceTime, color="red", marker="x")
# set x-axis label
ax.set_xlabel("Date", fontsize=14)

# set y-axis label
ax.set_ylabel("Performance Time",color="red",fontsize=14)

# set title
ax.set_title("Execution History",fontsize=20, loc="center", pad=10)

# format y-axis label to hh:mm:ss
formatter_yx1 = matplotlib.ticker.FuncFormatter(lambda s, x: time.strftime('%H:%M:%S', time.gmtime(s)))
ax.yaxis.set_major_formatter(formatter_yx1)

# rotate x-axis lables and adjust size
plt.xticks(rotation=90, ha='right')
# plt.xticks(rotation=90, ha='right', fontsize='x-small') # Small font text

# set minimum level for performance time, y-axis 1
ax.set_ylim([min(PerformanceTime)-100,25*60]) # -100 -> set minimum. 25*60 -> Set maximum

# twin object for two different y-axis on the sample plot
ax2=ax.twinx()
# make a plot with different y-axis using second axis object
ax2.plot(date_portfolio, numberofstocks,color="blue",marker="o")
# ax2.set_ylim([620, 680])
ax2.set_ylabel("Nr Of Stocks",color="blue",fontsize=14)

# set minimum level for performance time, y-axis 2
ax2.set_ylim([600, max(numberofstocks)+10]) # -100 -> set minimum. 25*60 -> Set maximum

# set date interval
ax.xaxis.set_major_locator(mdates.DayLocator(interval=7)) # max interval
ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1)) # minimum interval
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) # set date format
ax.xaxis.labelpad = -10 # Adjust x-axis label position


# Plot graph
plt.show()
1

There are 1 answers

0
Wizhi On BEST ANSWER

You could use "Tight Layout" function in matplotlib to solve the issue.

Add the line before you plot the graph, where h_pad will adjust the height, w_pad will adjust the width.

# Adjust x-axis margins
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=5.0)

And remove this part:

ax.xaxis.labelpad = -10 # Adjust x-axis label position

Result:

enter image description here