Seaborn heatmap fit annotation text to cell - python

I have this code that displays a confusion matrix. In each cell, first the accuracy is displayed then under it the number of correct predicted samples/total samples. Now I want to display all the text inside each cell. First cell for exemple should displays 186/208 under the accuracy.
How can I show the whole text of annotations inside the cell ? I tried to reduce the font size but it did not work.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def cm_analysis(cm, labels, figsize=(20,15)):
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float)
annot = np.empty_like(cm).astype(str)
nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = '%.2f%%\n%d/%d' % (p, c, s)
elif c == 0:
annot[i, j] = ''
else:
annot[i, j] = '%.2f%%\n%d' % (p, c)
cm = pd.DataFrame(cm, index=labels, columns=labels)
cm.index.name = 'Groundtruth labels'
cm.columns.name = 'Predicted labels'
fig, ax = plt.subplots(figsize=figsize)
ax.axhline(color='black')
g =sns.heatmap(cm, cmap="BuPu", annot_kws={"weight": "bold"}, annot=annot, fmt='', ax=ax, cbar_kws={'label': 'Number of samples'}, linewidths=0.1, linecolor='black')
g.set_xticklabels(g.get_xticklabels(), rotation = 45)
sns.set(font_scale=1.1)
plt.savefig("filename.png")
normalised_confusion_matrix = np.array(
[[186,3,0,1,2,0,3,3,7,1,2,0,0],
[5,9,1,0,3,0,0,0,0,0,0,0,1],
[0,0,49,3,0,0,0,0,1,0,0,0,6],
[1,0,6,89,0,0,0,0,1,1,1,0,1],
[3,7,0,0,50,0,0,0,6,0,1,0,0],
[1,0,0,0,0,9,0,1,0,0,0,0,0],
[3,0,1,0,0,0,54,0,0,0,3,0,0],
[2,0,0,0,0,0,2,7,0,0,0,0,0],
[3,0,0,0,2,1,2,0,53,2,4,0,0],
[0,0,0,1,0,1,0,0,1,7,0,1,0],
[1,1,0,0,1,0,1,0,3,0,52,0,0],
[1,0,0,0,0,0,0,0,1,0,0,5,0],
[0,0,11,2,0,0,0,0,0,0,0,0,26]]
)
classes = ['Assemble system','Consult sheets','Picking in front','Picking left','Put down component','Put down measuring rod','Put down screwdriver','Put down subsystem','Take component','Take measuring rod','Take screwdriver','Take subsystem','Turn sheets']
cm_analysis(cm= normalised_confusion_matrix, labels = classes)

The principal problem is creating the annot array as type str instead of object (so, annot = np.empty_like(cm).astype(object)). Having it of type str leads to strange errors, as numpy strings have some maximum length built-in. (See also this post.)
As you only use one index in cm_sum[i], it's better to not "keep the dimensions" in cm_sum = np.sum(cm, axis=1, keepdims=False) (docs).
Also, note that for a percentage, you need to multiply with 100. (The modern way to created formatted strings would use f-strings: annot[i, j] = f'{p*100:.2f}%\n{c}/{s}').
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def cm_analysis(cm, labels, figsize=(20, 15)):
cm_sum = np.sum(cm, axis=1, keepdims=False)
cm_perc = cm / cm_sum.astype(float)
annot = np.empty_like(cm).astype(object)
nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = f'{p*100:.1f}%\n{c}/{s}'
elif c == 0:
annot[i, j] = ''
else:
annot[i, j] = f'{p*100:.1f}%\n{c}'
cm = pd.DataFrame(cm, index=labels, columns=labels)
cm.index.name = 'Groundtruth labels'
cm.columns.name = 'Predicted labels'
fig, ax = plt.subplots(figsize=figsize)
ax.axhline(color='black')
g = sns.heatmap(cm, cmap="BuPu", annot_kws={"weight": "bold"}, annot=annot, fmt='', ax=ax,
cbar_kws={'label': 'Number of samples'}, linewidths=0.1, linecolor='black')
g.set_xticklabels(g.get_xticklabels(), rotation=45)
sns.set(font_scale=1.1)
plt.savefig("filename.png")
normalised_confusion_matrix = np.array(
[[186, 3, 0, 1, 2, 0, 3, 3, 7, 1, 2, 0, 0],
[5, 9, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 49, 3, 0, 0, 0, 0, 1, 0, 0, 0, 6],
[1, 0, 6, 89, 0, 0, 0, 0, 1, 1, 1, 0, 1],
[3, 7, 0, 0, 50, 0, 0, 0, 6, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 9, 0, 1, 0, 0, 0, 0, 0],
[3, 0, 1, 0, 0, 0, 54, 0, 0, 0, 3, 0, 0],
[2, 0, 0, 0, 0, 0, 2, 7, 0, 0, 0, 0, 0],
[3, 0, 0, 0, 2, 1, 2, 0, 53, 2, 4, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 1, 7, 0, 1, 0],
[1, 1, 0, 0, 1, 0, 1, 0, 3, 0, 52, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 5, 0],
[0, 0, 11, 2, 0, 0, 0, 0, 0, 0, 0, 0, 26]]
)
classes = ['Assemble system', 'Consult sheets', 'Picking in front', 'Picking left', 'Put down component',
'Put down measuring rod', 'Put down screwdriver', 'Put down subsystem', 'Take component',
'Take measuring rod', 'Take screwdriver', 'Take subsystem', 'Turn sheets']
cm_analysis(cm=normalised_confusion_matrix, labels=classes)

Related

Plot csv file taking the first column as x axis and the others as y axis once more

Can you please elaborate more how to use the solution of here once more? I have the same problem, I want to use the first column as the x axis and the following columns as y axis values. My code currently looks like this.
But basically I want to have it look like a scatter plot, with the values on each x value.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv(
"ORBGRAND_Hamming_4LWmax_63storeLWsuccess", sep=", ")
[plt.plot(data[0], data[x]) for x in range(1, len(filecsv[:, 0]))]
plt.grid(True)
plt.suptitle("(63,45) BCH", fontsize=40)
plt.grid(True)
plt.yscale('log')
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.legend(loc='lower left')
plt.xlabel('$E_b/N_0$ (dB)', fontsize=20)
plt.ylabel('BLER', fontsize=20)
plt.legend(loc='lower left', prop={'size': 17})
plt.show()
My file looks like this:
0.000000, 0, 2, 0, 0, 1, 0, 0, 3, 0, 1
0.500000, 1, 0, 3, 3, 1, 0, 0, 3, 2, 1
1.000000, 4, 5, 0, 0, 0, 0, 0, 0, 0, 1
1.500000, 0, 0, 1, 3, 1, 0, 2, 0, 0, 0
2.000000, 1, 0, 0, 1, 0, 3, 0, 0, 0, 0
2.500000, 0, 0, 0, 0, 5, 1, 0, 1, 0, 1
3.000000, 3, 0, 1, 2, 0, 0, 0, 0, 1, 0
3.500000, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0
4.000000, 2, 2, 0, 0, 0, 0, 3, 0, 0, 0
4.500000, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0
5.000000, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0
5.500000, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
6.000000, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0
I get the following error:
Expected 85 fields in line 3, saw 88. Error could possibly be due to quotes being ignored when a multi-char delimiter is used.
This creates a scatter plot with the first column as x against all the following columns.
As commented above, your error is not from the plotting itself.
import pandas as pd
import matplotlib.pyplot as plt
# This is just a subset of your posted data
data = [[0.000000, 0, 2, 0, 0, 1, 0, 0, 3, 0, 1],
[0.500000, 1, 0, 3, 3, 1, 0, 0, 3, 2, 1],
[1.000000, 4, 5, 0, 0, 0, 0, 0, 0, 0, 1],
[1.500000, 0, 0, 1, 3, 1, 0, 2, 0, 0, 0],
[2.000000, 1, 0, 0, 1, 0, 3, 0, 0, 0, 0]]
# build the dataframe
column_names = [f'y{i}' for i in range(1, len(data[0]))]
column_names[0] = "x"
df = pd.DataFrame(data, columns=["x"] + column_names)
# You didn't specify what plot, I assumed you want all in one figure?
# This plots all the values (y) to the same x colum
plt.figure()
for y in column_names:
plt.scatter(df["x"], df[y], label=y)
plt.legend()
# Add your aesthetics here

How to fix hopfield library errors in python

I have some code about hopfield network. I am trying to train some letters with hebbian training and then add noise and denoise them. However, I have problem with libraries. I did a search but I cant find the right answer to my problem. Hopfieldnet library is not working and if I try to replace it I take other errors. Can u help me?
from random import randint
import numpy as np
import HopfieldNetwork
from hopfieldnet.trainers import hebbian_training
from matplotlib import pyplot as plt
# Create the training patterns
j_pattern = np.array([[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[1, 0, 1, 0, 0],
[1, 1, 1, 0, 0]])
a_pattern = np.array([[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1]])
m_pattern = np.array([[1, 0, 0, 0, 1],
[1, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1]])
e_pattern = np.array([[1, 1, 1, 1, 1],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 1, 1, 1, 1]])
s_pattern = np.array([[1, 1, 1, 1, 1],
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
[1, 1, 1, 1, 1]])
j_pattern *= 2
j_pattern -= 1
a_pattern *= 2
a_pattern -= 1
m_pattern *= 2
m_pattern -= 1
e_pattern *= 2
e_pattern -= 1
s_pattern *= 2
s_pattern -= 1
input_patterns = np.array([j_pattern.flatten(), a_pattern.flatten(), m_pattern.flatten(), e_pattern.flatten(), s_pattern.flatten()])
# Create the neural network and train it using the training patterns
network = HopfieldNetwork(35)
hebbian_training(network, input_patterns)
# Create the test patterns by using the training patterns and adding some noise to them
# and use the neural network to denoise them
j_test = j_pattern.flatten()
for i in range(4):
p = randint(0, 34)
j_test[p] *= -1
j_result = network.run(j_test)
j_result.shape = (7, 5)
j_test.shape = (7, 5)
a_test = a_pattern.flatten()
for i in range(4):
p = randint(0, 34)
a_test[p] *= -1
a_result = network.run(a_test)
a_result.shape = (7, 5)
a_test.shape = (7, 5)
m_test = m_pattern.flatten()
for i in range(4):
p = randint(0, 34)
m_test[p] *= -1
m_result = network.run(m_test)
m_result.shape = (7, 5)
m_test.shape = (7, 5)
e_test = e_pattern.flatten()
for i in range(4):
p = randint(0, 34)
e_test[p] *= -1
e_result = network.run(e_test)
e_result.shape = (7, 5)
e_test.shape = (7, 5)
s_test = s_pattern.flatten()
for i in range(4):
p = randint(0, 34)
s_test[p] *= -1
s_result = network.run(s_test)
s_result.shape = (7, 5)
s_test.shape = (7, 5)
# Show the results
plt.subplot(3, 2, 1)
plt.imshow(j_test, interpolation="nearest")
plt.subplot(3, 2, 2)
plt.imshow(j_result, interpolation="nearest")
plt.subplot(3, 2, 3)
plt.imshow(a_test, interpolation="nearest")
plt.subplot(3, 2, 4)
plt.imshow(a_result, interpolation="nearest")
plt.subplot(3, 2, 5)
plt.imshow(m_test, interpolation="nearest")
plt.subplot(3, 2, 6)
plt.imshow(m_result, interpolation="nearest")
plt.subplot(3, 2, 7)
plt.imshow(e_test, interpolation="nearest")
plt.subplot(3, 2, 8)
plt.imshow(e_result, interpolation="nearest")
plt.subplot(3, 2, 9)
plt.imshow(s_test, interpolation="nearest")
plt.subplot(3, 2, 10)
plt.imshow(s_result, interpolation="nearest")
plt.show()
The error:
"C:/Users/chriss/PycharmProjects/untitled3/hopfield.py", line 3, in <module> import HopfieldNetwork ModuleNotFoundError: No module named 'HopfieldNetwork'
Not sure if it is actual solution to the problem. I cannot reproduce the error. Code works for me after 3 slight modifications.
(1) Imports
from random import randint
import numpy as np
from hopfieldnet.net import HopfieldNetwork # modified import
from hopfieldnet.trainers import hebbian_training
from matplotlib import pyplot as plt
(2) Drawing
plt.subplot(3, 4, ...) # 3 * 4 = 12 places for 10 plots
(3) hopfieldnet module -> net.py -> class HopfieldNetwork -> method run
update_list = list(range(self._num_inputs)) # list(range(...)) instead of just range(...)

Python matlibplot: visualizing a matrix of integers (-1, 0 or 1) with circles (same radius, different color)

I have a square matrix filled by -1, 0 or 1. I would like to visualize this matrix with spheres or circles of the same radius. Radius, indeed is not important at all. Those circles though, must have a different colour according to the number of the matrix cell.
For example:
10 x 10 matrix -> 100 circles on a plane, 10 rows x 10 columns
Color of circle in position (2,9) depending on number of matrix in position (2,9).
Thank you!
People I know told me to use matlibplot, but I am new to Python and
I have many issues!
This is what I did up to now:
{`
import numpy as np
#from implementations import *
#from config import *
import matplotlib.pyplot as plt
A = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])
rows=len(A) # finding number rows of lattice, so no need to worry about it!
columns=len(A[0]) # finding number columns of lattice, so no need to worry about it!
fig, ax = plt.subplots()
for i in range(rows):
for j in range(columns):
if A[i][j]==-1:
circle1 = plt.Circle((i*4, j*4), 2, color='blue')
fig = plt.gcf()
ax = fig.gca()
ax.add_artist(circle1)
if A[i][j]== 1:
circle2 = plt.Circle((i*4, j*4), 2, color='yellow')
fig = plt.gcf()
ax = fig.gca()
ax.add_artist(circle2)
`}
Here is the matplotlib code that uses scatter matrix:
# Imports
import matplotlib.pyplot as plt
from itertools import chain
# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# Here is our matrix. Note, that it is expected to be rectangular!
matrix = [
[0, 1, 0,1],
[-1,1, 0,0],
[1,-1,-1,1],
]
# Get X-length
X = len(matrix[0])
# Get Y-length
Y = len(matrix)
# Construct grids for scatter
x_grid = list(range(X)) * Y # 1,2,3,4,1,2,3,4...
y_grid = [y for y in range(Y) for _ in range(X)] # 1,1,1,1,2,2,2,2...
# Flatten the matrix because ax.scatter uses flat arrays
matrix_grid = list(chain(*matrix))
plt.scatter(
x_grid, # X-grid array of coordinates
y_grid, # Y-grid array of coordinates
c=matrix_grid, # Our flatten matrix of -1/0/1s
cmap='gist_rainbow' # Color map - defines colors
)
You can directly use a scatter as follows:
import numpy as np
import matplotlib.pyplot as plt
A = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])
X,Y = np.meshgrid(np.arange(A.shape[1]), np.arange(A.shape[0]))
plt.scatter(X.flatten(), Y.flatten(), c=A.flatten())
plt.show()

Plotting static Game of Life patterns in Python Matplotlib

There are a number of Python programs around for playing the classical 'Game of Life', and most of them offer nice graphic animations which allow one to follow the fantastic patterns emerging from the game. Such programs are fine for interactive use on a computer, but I have found very difficult to find one that provides static graphic output intended for printed pages (like those displayed in the 1970 article by Martin Gardner which presented the Game of Life to the world: The fantastic combinations of John Conway's new solitaire game "life"). There are many programs that offer text-based output of Life patterns, but I have not found any one capable of doing the same thing with Matplolib graphics. So, I started writing one, as shown in the code below:
import matplotlib.pyplot as plt
def iterate(Z):
shape = len(Z), len(Z[0])
N = [[0,]*(shape[0]+2) for i in range(shape[1]+2)]
# Compute number of neighbours for each cell
for x in range(1,shape[0]-1):
for y in range(1,shape[1]-1):
N[x][y] = Z[x-1][y-1]+Z[x][y-1]+Z[x+1][y-1] \
+ Z[x-1][y] +Z[x+1][y] \
+ Z[x-1][y+1]+Z[x][y+1]+Z[x+1][y+1]
# Update cells
for x in range(1,shape[0]-1):
for y in range(1,shape[1]-1):
if Z[x][y] == 0 and N[x][y] == 3:
Z[x][y] = 1
elif Z[x][y] == 1 and not N[x][y] in [2,3]:
Z[x][y] = 0
return Z
# The 'beehive' pattern
Z = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
n_generations = 10
fig, axes = plt.subplots(2, 5, figsize=(8, 8))
for i in range(n_generations):
iterate(Z)
ax = axes.flat[i]
ax.imshow(Z, interpolation='nearest', cmap=plt.cm.binary)
ax.set_axis_off()
ax.set_title('Generation {}'.format(i+1))
plt.grid(True)
plt.tight_layout()
plt.show()
This works, but it far from good, because:
(1) I would like that each plot showed grid lines, so that they could reproduce the original figures of Gardner's article, but I have not been able to found out how to do that;
(2) I would also like to be able to use spheres instead of squares for representing the living cells (just like they appear in Gardner's article);
Any help towards improving this code will be much appreciated!
To produce grid lines you need ticks at the respective positions. Using a MultipleLocator would produce ticks at multiples of 1.
Circles are standard scatter markers. You can plot the data as scatter instead of image.
Together it might look as follows, where I also made the code a bit more compact.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
def iterate(Z):
# http://www.labri.fr/perso/nrougier/from-python-to-numpy/code/game_of_life_numpy.py
N = (Z[0:-2, 0:-2] + Z[0:-2, 1:-1] + Z[0:-2, 2:] +
Z[1:-1, 0:-2] + Z[1:-1, 2:] +
Z[2: , 0:-2] + Z[2: , 1:-1] + Z[2: , 2:])
birth = (N == 3) & (Z[1:-1, 1:-1] == 0)
survive = ((N == 2) | (N == 3)) & (Z[1:-1, 1:-1] == 1)
Z[...] = 0
Z[1:-1, 1:-1][birth | survive] = 1
return Z
# The 'beehive' pattern
Z = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Z = np.array(Z)
X, Y = np.meshgrid(np.arange(Z.shape[1])+.5, np.arange(Z.shape[0])+.5)
fig, axes = plt.subplots(2, 5, figsize=(8, 4))
for i, ax in enumerate(axes.flat):
Z = iterate(Z)
ax.scatter(X[Z > 0], Y[Z > 0], color="k")
ax.grid(True, color="k")
ax.xaxis.set_major_locator(mticker.MultipleLocator())
ax.yaxis.set_major_locator(mticker.MultipleLocator())
ax.tick_params(size=0, length=0, labelleft=False, labelbottom=False)
ax.set(xlim=(0, Z.shape[1]), ylim=(Z.shape[0], 0),
title='Generation {}'.format(i+1), aspect="equal")
plt.tight_layout()
plt.show()
For the first problem, it's because you set axes off. I turned it on and make some tweaks:
n_generations = 10
fig, axes = plt.subplots(2, 5, figsize=(16, 8))
for i in range(n_generations):
iterate(Z)
ax = axes.flat[i]
ax.imshow(Z, interpolation='nearest', cmap=plt.cm.binary)
# ax.set_axis_off()
ax.set_xticks(np.arange(10)+.5)
ax.set_yticks(np.arange(10)+.5)
ax.set_xticklabels('')
ax.set_yticklabels('')
ax.set_title('Generation {}'.format(i+1))
plt.tight_layout()
plt.show()
Output:
For the other question. I don't think it's possible with imshow. You may need to write a custom function my_plot(Z, ax).

MatPlotLib printing out graphs on the same line next to each other

So I am making a program that reads in multiple two dimensional lists and plots them as step graph functions. I want to print out each set of graphs side by side like so (I made the graphs different colors just to differentiate the two):
Desired Output
However my code right now makes these two sets overlap each other instead, like so:
Actual Output
I believe it might have something to do with my "t" variable in plotPoints but I am not sure what I need to do. Any help would be greatly appreciated.
# supress warning message
import warnings; warnings.simplefilter("ignore")
# extension libraries
import matplotlib.pyplot as plt
import numpy as np
def plotPoints(bits, color):
for i in range(len(bits)):
data = np.repeat(bits[i], 2)
t = 0.5 * np.arange(len(data))
plt.step(t, data + i * 3, linewidth=1.5, where='post', color=color)
# Labels the graphs with binary sequence
for tbit, bit in enumerate(bits[i]):
plt.text(tbit + 0.3, 0.1 + i * 3, str(bit), fontsize=6, color=color)
def main():
plt.ylim([-1, 32])
set1 = [[0, 0, 0, 1, 1, 0, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [1, 1, 0, 0, 1, 0, 0, 0]]
set2 = [[1, 1, 1, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 0, 1, 1], [0, 0, 1, 1, 0, 1, 1, 1]]
plotPoints(set1, 'g')
plotPoints(set2, 'b')
# removes the built in graph axes and prints line every interation
plt.gca().axis('off')
plt.ylim([-1, 10])
plt.show()
main()
You can add some offset to t.
import matplotlib.pyplot as plt
import numpy as np
def plotPoints(bits, color, offset=0):
for i in range(len(bits)):
data = np.repeat(bits[i], 2)
t = 0.5 * np.arange(len(data)) + offset
plt.step(t, data + i * 3, linewidth=1.5, where='post', color=color)
# Labels the graphs with binary sequence
for tbit, bit in enumerate(bits[i]):
plt.text(tbit + 0.3 +offset, 0.1 + i * 3, str(bit), fontsize=6, color=color)
def main():
set1 = [[0, 0, 0, 1, 1, 0, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [1, 1, 0, 0, 1, 0, 0, 0]]
set2 = [[1, 1, 1, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 0, 1, 1], [0, 0, 1, 1, 0, 1, 1, 1]]
plotPoints(set1, 'g')
plotPoints(set2, 'b', offset=len(set1[0]))
# removes the built in graph axes and prints line every interation
plt.gca().axis('off')
plt.ylim([-1, 10])
plt.show()
main()

Categories