I'm trying to save an image I generated using seaborn. The image is 4x4 confusion matrix ('confmat' np.array). I learned that when I save the image in vector format, certain viewers have issues resulting in white lines on colorbar, quoting from matplotlib reference:
It is known that some vector graphics viewer (svg and pdf) renders
white gaps between segments of the colorbar. This is due to bugs in
the viewers not matplotlib. As a workaround the colorbar can be
rendered with overlapping segments:
cbar = colorbar()
cbar.solids.set_edgecolor("face")
draw()
However, I have trouble doing what is suggested.
Here is what I did:
import seaborn as sns
import matplotlib.pyplot as plt
cmap=plt.cm.Blues
fig, ax = plt.subplots()
ax = sns.heatmap(confmat, annot=True, cmap=cmap)
ax.set_title('title')
ax.tick_params(
axis='both', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom='off', # ticks along the bottom edge are off
top='off', # ticks along the top edge are off
labelbottom='off', # labels along the bottom edge are off
labelleft='off',
right='off')
fig.savefig('confusion_matrix.svg', format='svg')
I tried to get colorbar using
cbar = ax.colorbar()
But get an error AttributeError: 'AxesSubplot' object has no attribute 'colorbar'.
I searched for solution and found a few questions here that suggest using plt.imshow() to get the colorbar object, but I'm completely confused about what I'm doing by now.
Can someone suggest, and if possible, explain why, the solution for implementing what matplotlib documentation has offered for colorbar?
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
x = np.random.randn(10, 10)
f, ax = plt.subplots()
sns.heatmap(x)
cbar_ax = f.axes[-1]
cbar_solids = cbar_ax.collections[0]
cbar_solids.set_edgecolor("face")
f.savefig("heatmap.svg")
Changing a colorbar with cb.solid.set_edgecolor("face") as suggested in the matplotlib docs appears to be a bit of a hack to ensure there are no white lines between elements on the colorbar. I think seaborn is designed assuming you should be able to do everything you need by passing kwargs (cbar_kws in heatmap). For example, you can pass cb_kwargs to the sns.heatmap function cbar_kws={"drawedges": "False"} but unfortunately this doesn't fix the problem.
As the Seaborn Heatmap only returns an axis handle on which the heatplot and the colorbar are plotted, you don't have direct access to the mappable object, cbar in the source code. As a result you can't apply this hack.
One solution is to just plot this using pcolormesh and colorbar. I think seaborn actually redefines matplotlib styles so should look the same,
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
cmap=plt.cm.Blues
fig, ax = plt.subplots()
confmat = np.random.rand(4, 4)
cb = ax.pcolormesh(confmat, cmap=cmap)
ax.set_title('title')
ax.tick_params(
axis='both', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom='off', # ticks along the bottom edge are off
top='off', # ticks along the top edge are off
labelbottom='off', # labels along the bottom edge are off
labelleft='off',
right='off')
cbar = plt.colorbar(cb)
cbar.solids.set_edgecolor("face")
plt.draw()
fig.savefig('confusion_matrix.svg', format='svg')
The result for me looks to be rid of the white lines when you zoom in.
Related
I want to draw multiple bar plots with the same y-scale, and so I need the y-scale to be consistent.
For this, I tried using ylim() after yscale()
plt.yscale("log")
plt.ylim(top=2000)
plt.show()
However, python keeps autoscaling the intermittent values depending on my data.
Is there a way to fix this?
overlayed graphs
import numpy as np
import matplotlib.pyplot as plt
xaxis = np.arange(10)
yaxis = np.random.rand(10)*100
fig = plt.subplots(figsize =(10, 7))
plt.bar(xaxis, yaxis, width=0.8, align='center', color='y')
# show graph
plt.yscale("log")
plt.ylim(top=2000)
plt.show()
You can set the y-axis tick labels manually. See yticks for an example. In your case, you will have to do this for each plot to have consistent axes.
I am using custom colorbar in my plot with following code
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
cmap = matplotlib.cm.get_cmap("hot")
norm = matplotlib.colors.Normalize(vmin=0, vmax=10)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
fig = plt.figure()
gs = GridSpec(10, 10, figure=fig)
ax = fig.add_subplot(gs[1:, :])
colorbar_ax = fig.add_subplot(gs[0, :])
plt.colorbar(cax=colorbar_ax, mappable=sm, orientation="horizontal",
shrink=0.5)
plt.tight_layout()
plt.show()
This gives me following output,
For some other aspects of my other code, I HAVE to use gridspec. How can I shrink the colorbar by half (or any other fraction)? shrink=0.5 or fraction=0.5 are not working.
From the documentation for colorbar:
The shrink kwarg provides a simple way to scale the colorbar with
respect to the axes. Note that if cax is specified, it determines the
size of the colorbar and shrink and aspect kwargs are ignored.
So you can't use shrink if you are using gridspec.
Of course, gridspec is designed to make sizing of subplot axes easy, so we can use that to define the colorbar axes size. Since you already have a 10x10 grid defined by gridspec, we could just use the middle portion in the x-direction; For example, you could change to something like this to shorten your colorbar:
colorbar_ax = fig.add_subplot(gs[0, 3:7])
I am trying to draw a big heatmap with sns.heatmap function. However, since the map is too big, it's a little hard to find the xtick label or ytick label with corresponding rows and columns. Can I add the xtick and xlabels also on the top and ytick and ylabels also on the right??
I have tried many different ways. But they all didn't work.
The usual way would be via tick_params, which has the labelrotation parameter, and accepts rotation:
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
ax.tick_params(right=True, top=True, labelright=True, labeltop=True, labelrotation=0)
plt.show()
Without labelrotation=0 or rotation=0
Axis ticks for all sides can be placed using tick_params from matplotlib
#Get sample correlation data to plot something. 'data' is a dataframe
corr=data.corr()
#Create Heatmap
axr = sns.heatmap(corr,cmap="coolwarm", annot=True, linewidths=.5,cbar=False)
#Set all sides
axr.tick_params(right=True, top=True, labelright=True, labeltop=True,rotation=0)
#Rotate X ticks
plt.xticks(rotation='vertical')
When plotting matrix with imshow in Matplotlib, how to change colorbar legend bar size, location, font and other parameters?
Here I created an example code
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
def plot_matrix(mat, title='example', cmap=plt.cm.Blues):
plt.imshow(mat, interpolation='nearest', cmap=cmap)
plt.grid(False)
plt.title(title)
plt.colorbar()
data = np.random.random((20, 20))
plt.figure(figsize=(8,8))
plt.tick_params(axis='both', which='major', labelsize=12)
plot_matrix(data)
In a real use case, I got complex labels and the legend bar becomes much higher then the matrix itself. I want to change the legend bar to make the plot more efficiently use the space.
I found a documentation for the matplotlib.pyplot.colorbar, however have not figure out a good way to set the size, location and font size for the color legend bar.
imshow enforces a 1:1 aspect (by default, but you can change it with aspect parameter), which makes things a little trickier. To always get consistent result, I might suggest manually specify the size of axes:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
def plot_matrix(mat, figsize, title='example', cmap=plt.cm.Blues):
f = plt.figure(figsize=figsize)
ax = plt.axes([0, 0.05, 0.9, 0.9 ]) #left, bottom, width, height
#note that we are forcing width:height=1:1 here,
#as 0.9*8 : 0.9*8 = 1:1, the figure size is (8,8)
#if the figure size changes, the width:height ratio here also need to be changed
im = ax.imshow(mat, interpolation='nearest', cmap=cmap)
ax.grid(False)
ax.set_title(title)
cax = plt.axes([0.95, 0.05, 0.05,0.9 ])
plt.colorbar(mappable=im, cax=cax)
return ax, cax
data = np.random.random((20, 20))
ax, cax = plot_matrix(data, (8,8))
Now you have the axis where the colorbar is plotted in, cax. You can do a lot of thing with that, say, rotate the labels, using plt.setp(cax.get_yticklabels(), rotation=45)
I am trying to produce a scatter plot that has two different y-axes and also a colorbar.
Here is the pseudo-code used:
#!/usr/bin/python
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax1 = fig.add_subplot(111)
plt.scatter(xgrid,
ygrid,
c=be, # set colorbar to blaze efficiency
cmap=cm.hot,
vmin=0.0,
vmax=1.0)
cbar = plt.colorbar()
cbar.set_label('Blaze Efficiency')
ax2 = ax1.twinx()
ax2.set_ylabel('Wavelength')
plt.show()
And it produces this plot:
My question is, how do you use a different scale for the "Wavelength" axes, and also, how do you move the colorbar more to right so that it is not in the Wavelength's way?
#OZ123 Sorry that I took so long to respond. Matplotlib has extensible customizability, sometimes to the point where you get confused to what you are actually doing. Thanks for the help on creating separate axes.
However, I didn't think I needed that much control, and I ended up just using the PAD keyword argument in
fig.colorbar()
and this provided what I needed.
The pseudo-code then becomes this:
#!/usr/bin/python
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax1 = fig.add_subplot(111)
mappable = ax1.scatter(xgrid,
ygrid,
c=be, # set colorbar to blaze efficiency
cmap=cm.hot,
vmin=0.0,
vmax=1.0)
cbar = fig.colorbar(mappable, pad=0.15)
cbar.set_label('Blaze Efficiency')
ax2 = ax1.twinx()
ax2.set_ylabel('Wavelength')
plt.show()
Here is to show what it looks like now::
the plt.colorbar() is made for really simple cases, e.g. not really thought for a plot with 2 y-axes.
For a fine grained control of the colorbar location and properties you should almost always rather work with colorbar specifying on which axes you want to plot the colorbar.
# on the figure total in precent l b w , height
cbaxes = fig.add_axes([0.1, 0.1, 0.8, 0.05]) # setup colorbar axes.
# put the colorbar on new axes
cbar = fig.colorbar(mapable,cax=cbaxes,orientation='horizontal')
Note that colorbar takes the following keywords:
keyword arguments:
cax
None | axes object into which the colorbar will be drawn ax
None | parent axes object from which space for a new
colorbar axes will be stolen
you could also see here a more extended answer of mine regarding figure colorbar on separate axes.