from matplotlib import pyplot as plt
折线图
fig = plt.figure(figsize=(20,10), dpi=80) 或者
fig, ax = plt.subplots(1)
plt.plot(x,y, color="red", linewidth=2.5, linestyle="-", label="sine")
plt.xlim(x1,x2)
plt.xticks(range(2,16,2))
plt.minorticks_on() ---> 加minor tick
figure.ax_joint.xaxis.set_minor_locator(ticker.MultipleLocator(0.2))
axs[1].xaxis.set_ticks([np]
axs[1].xaxis.set_ticklabels(tickla)
当刻度太密集时,使用列表的步长间隔取值
plt.xticks(x[::2])
plt.xlabel('xtitle') /plt.ylabel('ytitle')/ plt.title('title')
plt.legend(loc='upper left', frameon=False)
textstr = '\n\n'%(mu, median)
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top') 位置放在左上 text设置参考
plt.savefig('./test.png')
plt.show()
from matplotlib.ticker import MultipleLocator
ax.yaxis.set_minor_locator(MultipleLocator(0.1))
加水平线: plt.axhline(y=2,linewidth=1.5,linestyle='--',color='magenta')
加竖直线: plt.axvline(0,linestyle=‘—‘,color=‘magenta')
常用线型: - 实线 -- 虚线 -. 点线 : 细小的虚线
可以改变点型: s 方形 h 六角形 *星星 +号 d菱形 p五角形
常用颜色:b 蓝色/ g 绿色/ r 红色/ c 天蓝/ m 紫红/ y 黄色/ k 黑色/ w 白色
一条线一个颜色的画法:
cmap = plt.get_cmap('plasma')
ax.text(1.02,0.95-i/20,''.format(Tc[i]),color=cmap(i/20),transform=ax.transAxes)
ax.plot(k,gamma[0],color=cmap(i/20.))
一个panel
fig,ax = plt.subplots(1,1,figsize=[5,4])
pm = plot2d(spt.xpsd,spt.t,spt.fnp.pi2, log=True, vmin=Evmin, vmax=Evmax,
clip=True, xlabel=r'',ylabel=r'',ax=ax)
cax = get_cax(fig,ax, orientation='v', width=0.025)
ax.set(xlabel=r'')
cb = plt.colorbar(pm.image, cax=cax) #, orientation='horizontal'
cb.ax.xaxis.set_ticks_position('top')
# cb.set_label(r'$\mathrm{log}_{10}(P_B)/\mathrm{max}[\mathrm{log}_{10}(P_B)]$')
cb.set_label(r'$P_B$')
cb.ax.xaxis.set_label_position('top')
多个panel的处理
fig, axes=plt.subplots(2,2,figsize=(4,8))
plt.subplots_adjust(left=0.13, right=0.8, top=0.9, bottom=0.15,hspace=0.25,wspace=0.4)
# now we add two dashed lines
ax0 = axes[0,1]
ax1 = axes[1,0]
x0 = ax0.get_xlim()[1]
y0 = ax0.get_ylim()[0]
x1 = ax1.get_xlim()[0]
y1 = ax1.get_ylim()[1]
_draw_connecting_line(ax0, x0, y0, ax1, x1, y1)
ax0 = axes[0,2]
ax1 = axes[1,3]
x0 = ax0.get_xlim()[0]
y0 = ax0.get_ylim()[0]
x1 = ax1.get_xlim()[1]
y1 = ax1.get_ylim()[1]
_draw_connecting_line(ax0, x0, y0, ax1, x1, y1)
# now we determine colorbar location
ax_pos_ul = axes[0,-1].get_position()
ax_pos_ll = axes[1,-1].get_position()
cb_height = ax_pos_ul.ymax-ax_pos_ll.ymin
cb_width = 0.015
hspace = 0.025
x0 = ax_pos_ll.xmax + hspace
y0 = ax_pos_ll.ymin
x1 = x0 + cb_width
cbp = [x0, y0, cb_width, cb_height]
cax = fig.add_axes(cbp)
plt.colorbar(img, label=r'P$_B$', cax=cax)
ax=fig.add_subplot(211)
ax=axes[0,0]
每个panel 加上小标号:
labels = ['a', 'b', 'c']
for i in range(3):
ax[i].text(0.05,0.93,labels[i], color ='k', transform=ax[i].transAxes)
更改全局字号:
font = {'family':'normal','weight':'normal','size':16}
matplotlib.rc('font',**font)
sublabel=['(a)','(c)','(e)','(b)','(d)','(f)']
for i in range(2):
for j in range(3):
axes[i,j].text(0.85,0.9,sublabel[3*i+j],transform=axes[i,j].transAxes,color='k',fontsize=20)
多个panel 共用一个x axis:
fig, a = plt.subplots(3,figsize = [6,10],sharex=True)
plt.subplots_adjust(left=0.16, right=0.8, top=0.9, bottom=0.15,hspace=0.3,wspace=0.4)
colorbar 相关
Colormap选取
cmap=plt.cm.jet
clb=plt.colorbar()
plt.clim([1,33])
ax1.set(xlim=[xx,xx], ylim=[xx, xx])
Colorbar 摆放位置:
fig, (ax1, ax2) = plt.subplots(ncols=2)
img1 = ax1.imshow(data)
fig.colorbar(img1, ax=ax1)
ax1.set_aspect('auto')
img2 = ax2.imshow(-data)
fig.colorbar(img2, ax=ax2)
ax2.set_aspect('auto’)
plt.tight_layout(h_pad=1)
想放在水平位置 fig.colorbar(im, cax=cax, orientation="horizontal")
Colorbar title 的位置:
clb=plt.colorbar()
Clb.ax.set_xlabel() 放在底下, clb.ax.set_ylabel() 放在侧边, clb.ax.set_title() 放在顶上
Colorbar ticks:log 轴科学计数改成普通 比如 3*10 —> 30
from matplotlib.ticker import LogFormatter
formatter = LogFormatter(10, labelOnlyBase=False)
cb = plt.colorbar(ticks=[1,5,10,20,50], format=formatter)
add_color_bar_V(p2d.image,axes[0,2],fig,label='PSD') 可参考这个函数进行修改
多个panel 一个colorbar
pcm.set_clim([1,2000])
cb_ax = fig.add_axes([0.9, 0.1, 0.02, 0.8])
cbar = fig.colorbar(pcm, cax=cb_ax)
cbar.ax.set_ylabel('Counts’)
clb = axs[n, m].pcolormesh(t, w[:], np.log10(psd[:, :]), cmap=purula(), vmax=-2, vmin=-4)
axs[n, m].tick_params(labelsize=15)
axs[n, m].set(ylim=[0, 1])
axs[n, m].set_title(r'Z=' + '%.2f' % z, fontsize=20)
position0 = fig.add_axes([0.85, 0.1, 0.02, 0.8])
cbar = plt.colorbar(clb, cax=position0)
cbar.ax.tick_params(labelsize=18)
cbar.set_label(r'', fontsize=20)
legend 摆放位置 https://www.delftstack.com/zh/howto/matplotlib/how-to-place-legend-outside-of-the-plot-in-matplotlib/
散点图
plt.scatter(x,y)
直方图
d = xxx ;; 组距
num_bins = int(max(a)-min(a)/bin_width)
plt.hist(a,num_bins,normed=1) 默认是频数直方图,加上关键字参数normed = 1变为频率分布直方图
当组距不均匀时: plt.hist(a,[min(a)+i*bin_width for i in range(num_bins)])
画不同样式的bar:histtype='stepfilled','histtype', 'barstacked',rwidth=0.8
facecolor='g',alpha=0.75
kwargs = dict(hist_kws={'alpha':0.5}, kde_kws={'linewidth':2},norm_hist=True, kde=False)
x=EMIC_bw
width=(np.nanmax(x)-np.nanmin(x))/200
bins=np.arange(np.nanmin(x),np.nanmax(x),width)
x_weight = np.ones_like(x)/float(len(x))
plt.hist(x,bins=bins,weights=x_weight,linewidth=2,color='dodgerblue',alpha=0.5,label='EMIC')## ,histtype='step'
plt.xticks(range(min(a),max(a)+d, d))
plt.grid(True, linestyle='-.', alpha=0.5) 显示网格,设置透明度
其他画图参考
Ploty 画图的github
seaborn