How can I plot a confusion matrix? [duplicate]

you can use plt.matshow() instead of plt.imshow() or you can use seaborn module’s heatmap (see documentation) to plot the confusion matrix import seaborn as sn import pandas as pd import matplotlib.pyplot as plt array = [[33,2,0,0,0,0,0,0,0,1,3], [3,31,0,0,0,0,0,0,0,0,0], [0,4,41,0,0,0,0,0,0,0,1], [0,1,0,30,0,6,0,0,0,0,1], [0,0,0,0,38,10,0,0,0,0,0], [0,0,0,3,1,39,0,0,0,0,4], [0,2,2,0,4,1,31,0,0,0,2], [0,1,0,0,0,0,0,36,0,2,0], [0,0,0,0,0,0,1,5,37,5,1], [3,0,0,0,0,0,0,0,0,39,0], [0,0,0,0,0,0,0,0,0,0,38]] df_cm = pd.DataFrame(array, index = [i for i in … Read more

matplotlib: colorbars and its text labels

import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import ListedColormap #discrete color scheme cMap = ListedColormap([‘white’, ‘green’, ‘blue’,’red’]) #data np.random.seed(42) data = np.random.rand(4, 4) fig, ax = plt.subplots() heatmap = ax.pcolor(data, cmap=cMap) #legend cbar = plt.colorbar(heatmap) cbar.ax.get_yaxis().set_ticks([]) for j, lab in enumerate([‘$0$’,’$1$’,’$2$’,’$>3$’]): cbar.ax.text(.5, (2 * j + 1) / 8.0, lab, ha=”center”, va=”center”) … Read more

Saving interactive Matplotlib figures

I just found out how to do this. The “experimental pickle support” mentioned by @pelson works quite well. Try this: # Plot something import matplotlib.pyplot as plt fig,ax = plt.subplots() ax.plot([1,2,3],[10,-10,30]) After your interactive tweaking, save the figure object as a binary file: import pickle pickle.dump(fig, open(‘FigureObject.fig.pickle’, ‘wb’)) # This is for Python 3 – … Read more

figure of imshow() is too small

If you don’t give an aspect argument to imshow, it will use the value for image.aspect in your matplotlibrc. The default for this value in a new matplotlibrc is equal. So imshow will plot your array with equal aspect ratio. If you don’t need an equal aspect you can set aspect to auto imshow(random.rand(8, 90), … Read more

Moving x-axis to the top of a plot in matplotlib

Use ax.xaxis.tick_top() to place the tick marks at the top of the image. The command ax.set_xlabel(‘X LABEL’) ax.xaxis.set_label_position(‘top’) affects the label, not the tick marks. import matplotlib.pyplot as plt import numpy as np column_labels = list(‘ABCD’) row_labels = list(‘WXYZ’) data = np.random.rand(4, 4) fig, ax = plt.subplots() heatmap = ax.pcolor(data, cmap=plt.cm.Blues) # put the major … Read more

How to rotate x-axis tick labels in a pandas plot

Pass param rot=0 to rotate the xticklabels: import matplotlib matplotlib.style.use(‘ggplot’) import matplotlib.pyplot as plt import pandas as pd df = pd.DataFrame({ ‘celltype’:[“foo”,”bar”,”qux”,”woz”], ‘s1’:[5,9,1,7], ‘s2′:[12,90,13,87]}) df = df[[“celltype”,”s1″,”s2″]] df.set_index([“celltype”],inplace=True) df.plot(kind=’bar’,alpha=0.75, rot=0) plt.xlabel(“”) plt.show() yields plot:

Ubuntu running `pip install` gives error ‘The following required packages can not be built: * freetype’

No. pip will not install system-level dependencies. This means pip will not install RPM(s) (Redhat based systems) or DEB(s) (Debian based systems). To install system dependencies you will need to use one of the following methods depending on your system. Ubuntu/Debian: apt-get install libfreetype6-dev To search for packages on Ubuntu/Debian based systems: apt-cache search <string> … Read more