I am trying to create a contourf image using matplotlib pyplot contourf. It needs to look the same as what a SAS program creates with the same data. We are transitioning from SAS to Python. The contourf image does not look the same. I need help with what to do with the data.
The x axis needs to be the months 1 through 12
The y axis needs to be the hours 0 through 21, step 3
The above data is a Pandas dataframe named cigvis_df
I get the data to plot using:
cigvis3000_df = cigvis_df[['CIG3000V3']].copy()
z_data = cigvis3000_df.to_numpy()
x_axis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y_axis = [0, 3, 6, 9, 12, 15, 18, 21]
I'm not sure what to do with all the month and hour data from the dataframe.
I'm sure it must influence the 'z_data' but I don't know how.
This is the code that produces an image but it doesn't look exactly like the one produced by SAS.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
cigvis3000_df = cigvis_df[['CIG3000V3']].copy()
z_data = cigvis3000_df.to_numpy()
plotcolors = 'white', '#FFFFB2', '#FED976', '#FEB24C', '#FD8D3C', '#F03B20', '#BD0026'
fig, ax = plt.subplots(figsize=(6, 4))
x_axis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y_axis = [0, 3, 6, 9, 12, 15, 18, 21]
z2 = z_data.reshape((len(x_axis), len(y_axis)))
x, y = np.meshgrid(x_axis, y_axis)
img = plt.contourf(x, y, z2.T, origin='image', levels=np.arange(5, 50, 5), colors=plotcolors),
extend='both')
plt.grid(b=True, which='both', color='gray', linestyle='dashdot')
yticks = [0, 3, 6, 9, 12, 15, 18, 21]
ax.set_yticks(yticks)
ax.set_ylabel('Hour (UTC)')
positions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
labels = ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D']
ax.xaxis.set_major_locator(ticker.FixedLocator(positions))
ax.xaxis.set_major_formatter(ticker.FixedFormatter(labels))
plt.colorbar(mappable=plt.gci(), pad=.15, orientation='horizontal',extend='both', aspect=35)
plt.tight_layout()
plt.show()
Is this the right way to create the image for those months and hours and data?
I had to use the '.T' on the z2 data to orient the image correctly.
The data is in a Pandas dataframe.
The data:
MONTH,HOUR,CIG3000V3
1,0,23.1
1,3,22.6
1,6,24.6
1,9,26.2
1,12,27.3
1,15,27.7
1,18,26.7
1,21,26.3
2,0,20.8
2,3,18.2
2,6,19.6
2,9,20.3
2,12,24
2,15,26.4
2,18,26.1
2,21,22.8
3,0,20.3
3,3,20.7
3,6,23.4
3,9,24.6
3,12,28
3,15,29.1
3,18,26.5
3,21,20.6
4,0,16
4,3,15.6
4,6,15.9
4,9,18.3
4,12,21.3
4,15,24.9
4,18,21.9
4,21,17.7
5,0,11.2
5,3,11.8
5,6,18
5,9,21.4
5,12,24.1
5,15,26.1
5,18,19.8
5,21,11.7
6,0,3.2
6,3,3.8
6,6,4.6
6,9,6
6,12,10.5
6,15,9.6
6,18,8.4
6,21,4
7,0,3.5
7,3,2.1
7,6,2.2
7,9,4.3
7,12,5.3
7,15,6
7,18,5.4
7,21,3.7
8,0,4.1
8,3,4.2
8,6,6.2
8,9,8.8
8,12,12.5
8,15,12.7
8,18,10.3
8,21,5.5
9,0,5.7
9,3,6.4
9,6,9.3
9,9,10.9
9,12,16.5
9,15,15.3
9,18,12.3
9,21,8.6
10,0,14.3
10,3,14.8
10,6,16.9
10,9,18.9
10,12,21.2
10,15,19.6
10,18,18.4
10,21,15.8
11,0,13.9
11,3,14.1
11,6,14.8
11,9,14.7
11,12,15.7
11,15,17.7
11,18,17.2
11,21,13.2
12,0,22.6
12,3,24.2
12,6,26.4
12,9,26.4
12,12,28.8
12,15,29.5
12,18,27.3
12,21,26.3