-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_mp.py
118 lines (102 loc) · 3.57 KB
/
cc_mp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'matplotlib 相关'
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib import font_manager
import pandas as pd
plt.rcParams['xtick.direction'] = 'in' #将tick放入内内侧
plt.rcParams['ytick.direction'] = 'in'
#simsun = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simsun.ttf',size=12)
simsun = font_manager.FontProperties('simsun',size=12)
# plt.legend(prop=simsun)
# plt.xlabel(xlabel,fontproperties=simsun)
# plt.ylabel(ylabel,fontproperties=simsun)
# plt.title(title,fontproperties=simsun)
def setup_paper_figure():
'论文绘图样式'
plt.rc('font',family='Times New Roman')
plt.rcParams['mathtext.fontset'] = 'custom'
plt.rcParams['mathtext.rm'] = 'Times New Roman'
plt.rcParams['mathtext.it'] = 'Times New Roman:italic'
plt.rcParams['mathtext.bf'] = 'Times New Roman:bold'
mp_colors=['r','g','b','m','c','y',
'orange','lime','forestgreen','slategrey','royalblue',
'pink','magenta','blueviolet',
'coral','olive','cadetblue','lightskyblue','darkseagreen',
'navy','thistle','darkcyan',]
mp_linestyles = ['-','--',':','-.']
mp_markers_filled = {'o','v','^','<','>'}
mp_markers = ['o','v','^','<','>','x','+']
def get_session_value(session:dict,values:list,key):
'返回 key 对应的唯一值'
try:
return session[key]
except KeyError:
pass
i = len(session)
i %= len(values)
ans = values[i]
session[key] = ans
return ans
GLB_SESSION = {}
def get_session_color(key,session=GLB_SESSION)->str:
return get_session_value(session.setdefault('color',{}),mp_colors,key)
def get_session_linestyle(key,session=GLB_SESSION)->str:
return get_session_value(session.setdefault('linestyle',{}),mp_linestyles,key)
def get_session_marker(key,session=GLB_SESSION)->str:
return get_session_value(session.setdefault('marker',{}),mp_markers,key)
def plot_fin(show=True,tight=True,grid=True):
if grid:
plt.grid(True,which='major',linestyle=':',color='gray')
plt.grid(True,which='minor',linestyle=':',color=(0.7,0.7,0.7))
if tight:
plt.tight_layout()
if show:
plt.show()
def plot(*a,
xlabel=None,ylabel=None,label=None,
color=None,linestyle=None,marker=None,
color_key=None,linestyle_key=None,marker_key=None,
logx=False,logy=False,
**k):
color_key = color_key or label
color = color or get_session_color(color_key)
if linestyle is None:
if linestyle_key is not None:
linestyle = get_session_linestyle(linestyle_key)
else:
linestyle = '-'
if marker is None:
if marker_key is not None:
marker = get_session_marker(marker_key)
else:
marker = 'o'
if marker == 'none':
marker = None
ka = dict(linestyle=linestyle,marker=marker,color=color,label=label)
if marker in mp_markers_filled:
ka['markerfacecolor'] = 'none'
k.update(ka)
plt.plot(*a,**k)
if xlabel:plt.xlabel(xlabel)
if ylabel:plt.ylabel(ylabel)
if logx:plt.xscale('log',base=10)
if logy:plt.yscale('log',base=10)
def plot_df(df,xkey:str,ykey:str,**k):
'绘制 df 中的数据,df 为字典构成的列表'
df = pd.DataFrame(df)
x = np.asarray(df[xkey])
y = np.asarray(df[ykey])
k.setdefault('xlabel',xkey)
k.setdefault('ylabel',ykey)
plot(x,y,**k)
def _test():
x = np.linspace(-5,5,100)
for func in ('sin','cos'):
f = getattr(np,func)
y = f(x)
plot(x,y,label=func,xlabel='x',ylabel='y')
plt.legend()
plot_fin()
if __name__ == '__main__':
_test()