I am trying to set the number of tick marks for both the x and y axis to 12. However, only the y axis is being set and the x axis stays the same. I am using the locator_params to set both axis.
plt.locator_params(axis='both', nbins=12)
from pathlib import Path
import csv
import matplotlib.pyplot as plt
from datetime import datetime
path = Path('data_visualization/weather_data/death_valley_2021_simple.csv')
lines = path.read_text(encoding='utf-8').splitlines()
reader = csv.reader(lines)
header_row = next(reader)
# Extract dates and high temperatures.
dates, highs, lows = [], [], []
for row in reader:
current_date= datetime.strptime(row[2], '%Y-%m-%d')
try:
high = int(row[3])
low = int(row[4])
except ValueError:
print(f"Missing data for {current_date}")
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
# Plot the high temperatures.
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.plot(dates, highs, color='red', alpha=0.5)
ax.plot(dates, lows, color='blue', alpha=0.5)
ax.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)
# Format the plot.
title = "Daily High and Low Temperatures, 2021nDeath Valley, CA"
ax.set_title(title, fontsize=20)
ax.set_xlabel('', fontsize=16)
fig.autofmt_xdate()
ax.set_ylabel("Temperature (F)", fontsize=16)
ax.tick_params(labelsize=16)
plt.locator_params(axis='both', nbins=12)
plt.show()
I tried setting just the x axis to 12 and still nothing worked. Would it be because its a date and not an integer? Any help would be appreciated.
sample data from csv file:
"STATION","NAME","DATE","TMAX","TMIN","TOBS"
"USC00042319","DEATH VALLEY NATIONAL PARK, CA US","2021-01-01","71","51","56"
"USC00042319","DEATH VALLEY NATIONAL PARK, CA US","2021-01-02","67","42","51"
"USC00042319","DEATH VALLEY NATIONAL PARK, CA US","2021-01-03","66","41","49"
jmthompson32 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.