I have data where I want to plot their timeseries. The FRQ is the column bar and the FILT is the line chart.
YEAR FRQ FILT
1960 1
1961 3
1962 1 1.416666667
1963 1 0.916666667
1964 0 0.833333333
1965 1 1.333333333
1966 3 1.75
1967 2 1.5
1968 0 0.833333333
1969 0 0.666666667
1970 1 1.166666667
1971 3 1.666666667
1972 1 1.833333333
1973 2 1.75
1974 2 1.5
1975 1 1
1976 0 0.5
1977 0 0.416666667
1978 1 0.833333333
1979 1 1.333333333
1980 3 1.5
1981 0 1.333333333
1982 2 1
1983 0 0.833333333
1984 1 0.75
1985 1 0.583333333
1986 0 0.5
1987 0 0.75
1988 2 1.166666667
1989 2 1.25
1990 0 0.916666667
1991 1 0.833333333
1992 0 1.25
1993 4 1.5
1994 0 1.416666667
1995 1 1.25
1996 2 1.416666667
1997 1 1.833333333
1998 3 2
1999 2 1.75
2000 1 1.166666667
2001 0 1.083333333
2002 1 1.666666667
2003 5 2
2004 0 1.75
2005 1 1.5
2006 2 1.75
2007 3 2.166666667
2008 1 2.333333333
2009 4 2.333333333
2010 1 2.25
2011 3 1.916666667
2012 1 1.5
2013 1 1.166666667
2014 1 0.916666667
2015 1 0.75
2016 0 0.666666667
2017 1 0.75
2018 1 0.833333333
2019 1
2020 0
My working code looks like this:
#Read Tropical cyclone frequency
TC = pd.read_csv (r'G:\TC_Atlas\\data.csv', encoding="ISO-8859-1")
TC = pd.DataFrame(TC,columns=['YEAR','FRQ','FILT','FRQ2','FILT2','LMI','FILTLMI','LMI2','FILTLMI2'])
TC= TC[TC['YEAR'].between(1960,2020,inclusive="both")]
#TC = TC.set_index('YEAR')
labels =['1960','1961','1962','1963','1964','1965','1966','1967','1968','1969','1970','1971','1972','1973','1974','1975',
'1976','1977','1978','1979','1980','1981','1982','1983','1984','1985','1986','1987','1988','1989','1990','1991','1992',
'1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009',
'2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020']
#Plot timeseries
TC['FRQ'].plot(kind='bar', color='lightgray', width=1, edgecolor='darkgray')
TC['FILT'].plot(kind='line',color='black')
plt.suptitle("TC Passage Frequency",fontweight='bold',y=0.95,x=0.53)
plt.title("Isabela (1960-2020)", pad=0)
L=plt.legend()
L.get_texts()[0].set_text('filtered')
plt.yticks(fontsize=12)
tickvalues = range(0,len(labels))
plt.xticks(ticks = tickvalues ,labels = labels, rotation = 30)
plt.xlabel('Year', color='black', fontsize=14, weight='bold',labelpad=10)
plt.ylabel('Frequency' , color='black', fontsize=14, weight='bold',labelpad=15)
plt.tight_layout()
plt.show()
Unfortunately, I cannot adjust the interval of the x-axis to make the xticks every 4 year interval. I have scouring for possible solution. Kindly help. I use Jupyter Notebook in Python. Below is the sample output but my goal is to make the xticks 4 year interval.
You are explicitly adding ticks every year. The culprit is this line:
plt.xticks(ticks = tickvalues ,labels = labels, rotation = 30)
To make them less frequent, you can take advantage of list slicing like so:
plt.xticks(ticks=tickvalues[::4], labels=labels[::4], rotation=30)
If you need to e.g. shift them so that a specific year is listed, you could set the initial index in the slice as well (e.g. tickvalues[2::4]
).
EDIT: Since you are producing plots from a pd.DataFrame
, a more sensible way would be using a column/index for ticks:
plt.xticks(TC['YEAR'][::4], rotation=30)
If your data is not converted properly, you might encounter weird bugs with ordering, in which case make sure you convert YEAR
column to a number type by using astype
or to_numeric
first (a detailed writeup can be found in this SO answer).