参考博文
Python绘图总结(Matplotlib篇)之坐标轴及刻度
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as sts
if __name__ == '__main__':
a = 0.031 / 10000 + 0.0337 / 10000
print(0.0336 * 100 / np.sqrt(a)) # 1320.95
r = sts.lognorm.rvs(0.954, size=1000)
c = plt.hist(r, bins=500)
plt.show()
# 双对数坐标下
fig, ax = plt.subplots()
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_adjustable("datalim")
ax.plot(c[1][:-1], c[0], 'o')
ax.set_xlim(1e-1, 1e6)
ax.set_ylim(1e-2, 1e6)
ax.grid()
plt.draw()
plt.show()
# 半对数坐标
fig1, ax1 = plt.subplots()
ax1.hist(r, bins=500)
ax1.set_xscale("log")
ax1.set_xlim(1e-1, 1e6)
ax1.grid()
plt.draw()
plt.show()
效果如下
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)
font = {'family': 'arial', 'weight': 'normal', 'size': 14}
ax1.set_xticks([1,2,3,4,5)
ax1.set_xticklabels(x,font)
ax1.set_yticks([0,0.2,0.4,0.6,0.8,1])
ax1.set_yticklabels([0,0.2,0.4,0.6,0.8,1],font)
逆序前的代码和图
import matplotlib.pyplot as plt
plt.figure()
ax1 = plt.subplot(111)
x_list = [1, 2, 3, 4, 5]
y_list = [5, 10, 20, 40, 80]
plt.sca(ax1)
plt.title("Test 1")
plt.xlabel("X")
plt.ylabel("Y")
plot1, = plt.plot(x_list, y_list, 'bo')
plt.show()
逆序后的代码和图
import matplotlib.pyplot as plt
plt.figure()
ax1 = plt.subplot(111)
x_list = [1, 2, 3, 4, 5]
y_list = [5, 10, 20, 40, 80]
plt.sca(ax1)
plt.title("Test 1")
plt.xlabel("X")
plt.ylabel("Y")
plot1, = plt.plot(x_list, y_list, 'bo')
plt.gca().invert_xaxis() # 就这句话
plt.show()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
参考博文
matplotlib命令与格式:图例legend语法及设置
参考博文
ax.legend(loc='center left', bbox_to_anchor=(0.2, 1.12),ncol=3)
官方教程
ax.set_title('AUC', font, x=0.5, y=1.05)
x,y可以控制标题的位置,但是记住要放在 font 后面,不然会报错.
SyntaxError: positional argument follows keyword argument
matplotlib.axes.Axes.annotate
matplotlib命令与格式:标题(title),标注(annotate),文字说明(text)
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
# 字体属性
size=12, family='arial',
ha='center', va='bottom')
用色环图帮你搞定配色
# 色系 浅灰色 #d8dcd6 天蓝色 #069af3 lightblue(#7bc8f6) blue with a hint of purple(#533cc6) # purple red(#990147)
# dark royal blue(#02066f) royal(#0c1793)
ax.plot(a,b,linestyle='dotted',linewidth=2.5,marker='o',color='#e377c2',label='CN')
ax.plot(a,c,linestyle='dotted',linewidth=2.5,marker='v',color='#533cc6',label='RA')
ax.plot(a,d,linestyle='solid',linewidth=2.5,marker='d',color='#7d7f7c',label='PA')
ax.plot(a,e,linestyle='dashed',linewidth=2.5,marker='8',color='#01ff07',label='LP')
matplotlib.pyplot.subplots_adjust
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
## 默认参数
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for space between subplots,
# expressed as a fraction of the average axis width
hspace = 0.2 # the amount of height reserved for space between subplots,
# expressed as a fraction of the average axis height