1. 一行一列图
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
font_path = '/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf'
fm.fontManager.addfont(font_path)
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['axes.linewidth'] = 4
fig = plt.figure(figsize=(8,8),dpi = 300)
grid = fig.add_gridspec(1,1)
ax = fig.add_subplot(grid[0,0])
data1 = np.loadtxt('li_energy.dat')
sort_indices = np.argsort(data1[:, 0])
data1 = data1[sort_indices]
data2 = np.loadtxt('na_energy.dat')
sort_indices = np.argsort(data2[:, 0])
data2 = data2[sort_indices]
data3 = np.loadtxt('k_energy.dat')
sort_indices = np.argsort(data3[:, 0])
data3 = data3[sort_indices]
x = np.sort(data2[:,0])
y1 = data1[:,1]
y2 = data2[:,1]
y3 = data3[:,1]
y1_lowest = y1.min()
y1_minus = y1 - y1_lowest
y1_per_atom = y1_minus/ 132 * 1000
y1_per_atom_pad = np.pad(y1_per_atom,(0,x.shape[0] - len(y1_per_atom)),'constant',constant_values = np.nan)
y2_lowest = y2.min()
y2_minus = y2 - y2_lowest
y2_per_atom = y2_minus/ 132 * 1000
y2_per_atom_pad = np.pad(y2_per_atom,(0,x.shape[0] - len(y2_per_atom)),'constant',constant_values = np.nan)
y3_lowest = y3.min()
y3_minus = y3 - y3_lowest
y3_per_atom = y3_minus/ 132 * 1000
y3_per_atom_pad = np.pad(y3_per_atom,(0,x.shape[0] - len(y3_per_atom)),'constant',constant_values = np.nan)
x = np.arange(1,21,1)
ax.plot(x,y1_per_atom_pad[0:20],label='Li')
ax.scatter(x,y1_per_atom_pad[0:20])
ax.plot(x,y2_per_atom_pad[0:20],label='Na')
ax.scatter(x,y2_per_atom_pad[0:20])
ax.plot(x,y3_per_atom_pad[0:20],label='K')
ax.scatter(x,y3_per_atom_pad[0:20])
ax.set_ylabel('Energy relative the lowest energy per atom (meV)', fontsize = 35,fontname='Times New Roman',fontweight="bold")
plt.xticks(fontsize = 30,fontname = "Times New Roman",fontweight = "bold")
plt.yticks(fontsize = 30,fontname = "Times New Roman",fontweight = "bold")
plt.legend(prop={'family': 'Times New Roman', 'weight': 'bold', 'size': 25})
plt.gca().xaxis.set_major_locator(plt.MaxNLocator(4))
plt.savefig('plot.png',dpi = 1000)
plt.show()
2. 一行两列图
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['font.size'] = 12
plt.rcParams['axes.titlesize'] = 16
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
plt.rcParams['legend.fontsize'] = 12
plt.rcParams['axes.linewidth'] = 1.5
fig, axs = plt.subplots(1, 2, figsize=(12, 5), dpi=300)
for ax in axs:
ax.plot([1,2,3], [4,5,6], linewidth=1.8, marker='o', markersize=7)
ax.set_xlabel("X轴", fontsize=14)
ax.set_ylabel("Y轴", fontsize=14)
ax.legend(["数据"], frameon=True)
plt.tight_layout()
plt.savefig('figure.png',dpi=600)
plt.savefig('figure.pdf',dpi=600)
plt.show()
3. 两行两列图
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['font.size'] = 12
plt.rcParams['axes.titlesize'] = 16
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
plt.rcParams['legend.fontsize'] = 12
plt.rcParams['axes.linewidth'] = 1.5
fig, axs = plt.subplots(2, 2, figsize=(10, 8), dpi=300)
for ax in axs.flat:
ax.plot([1,2,3], [4,5,6], linewidth=1.8, marker='o', markersize=7)
ax.set_xlabel("X轴", fontsize=14)
ax.set_ylabel("Y轴", fontsize=14)
ax.legend(["数据"], frameon=True)
plt.tight_layout()
plt.show()
4. 处理 1*2数据
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
import argparse
parser = argparse.ArgumentParser(description='deal two column data')
parser.add_argument('filename')
parser.add_argument('xlabel')
parser.add_argument('ylabel')
args = parser.parse_args()
data_txt = args.filename
x_label = args.xlabel
y_label = args.ylabel
# print('='*40)
# filename = input('请输入文件名:').strip()
# x_label = input("请输入x轴标签(直接回车可跳过): ").strip()
# y_label = input("请输入y轴标签(直接回车可跳过): ").strip()
# print("="*40)
# data_txt = filename
# x_label = x_label
# y_label = y_label
data = np.loadtxt(data_txt)
plt.rcParams['font.size'] = 16
plt.rcParams['axes.linewidth'] = 2
plt.rcParams['axes.titlesize'] = 16
plt.rcParams['axes.labelsize'] = 20
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['xtick.labelsize'] = 16
plt.rcParams['ytick.labelsize'] = 16
plt.rcParams['legend.fontsize'] = 16
figure = plt.figure(figsize=(8,6),dpi = 300)
grid = figure.add_gridspec(1,1)
ax1 = figure.add_subplot(grid[0,0])
x = data[:,0]
y = data[:,1]
ax1.plot(x,y,linewidth=1.8)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.xticks(fontweight = 'bold')
plt.yticks(fontweight = 'bold')
plt.legend(prop = {'weight' :'bold'})
plt.tight_layout()
plt.savefig('plot.png',dpi = 300)
转载请注明来源 有问题可通过github提交issue