0

I am python beginner and I have one problem I can't solve. I need to make a 2d hystogram using matrix I got in txt, also I can get it in xls. Example of matrix:

4.52   4.54   4.52   4.44   4.34   4.28
5.10   4.92   4.82   4.80   4.66   4.44
6.12   5.80   5.57   5.50   5.15   4.70 
6.47   6.54   6.27   6.13   6.21   5.97
8.11   8.73   8.70   8.63   8.84   8.55

I can get it in code:

a = np.loadtxt('matrix.txt')

And then I will have it. So I have a part of code, which make a hystogram like an example, but I don't understand how integrate my matrix in here:

import matplotlib.pyplot as plt

n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

xedges = np.linspace(-4, 4, 42)
yedges = np.linspace(-25, 25, 42)

hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
xidx = np.clip(np.digitize(x, xedges), 0, hist.shape[0]-1)
yidx = np.clip(np.digitize(y, yedges), 0, hist.shape[1]-1)
c = hist[xidx, yidx]
plt.scatter(x, y, c=c)

plt.show()

With the help of this code I want to make a hystogram using my matrix, but I don't understand how. I will be thankful.

1 Answers1

2

The easiest way to display your matrix is via seaborn's heatmap. It would look like:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

a = np.array([[4.52, 4.54, 4.52, 4.44, 4.34, 4.28],
              [5.10, 4.92, 4.82, 4.80, 4.66, 4.44],
              [6.12, 5.80, 5.57, 5.50, 5.15, 4.70],
              [6.47, 6.54, 6.27, 6.13, 6.21, 5.97],
              [8.11, 8.73, 8.70, 8.63, 8.84, 8.55]])
sns.heatmap(a, annot=True, fmt='.2f', square=True)
plt.show()

resulting plot

As the values aren't integers nor are they floats that sum to 1, a doesn't seem to be a histogram.

An alternative is to create a 3D plot:

import matplotlib.pyplot as plt
import numpy as np

a = np.array([[4.52, 4.54, 4.52, 4.44, 4.34, 4.28],
              [5.10, 4.92, 4.82, 4.80, 4.66, 4.44],
              [6.12, 5.80, 5.57, 5.50, 5.15, 4.70],
              [6.47, 6.54, 6.27, 6.13, 6.21, 5.97],
              [8.11, 8.73, 8.70, 8.63, 8.84, 8.55]])
fig = plt.figure(figsize=plt.figaspect(0.5))
ax = fig.add_subplot(1, 1, 1, projection='3d')

xedges = np.arange(a.shape[1] + 1)
yedges = np.arange(a.shape[0] + 1)

# Construct arrays for the anchor positions of the 30 bars.
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25, indexing="ij")
xpos = xpos.ravel()
ypos = ypos.ravel()
zpos = 0

# Construct arrays with the dimensions for the 30 bars.
dx = dy = 0.5 * np.ones_like(zpos)
dz = a.ravel()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average', color='turquoise')

plt.show()

3d bar plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thanks, I made heatmap and now I need to save it as png 1920*1080 without white space and measuring scale, but my code don't work. `def plot(data, save_path): plt.imshow(data, cmap='jet') plt.savefig(save_path) plt.close() if __name__ == '__main__': w = 1920 h = 1080 data = np.zeros(h * w) data = data.reshape((h, w)) data = np.loadtxt('Final_redacted.txt') data = filters.gaussian_filter(data, sigma=15) plot(data, 'heatmap.png') ` –  Feb 04 '21 at 21:18
  • See [Removing white space around a saved image in matplotlib](https://stackoverflow.com/questions/11837979/removing-white-space-around-a-saved-image-in-matplotlib). Try to avoid `'jet'` as it [creates highlights at the wrong places](http://jakevdp.github.io/blog/2014/10/16/how-bad-is-your-colormap/). Without test-data it is hard to know what is going wrong with your example code. Please don't add code into the comments, instead [edit](https://stackoverflow.com/posts/66035866/edit) the question. – JohanC Feb 04 '21 at 21:28