Question

I'm trying to edit the linewidths that show up in my colorbar separately from the linewidths of the contours I am plotting in Matplotlib. I would like to set my contour linewidths to 0.5, but when I do I cannot see the color contours in the colorbar. If I set the contour linewidths to 1.5, I can see them in the color bar, but the contours are too thick for me. plots of each case

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np

#get data
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
#get contour levels
levels=[-1.5,-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25,1.5]
fig = plt.figure()
ax = fig.add_subplot(111)
#plot contours and color bar
CS = plt.contour(X,Y,Z,levels, linestyles='solid', linewidths=0.5, extent=(-3,3,-3,3))  
plt.clabel(CS, colors='black', inline=True, inline_spacing=0, fontsize=8)
CB = plt.colorbar(CS, aspect=35, shrink=0.5, pad=0.09, orientation='horizontal', extend='both')
CB.set_ticks(levels)
CB.set_label('(values)',size=8)
CB.ax.tick_params(labelsize=6) 
#set plot limits
plt.xlim([-3,3])
plt.ylim([-3,3])
#set aspect ratios to be equal
plt.axes().set_aspect('equal')
#set ticks of plot
ax.xaxis.set_major_locator(plt.MultipleLocator(1.0))
ax.yaxis.set_major_locator(plt.MultipleLocator(1.0))
plt.xticks(fontsize=8)
plt.yticks(fontsize=8)

plt.show()

Any ideas on how I can control the contour linewidths and the colorbar linewidths separately?

Was it helpful?

Solution

Use the linewidth you want for CS (i.e 0.5). Then add this line:

CB.lines[0].set_linewidth(10)

And you will get thick lines in the colorbar.
This gives the same result than the method the OP realized in the comments but you do not need to get the axes childrens and to discover which is the object you have to set linewithds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top