I am making a script that would create some dataframe graphs for me in Pandas and then publish them on a website.
I’m having problems with set_major_formatter_locator but I don’t understand where I’m going wrong….
This is part of the code:
import pandas as pd
from matplotlib import pyplot as plt
import shutil
import subprocess
from datetime import datetime
import matplotlib.dates as mdates
import locale
locale.setlocale(locale.LC_ALL,'it_IT.utf8')
df = pd.read_csv('/var/www/my/path/output.csv',index_col=0, parse_dates=[0])
df=df.sort_index()
# Precipitazioni - irrigazioni
plt = df.plot(y="P_1_1_1_Tot_2", kind="line", title="Precipitazioni e irrigazioni", ylabel="mm", grid=1, figsize=(8,4),xlabel="",rot=90,legend=False)
#plt.legend(["Precipitazioni e irrigazioni"],loc="best",frameon=False , framealpha=None)
plt.set_ylim(bottom=0)
keep=keep1=df['P_1_1_1_Tot_2'].dropna()
keep=keep.sort_index()
keep=keep.tail(1)
keep=keep.index[0]
keep=keep.strftime("%d/%m/%Y %H:%M")
dt_string="Ultimo dato: " + keep
plt.set_xlim(keep1.index.min(),keep1.index.max())
plt.xaxis.set_major_formatter_locator(mdates.DayLocator(interval=1))
plt.xaxis.set_major_formatter(mdates.DateFormatter('%d-%b'))
plt.text(1.005, 0.01, dt_string,transform=plt.transAxes, fontsize='xx-small',rotation=90)
plt.grid(visible=True,which="both")
plt.figure.savefig('/my/path/fig1.png',dpi=150,bbox_inches="tight")
shutil.move('/my/path/fig1.png','/my/second/path/fig1.png')
Error:
Exception has occurred: AttributeError
‘XAxis’ object has no attribute ‘set_major_formatter_locator’
File “/opt/script/graphs.py”, line 46, in
plt.xaxis.set_major_formatter_locator(mdates.DayLocator(interval=1))
AttributeError: ‘XAxis’ object has no attribute ‘set_major_formatter_locator’
I’ve done some testing but I just can’t figure out where the problem is.
For weeks the code runs without errors or warnings, then suddenly it changes behavior.
Trisonic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.