#dpi为像素大小y
fig, ax = plt.subplots(dpi=3000)
plt.tick_params(labelsize=12)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
font = {'family': 'Times New Roman','size': 10}
font_1 = {'family': 'Times New Roman','size': 14}
plt.rcParams["font.family"] = "Times New Roman" #全图字号新罗马字体
'''
# linstyle 表示线条格式
plt.bar(r1, bars1, width = 'barWidth线宽',color = 'white填充颜色',
edgecolor = '边缘颜色black',
yerr=yer1, capsize=3, 误差棒大小
label='CK',lw=.8,hatch='...',填充大小
alpha=.6,linestyle='--'透明度和边线样式)
hatch填充:{'/','\','|','+','x','o','.','*'}
'''
'''
bbox_to_anchor=(num1, num2)
有时仅通过第一个参数还不能满足我们的预期,比如会出现图例堆叠在一起的情况,这时候就需要调整第二个参数。
num1 用于控制 legend 的左右移动,值越大,越向右移动;
num2 用于控制 legend 的上下移动,值越大,越向'上移动'。
'''
'''
# 图例放置的位置
# upper right
# upper left
# lower left
# lower right
# right
# center left
# center right
# lower center
# upper center
# center
'''
'''
1:几条线设置几个标签
2:fontsize:字体大小
3:edgecolor:图例边框颜色
4:loc:图例位置
5:frameon:图例加不加框
plt.legend([r"a",r"b",r"c"],fontsize=18,edgecolor="black",loc='lower right', frameon=True)
'''
# 设置图例显示行数
plt.legend(bbox_to_anchor=(0.1,0.98), loc=2, borderaxespad=0,prop=font,ncol=5,frameon=False)
ncol为设置图例行数
# # -*- coding: utf-8 -*-
# """
# Created on Thu Apr 13 09:07:03 2023
# @author: ypzhao
# """
import numpy as np
import matplotlib.pyplot as plt
#dpi为像素大小y
fig, ax = plt.subplots(dpi=3000)
plt.tick_params(labelsize=12)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
font = {'family': 'Times New Roman','size': 10}
font_1 = {'family': 'Times New Roman','size': 14}
plt.rcParams["font.family"] = "Times New Roman" #全图字号新罗马字体
#设置分辨率
#设置图的尺寸(480x480)
# plt.figure(dpi=3000)
barWidth = 0.1
bars1 = [5.20, 7.67, 7.50]
bars2 = [7.73,7.85, 7.50]
bars3 = [7.35,7.72,7.68]
bars4 = [7.45,7.90,7.74]
bars5 = [7.56,7.66,7.72]
#设置误差线的高度
yer1 = [2, 1.1, 0.8]
yer2 = [0.3, 0.2, 0.6]
yer3 = [1.1, 0.7, 0.7]
yer4 = [0.3, 2.4, 0.4]
yer5 = [1.2, 0.6, 0.1]
r1 = np.arange(len(bars4))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
r4 = [x + barWidth for x in r3]
r5 = [x + barWidth for x in r4]
'''
# linstyle 表示线条格式
plt.bar(r1, bars1, width = 'barWidth线宽',color = 'white填充颜色',
edgecolor = '边缘颜色black',
yerr=yer1, capsize=3, 误差棒大小
label='CK',lw=.8,hatch='...',填充大小
alpha=.6,linestyle='--'透明度和边线样式)
hatch填充:{'/','\','|','+','x','o','.','*'}
'''
plt.bar(r1, bars1, width = barWidth,color = 'white', edgecolor = 'black',
yerr=yer1, capsize=3, label='CK',lw=.8,hatch='...',alpha=.6,linestyle='--')
plt.bar(r2, bars2, width = barWidth, color = 'white', edgecolor = 'black',
yerr=yer2, capsize=3, label='S1',lw=.8,hatch='***',alpha=.4)
plt.bar(r3, bars3, width = barWidth, color = 'white',edgecolor = 'black',
yerr=yer2, capsize=3, label='S2',lw=.8,hatch='+++',alpha=.3)
plt.bar(r4, bars4, width = barWidth, color = 'white', edgecolor = 'black',
yerr=yer2, capsize=3, label='S3',lw=.8,hatch='ooo',alpha=.5)
plt.bar(r5, bars5, width = barWidth, color = 'white', edgecolor = 'black',
yerr=yer2, capsize=3, label='S4',lw=.8,hatch='xxx',alpha=.4)
plt.xticks([r + barWidth for r in range(len(bars3))], ['30', '45', '60'])
plt.ylabel('pH',font_1)
plt.xlabel("day",font_1)
#创建图例
# plt.annotate('c',xy=(1,1),xytext=(-0.04, 10),fontsize=20)
ax.axis([-0.1, 2.5, 0, 10]) #X、Y轴区间
plt.legend(bbox_to_anchor=(0.1,0.98), loc=2, borderaxespad=0,prop=font,ncol=5,frameon=False)
# 设置图例显示行数
from matplotlib import pyplot as plt
# 设置右和上边框是否可见
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# plt.savefig('respiration.pdf')
plt.savefig('pH.jpg',dpi=3000) #保存为图片jpg格式
plt.show()
#展示图片
- 运行结果