Python matplotlib drawing skills

1. Solve the phenomenon of inconsistent length and width of graphs from plt

import matplotlib.pyplot as plt
from pylab import * 
from matplotlib.ticker import MultipleLocator, FuncFormatter
z_show = np.zeros([17,101])
plt.imshow(z_show, origin='lower', interpolation='none')

write picture description here
plt.imshow displays the z image on the coordinate axis, but z_show is obviously inconsistent in length and width. How to make the length and width of the resulting image consistent? Add aspect='auto' to the parameters of imshow, the effect is as follows

plt.imshow(z_show, origin='lower', interpolation='none',aspect='auto')

write picture description here
At this time, you can also use the button of the picture to expand and contract, but you can't do it without adding aspect='auto' before. Without aspect='auto', these button operations will only translate the drawn image up, down, left and right, but it will not be done. Variation in length.
write picture description here

2、

z = np.random.random(size=(17, 101))
print(np.shape(z))
ax=plt.gca()
ax.set_yticks([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
ax.set_yticklabels([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
ax.set_xticks([0,10,20,30,40,50,60,70,80,90,100])
ax.set_xticklabels([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
plt.imshow(z, origin='lower', interpolation='none',aspect='auto')
plt.show()

set_yticks is to set the interval display in the y direction. The number here must correspond to the size of the matrix z_show, such as
ax.set_yticks([0,1,2,3,4,5,6,7,8,9,10 ,11,12,13,14,15,16,17])
The effect at this time is the same as ax.set_yticks([0,1,2,3,4,5,6,7,8,9,10,11, 12, 13, 14, 15, 16]) are the same, indicating what interval you want to divide, but ax.set_yticklabels is to set a label for each interval to express what you want to express. For example, the interval flag is set to a series of decimals below, but in fact the x direction is divided according to 0, 10, ..., 100.
write picture description here

3.
If there is a big difference between the statistical information and it does not look good on the graph, convert it to the log space. For log(0)=-inf, plt.imshow() directly displays it as blank.
4. Add other additional information, such as label, caption, etc.

plt.title('statistic ')
plt.xlabel('a')
plt.ylabel('b')

Off topic, storage is an array representation of the c type, and the array representation in lua can be done through table.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325741355&siteId=291194637