I have this df called normales
:
JavaScript
x
13
13
1
CODIGO MES TMAX TMIN PP
2
0 000130 Enero 31.3 23.5 51.1
3
1 000130 Febrero 31.7 23.8 136.7
4
2 000130 Marzo 31.8 23.9 119.5
5
3 000130 Abril 31.5 23.7 55.6
6
4 000130 Mayo 30.6 23.1 15.6
7
8
4447 158328 Agosto 11.9 -10.6 2.2
9
4448 158328 Septiembre 13.2 -9.1 1.2
10
4449 158328 Octubre 14.6 -8.2 4.9
11
4450 158328 Noviembre 15.4 -7.2 11.1
12
4451 158328 Diciembre 14.7 -5.3 35.9
13
With this code i’m plotting time series and bars:
JavaScript
1
33
33
1
from matplotlib.ticker import MaxNLocator
2
from matplotlib.font_manager import FontProperties
3
for code, data in normales.groupby('CODIGO'):
4
5
6
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, sharex=False, sharey=False,figsize=(20, 15))
7
data.plot('MES',["TMAX"], alpha=0.5, color='red', marker='P', fontsize = 15.0,ax=ax1)
8
9
data.plot('MES',["TMIN"], alpha=0.5,color='blue',marker='D', fontsize = 15.0,ax=ax2)
10
11
data.plot('MES',["PP"],kind='bar',color='green', fontsize = 15.0,ax=ax3)
12
13
tabla=ax4.table(cellText=data[['TMAX','TMIN','PP']].T.values,colLabels=["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto",
14
"Septiembre","Octubre","Noviembre","Diciembre"],
15
rowLabels=data[['TMAX','TMIN','PP']].columns,rowColours =["red","blue","green"],
16
colColours =["black"] * 12,loc="center",bbox = [0.0, -0.5, 1, 1])
17
tabla.auto_set_font_size(False)
18
tabla.set_fontsize(15)
19
tabla.scale(1,2)
20
ax4.axis('off')
21
22
ax1.set_ylabel("TemperaturanMáxima °C/mes", fontsize = 15.0)
23
ax1.yaxis.set_major_locator(MaxNLocator(integer=True))
24
ax2.set_ylabel("TemperaturanMínima °C/mes", fontsize = 15.0)
25
ax2.yaxis.set_major_locator(MaxNLocator(integer=True))
26
ax3.set_ylabel("Precipitación mm/mes", fontsize = 15.0)
27
ax3.yaxis.set_major_locator(MaxNLocator(integer=True))
28
ax1.set_xlabel("")
29
ax2.set_xlabel("")
30
ax3.set_xlabel("")
31
ax4.set_xlabel("")
32
33
You can realize that i’m using ax.yaxis.set_major_locator(MaxNLocator(integer=True))
in every axis to make integer the numbers of the axis. Although i’m using ax.yaxis.set_major_locator(MaxNLocator(integer=True))
i’m getting graphics with non integer (float) values in the yaxis. Do you know why this is happening?
Thanks in advance.
Advertisement
Answer
From the MaxNLocator
docs:
integer bool, default: False
If True, ticks will take only integer values, provided at least min_n_ticks integers are found within the view limits.
….
min_n_ticks int, default: 2
You need to change min_n_ticks
to 1
since ax2
only has one integer within the view limits, namely 12
.