1

I am using the following code to contour plot some data using contourf in matplotlib. I have set the transparency of the colourbar to 0.6, but there are annoying lines between each colour interval that I cant get rid of. There doesnt seem to be a way to set linestyle in contourf, any ideas?

#instantiating and titiling the figure
fig, ax1 = plt.subplots(figsize=(7,5))
fig.suptitle('Testing Simple Neural Networks', y=0.96, fontsize=16, fontweight='bold');

#defining colour tables
cm = plt.cm.coolwarm

#plotting the contour plot
levels = np.linspace(0, 1, 25)
cont1 = ax1.contourf(p1_mesh, p2_mesh, y_mesh, levels=levels, cmap=cm, alpha=0.6, linewidths=10)

#plotting the entire dataset - training and test data. 
scat1 = ax1.scatter(X['p1'], 
                    X['p2'], 
                    c=y,
                    cmap=cm,
                    edgecolors='k');

#setting axis and legend
ax1.set(ylabel='p2',
        xlabel='p1',
        xlim=(0,255),
        ylim=(0,255));
ax1.legend(*scat1.legend_elements(), title='Target');
ax1.set_axisbelow(True)
ax1.grid(color='xkcd:light grey')
cbar = fig.colorbar(cont1)

enter image description here

  • Try remove `linewidths=10` from `ax1.contourf()`. – swatchai Feb 26 '21 at 16:05
  • Yeh I actually put that in as a check to see whether it was making any difference to those lines which it wasn't unfortunately. As I later checked in the [documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.html), `linewidths` and `linestyles` only effects `contour` not `contourf`. – Matt_Haythornthwaite Feb 26 '21 at 19:01

1 Answers1

2

You can add the option antialiased=True to ax1.contourf, it should fix it.

bb1
  • 7,174
  • 2
  • 8
  • 23
  • Works only if alpha=0, unfortunately does not solve for alpha < 1. [Related discussion](https://stackoverflow.com/questions/8263769/hide-contour-linestroke-on-pyplot-contourf-to-get-only-fills) – Irigi Jul 16 '22 at 15:00