So I'm creating contour plots of electric fields for some school work, and the variations are pretty extreme (1e-6 to 1e+8), and I want to show the contour in a logarithmic scale. The issue is that there are both positive and negative electric fields, and the Matplotlib.Ticker.LogLocator destroys the negative values, and I want all the values plotted on a continuous colormap. I cannot find any easy way to set a ticker to do semilog scaling and other solutions have been eluding me.
Here is my code thus far:
def plotEField2D(data_in, ax, fig, field='Ey', xlim=100, ylim=100, num_levels=20,
contours=False, num_lines=10):
electric_data = data_in.electric
ax.set_xlim(-xlim, xlim)
ax.set_ylim(-ylim, ylim)
ax.set_title('2D ' + field + ' Contour')
ax.set_xlabel('x (nm)')
ax.set_ylabel('y (nm)')
e_data = pd.pivot_table(electric_data[['x','y', field]],
values=field, index='y', columns='x')
e_data = e_data.interpolate().fillna(0)
mymin = np.min(e_data.values)
mymax = np.max(e_data.values)
e = ax.contourf(e_data.columns, e_data.index, e_data.values, num_levels,
cmap=plt.cm.coolwarm, locator=matplotlib.ticker.LogLocator(),
norm=matplotlib.colors.SymLogNorm(linthresh=1000, linscale=0.1, vmin=mymin,
vmax=mymax), vmin=mymin, vmax=mymax)
fig.colorbar(e, ax=ax, format='%.0e', extend='both')
if contours:
e = ax.contour(e_data.columns, e_data.index, e_data.values,
num_lines, colors='k', linewidths=1)
I'm attempting to implement the SymLogNorm in my Contourf plot, but, as you can see in the image, the contourf is screening out values less than 0 even though I'm passing in the SymLogNorm parameter. I'm not super familiar with SymLogNorm, so I'm hoping it's something basic that I'm just missing.
Why is the SymLogNorm not creating the color gradient that extends into the negative like in this example? I've checked that mymin and mymax are negative and positive respectively, so I know it's not that.
Thanks for the assistance!