其实matplotlib画的图也没那么丑吧,尤其是第一张,感觉还是可以接受的(因为东西加的多所以看起来丑了点吧
代码如下:
# coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定中文字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False # 正常显示负号
fig = plt.figure(figsize=(10, 20), dpi=100) # 调整画布大小,清晰度
fig1 = fig.add_subplot(221) # 画布1
fig2 = fig.add_subplot(222) # 画布2
fig3 = fig.add_subplot(223) # 画布3
fig4 = fig.add_subplot(224) # 画布4
def draw_fig1():
# fig1
x = np.linspace(0, 5 * np.pi, 50)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = x
# x轴到y1的区域填充
fig1.fill(x, y1, 'b', alpha=0.5, label="sin(x)")
fig1.plot(x, y2, 'r', alpha=0.5, label="cos(x)")
fig1.scatter(x, y3, alpha=0.8, label="scatter")
# y1 +1 1 的区域, 且y1 > 0的区域填充
fig1.fill_between(x, y1 + 1, y1 - 1, where=y1 > 0, facecolor='b', alpha=0.2)
fig1.grid(True)
# fig1.set_ylim(bottom=-10, top=10) # 设置x刻度
# fig1.set_xlim(left=0, right=10, emit=1)
fig1.set_title("填充图")
fig1.set_xlabel("x轴", fontsize=10, labelpad=10) # 字的大小和里轴的距离
fig1.set_ylabel("y轴", fontsize=10, labelpad=10)
fig1.set_xticks([1, 2, 3])
fig1.set_xticklabels(["A", "B", "C"], rotation=30, fontsize=10) # 设置x轴坐标
# fig1.set_yticks([-1, 1, 0, 0.1])
fig1.legend(fontsize=10, loc="center") # 显示图例
# loc 可取(‘best’, ‘upper right’, ‘upper left’, ‘lower left’, ‘lower right’, ‘right’,
# ‘center left’, ‘center , right’, ‘lower center’, ‘upper center’, ‘center’)
def draw_fig2():
import numpy as np
import matplotlib.pyplot as plt
# 饼型图共分五份,每份的值
z = [10, 20, 30, 40, 50]
# 对应的块偏离圆心的比例
explode = [0, 0.2, 0, 0, 0]
# 每块对应的内容
labels = ('A', 'B', 'C', 'D', 'E')
colors = ["r", "g", "b", "y", "pink"]
# pie(x, explode=None, labels=None, colors=None, autopct=None,
# pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None,
# radius=None, hold=None)
# x:饼状图的块数,及每块的值
# explode:对应的块偏离圆心的比例
# labels:每块的标签
# colors:也是数组,表示每块内容的颜色
# autopct:表示显示每块所占的比例
# pctdistance=0.8:百分比标号距中心的距离比例
# shadow=True:是否有阴影效果
# labeldistance:表示每块对应内容文字距离中心点是饼图半径的倍数
# startangle=90 起始绘制角度,并且是逆时针绘制
# radius:饼状图的半径,默认是1 hold:是否覆盖其他图形,如果在一个figure中绘制,选择True会覆盖,False会共存
fig2.pie(z, explode=explode, labels=labels, startangle=90, autopct='%1.1f%%',
colors=colors, shadow=True, pctdistance=0.8, labeldistance=1.2)
fig2.legend()
def draw_fig3():
data = np.random.randint(100, size=(1, 100))[0]
data[99] = 300
fig3.boxplot(data)
fig3.set_xlabel("特征")
def draw_fig4():
key_name = ["A", "B", "C", "D", "E"]
key_values = [0.1, 0.9, 1, 1, -0.5]
def auto_label(rects):
for rect in rects:
height = rect.get_height()
if height >= 0:
# 标上对应柱上的数字
fig4.text(rect.get_x() + rect.get_width() / 2.0 - 0.3, height + 0.02, '%.3f' % height)
else:
fig4.text(rect.get_x() + rect.get_width() / 2.0 - 0.3, height - 0.06, '%.3f' % height)
# 如果存在小于0的数值,则画0刻度横向直线
fig4.axhline(y=0, color='black')
# 归一化,让颜色分布均匀些
norm = plt.Normalize(-1, 1)
norm_values = norm(key_values)
map_vir = cm.get_cmap(name='viridis') # 获得对应的颜色映射条的风格
colors = map_vir(norm_values) # 对映射条应到数据
ax = fig4.bar(key_name, key_values, width=0.5, color=colors, edgecolor='white') # 设置长方体边框的颜色
sm = cm.ScalarMappable(cmap=map_vir, norm=norm) # norm设置最大最小值
plt.colorbar(sm)
auto_label(ax)
bwith = 4 # 边框宽度设置为2
TK = plt.gca() # 获取边框
TK.spines['bottom'].set_linewidth(bwith) # 图框下边
TK.spines['left'].set_linewidth(bwith) # 图框左边
TK.spines['top'].set_linewidth(bwith) # 图框上边
TK.spines['right'].set_linewidth(bwith) # 图框右边
draw_fig1()
draw_fig2()
draw_fig3()
draw_fig4()
plt.savefig("四张图.png", dpi=100)
plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)