Python drawing skills (high-definition pictures)

1. Draw an ordinary multi-line diagram.

In addition to the regular style coordinate names and resizing etc., there are some special but necessary problems to be solved.

1. Note that the paintings we often save will leave a lot of blank space, which solves this problem

2. Chinese garbled characters

3. The image is blurred and the clarity is not enough.

import cv2
from pylab import *  # 支持中文
mpl.rcParams['font.sans-serif'] = ['SimHei']

img2 = cv2.imread("img/fenge-1-1.jpg")
img4 = cv2.imread("img/fenge-1-1-0.06.jpg")
img5 = cv2.imread("img/fenge-1-1-0.02.jpg")
img6 = cv2.imread("img/fenge-1-1-0.1.jpg")


img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
h1, s, img2 = cv2.split(img2)  # 看单通道的
img_array2 = np.array(img2)  # 把图像转成数组格式img = np.asarray(image)
mean2 = np.average(img_array2, axis=0)  # 按列求均值

img4 = cv2.cvtColor(img4, cv2.COLOR_BGR2HSV)
h1, s, img4 = cv2.split(img4)  # 看单通道的
img_array4 = np.array(img4)  # 把图像转成数组格式img = np.asarray(image)
mean4 = np.average(img_array4, axis=0)  # 按列求均值

img5 = cv2.cvtColor(img5, cv2.COLOR_BGR2HSV)
h1, s, img5 = cv2.split(img5)  # 看单通道的
img_array5 = np.array(img5)  # 把图像转成数组格式img = np.asarray(image)
mean5 = np.average(img_array5, axis=0)  # 按列求均值

img6 = cv2.cvtColor(img6, cv2.COLOR_BGR2HSV)
h1, s, img6 = cv2.split(img6)  # 看单通道的
img_array6 = np.array(img6)  # 把图像转成数组格式img = np.asarray(image)
mean6 = np.average(img_array6, axis=0)  # 按列求均值

# plt.plot(mean2,c="g",label=u'a=std/(255*2)')
# plt.plot(mean4,linestyle=":",c="r",label=u'a=0.06')
# plt.plot(mean5,linestyle="-.",c="b",label=u'a=0.02')
# plt.plot(mean6,linestyle="--",c="k",label=u'a=0.1')

plt.plot(mean2,linewidth=1,c="g",label=u'a=std/(255*2)')
plt.plot(mean4,linewidth=1,linestyle=":",c="r",label=u'a=0.06')
plt.plot(mean5,linewidth=1,linestyle="--",c="b",label=u'a=0.02')
plt.plot(mean6,linewidth=1,linestyle="-.",c="darkorange",label=u'a=0.1')


plt.legend(loc='upper right',prop={'family':'SimHei','size':9})  # 让图例生效
# plt.legend(loc='upper right',size=5)  # 让图例生效lower
plt.tick_params(direction='in')  # 刻度向里
plt.xlim([0, 255])  # 横坐标范围
plt.ylim([100, 150])  # 横坐标范围
plt.ylim([100, 150])  # 横坐标范围
# plt.grid(True)  ##增加格点
plt.axis('tight')  # 坐标轴适应数据量 axis 设置坐标轴
plt.xlabel("图像1宽度/(像素)", size=10)
plt.ylabel("列平均灰度值", size=10)

# plt.title("a不同取值的L*(x,y)的一维曲线图")  # 标题

#方法一
 # plt.rcParams['savefig.dpi'] = 800 #图片像素
# #
# # plt.rcParams['figure.dpi'] = 800 #分辨率
# #
# # # plt.axis('off')可以去坐标轴
# # plt.savefig('img/name-2.jpg')
# 方法二
#可以直接设置保存好的图的清晰度,大小
# plt.set_size_inches(6, 6)  #设置保存图片的尺寸
plt.savefig('img/name-1.jpg', dpi=800,bbox_inches='tight')

#bbox_inches使生成的图片周围的空白缩小
#在 plt.show() 之前调用 plt.savefig(),否则出现空白

plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

2. Note: Another way to set the scale direction:

import matplotlib.pyplot as plt

plt.rcParams['xtick.direction'] = 'in' # Set the direction of the scale line of the x week inward
plt.rcParams['ytick.direction'] = 'in' # Set the direction of the y-axis scale inward

plt.rcParams['xtick.direction'] = 'out' # Set the direction of the scale line of the x week outward
plt.rcParams['ytick.direction'] = 'out' # Set the direction of the scale line of the y-axis outward

plt.rcParams['xtick.direction'] = 'inout' # Set the direction of the scale line of the x week to the middle
plt.rcParams['ytick.direction'] = 'inout' # Set the direction of the y-axis scale to the middle

 3. Color style

Python matplotlib drawing (1. Line color, type setting)_qq_17576739's blog-CSDN blog_python drawing line thickness

 Fourth, draw a 3D map (show it)

import cv2
from pylab import *  # 支持中文
mpl.rcParams['font.sans-serif'] = ['SimHei']
from IPython.core.pylabtools import figsize # import figsize

img = cv2.imread("img/1-2.jpg", 0)
# print(img.shape)#可以查看是200*200 3通道还是单通道
cv2.imshow('img',img)
height, width= img.shape[:2]
print(height, width)

img = cv2.resize(img, (height,width))

xx, yy = np.mgrid[0:img.shape[0], 0:img.shape[1]]

# create the figure
fig = plt.figure()
ax = fig.gca(projection='3d')
# ax.invert_yaxis()
ax.invert_xaxis()

ax.plot_surface(xx, yy, img,rstride=1, cstride=1, cmap=plt.cm.gray,linewidth=0) #plt.cm.jet
ax.set_zlabel("图像灰度值")
ax.set_xlabel('图像2宽度/(像素)')
ax.set_ylabel('图像2高度/(像素)')
ax.set_ylim(bottom=0)#y轴从0开始
ax.set_zlim(bottom=0)#z轴从0开始
# plt.xlabel("图像1宽度/(像素)", size=10)
# plt.ylabel("图像1高度/(像素)", size=10)
# plt.tick_params(direction='in')  # 刻度向里
plt.rcParams['xtick.direction']='in'
# plt.rcParams['ytick.direction']='in'
# figsize(8, 4) # 设置 figsize
# plt.rcParams['savefig.dpi'] = 600 #图片像素

# plt.rcParams['figure.dpi'] = 600 #分辨率
# plt.savefig('img/3D-1-2.jpg')
#2、保存图片的大小
fig.set_size_inches(6, 6)  #设置保存图片的尺寸
plt.savefig('img/3D-1-2.jpg', dpi=600,bbox_inches='tight')
#bbox_inches使生成的图片周围的空白缩小
#在 plt.show() 之前调用 plt.savefig(),否则出现空白
plt.show()
cv2.waitKey()
cv2.destroyAllWindows()# 销毁所有窗口

There is still a difference between three-dimensional coordinates and two-dimensional coordinates.

 5. Application of double y-axis

The key code:

# plt.xticks(x, x, rotation=45)#横坐标刻度旋转45度
plt.xticks([])#不显示横坐标刻度
plt.xlim([0.5, 4.5]) #横坐标范围
plt.ylim([0, 1]) #坐标范围


plt.xlabel(u"原始图像              MSRCR              CALHE            本文算法")  # X轴标签
plt.title("不同算法的评价指标值的变化")  # 标题

# plt.plot(greyScale_map, color='r')
plt.tick_params(direction='in')  # 刻度向里

# 绘制另一Y轴
plt.twinx()
plt.plot(x, y1, marker='*',  mec='g', mfc='w', ms=10,label=u'信息熵曲线图')
plt.plot(x1, y4, marker='.', ms=10, label=u'PSNR曲线图')

Guess you like

Origin blog.csdn.net/m0_63172128/article/details/126268154