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)
當(dāng)刻度太密集時谒出,使用列表的步長間隔取值
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設(shè)置參考
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')
常用線型: - 實(shí)線 -- 虛線 -. 點(diǎn)線 : 細(xì)小的虛線
可以改變點(diǎn)型: s 方形 h 六角形 *星星 +號 d菱形 p五角形
常用顏色:b 藍(lán)色/ g 綠色/ r 紅色/ c 天藍(lán)/ 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 加上小標(biāo)號:
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 相關(guān)
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() 放在側(cè)邊蹬音, clb.ax.set_title() 放在頂上
Colorbar ticks:log 軸科學(xué)計數(shù)改成普通 比如 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') 可參考這個函數(shù)進(jìn)行修改
多個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/
散點(diǎn)圖
plt.scatter(x,y)
直方圖
d = xxx ;; 組距
num_bins = int(max(a)-min(a)/bin_width)
plt.hist(a,num_bins,normed=1) 默認(rèn)是頻數(shù)直方圖永部,加上關(guān)鍵字參數(shù)normed = 1變?yōu)轭l率分布直方圖
當(dāng)組距不均勻時: 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) 顯示網(wǎng)格,設(shè)置透明度
其他畫圖參考
Ploty 畫圖的github
seaborn