1.常用的一些技巧:
(1)调整图片大小:
from matplotlib import pyplot as plt
x = [i for i in range(0, 61)]
y = [random.randint(2,16) for i in range(0, 61)]
plt.figure(figsize=(6,4), dpi=100)
plt.plot(x,y)
plt.show()
figsize可以调整图像大小,dpi的值越大图像越清晰
(2)调整坐标轴显示的坐标数据密集度和坐标倾斜:
如图X坐标太过密集,可以通过plt.xticks()调整,rotation可以使坐标值倾斜
from matplotlib import pyplot as plt
x = [i for i in range(0, 61,1)]
y = [random.randint(2,16) for i in range(0, 61)]
plt.xticks(x[::5], rotation = 45)
plt.plot(x,y)
plt.show()
(3)显示汉字,matplotlib默认不显示汉字,font_manager可以显示汉字:
from matplotlib import pyplot as plt
from matplotlib import font_manager
import random
t = [random.randint(20,35) for i in range(120)]
per_minute = [i for i in range(0, 120)]
x_xticks = ["10点{}分".format(i) for i in range(60)]
x_xticks += ["11点{}分".format(i) for i in range(60)]
my_font = font_manager.FontProperties(fname = "C:/Windows/Fonts/simsun.ttc")
plt.figure(figsize =(20, 8), dpi = 100)
plt.plot(per_minute, t)
x_ticks = plt.xticks(per_minute[::5], x_xticks[::5], rotation = 45, fontproperties=my_font)