'Python - Pyplot x-axis not showing on graph

pyplot is not showing the x-axis on the graph:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('sitka_weather_2014.csv')    

df['AKST'] = pd.to_datetime(df.AKST)
df['Dates'] = df['AKST'].dt.strftime('%b %d, %Y')
df.set_index("Dates", inplace= True)

# Plot Data
fig = plt.figure(dpi=256, figsize=(14, 7))
plt.title("Daily high and low temperature - 2014")

df['Max TemperatureF'].plot(linewidth=1, c='blue', label="Max Temperature °F")
df['Min TemperatureF'].plot(linewidth=1, c='red', label="Min Temperature °F")

plt.grid(True)
plt.rc('grid', linestyle=":", linewidth=1, color='gray')

plt.legend(loc='upper left')

plt.xlabel('', fontsize=10)
plt.ylabel("Temperature (°F)", fontsize=10)
plt.tick_params(axis='both', which='major', labelsize=10)
fig.autofmt_xdate(rotation=45)

plt.show()

The x-axis should be the index of the Pandas Dataframe (df) containing the dates.

enter image description here



Solution 1:[1]

Your code is actually fine. I tried to run it with the necessary sitka_weather_2014.csv file and it works.

The problem is that you can't see the x-axis because the size of the figure is too big, and thus the description of the x-axis dissapears. Try to scale your figure e.g. by making the dpi smaller:

fig = plt.figure(dpi=100, figsize=(14, 7)) #dpi=100 instead of dpi=256

Or make the labelsize smaller:

plt.tick_params(axis='both', which='major', labelsize=5) #labelsize=5 instead of labelsize=10

Whatever works best for you. But the code is fine and the description of the x-axis is showing.

Solution 2:[2]

You have your xlabel value set to null:

plt.xlabel('', fontsize=10)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vuks
Solution 2 Grayrigel