纯 Fortran 绘图库 fplotlib 支持四种格式输出
原标题:fplotlib – Fortran Plotting Library
38AI 研判分
fplotlib 是一个纯 Fortran 实现的绘图库,提供 pylab 风格 API,可输出 SVG、PNG、PDF 与 EPS,且不依赖任何外部图形库,PNG 光栅化器与字体均内置。它支持 plot、scatter、bar、hist 等常用图表类型以及子图、图例、坐标轴标签等配置,面向科学计算场景中的 Fortran 用户。
关联知识实体
报道正文
fplotlib
Pure Fortran plotting library with a pylab-style API, writing SVG, PNG, PDF and EPS. No external graphics library: the PNG rasterizer and its fonts are part of fplotlib.
Documentation and gallery: https://fplotlib.fortran-lang.org/
use fplotlib
call plot(x, y, "k-", label="sin")
call title("My plot")
call xlabel("x")
call ylabel("y")
call grid(.true.)
call legend()
call savefig("out.svg") ! or "out.png", "out.pdf", "out.eps"
call show() ! Jupyter (LFortran) or writes fplotlib_show.svg
! or in a single call
call plot(x, y, "k-", label="sin", title="My plot", xlabel="x", ylabel="y", &
xlim=[0.0_dp, 6.28_dp], ylim=[-1.1_dp, 1.1_dp], &
grid=.true., legend=.true., savefig="out.svg")
! subplots
call clf()
call subplot(2, 1, 1)
call plot(x, y, "b-", label="sin")
call title("top")
call subplot(2, 1, 2)
call plot(x, y2, "r--", label="cos")
call title("bottom")
call suptitle("figure title")Features
- Object-oriented
figure_tandaxes_thandles for explicit figure management and multiple simultaneous independent figures plot,scatter,bar,hist,fill_between,errorbarbartakesyerr=,capsize=,align="edge"andtick_label=;errorbartakesecolor=,elinewidth=,lw=andalpha=plot(y)with a single array numbers the points0, 1, 2, ...for x- Single-call convenience kwargs on
plot(andax%plot):title=,xlabel=,ylabel=,xlim=,ylim=,grid=,legend=,savefig=,show= xlim/ylimto set,get_xlim/get_ylimto read back what autoscaling chose, andinvert_xaxis/invert_yaxisto make an axis count downscatteralso takes per-pointsizes=and color-mappedcvals=(separate keywords because Fortran cannot overload one dummy as scalar-or-array the way matplotlib'ss=andc=do)step,stem,barh,pie,boxplot,violinplotboxplot(y)for one dataset orboxplot(y(:,:), labels=)for a row each, withvert=,notch=,showmeans=,patch_artist=andwhis=violinplotthe same way, withvert=,showmeans=,showmedians=andshowextrema=pie(explode=, startangle=, counterclock=, autopct=, pctdistance=, labeldistance=, radius=, colors=, edgecolor=, linewidth=)semilogx,semilogy,loglog,axhline,axvlinefill_betweenxfor a band along y,stackplotfor layers summed on top of each other, andaxlinefor an endless line through two points or through one with aslope=textandannotate(arrowstyle="->"draws a leader with a head at the annotated point, witharrowcolor=,arrowlw=andshrink=), withrotation=,va=, abbox_facecolor=box behind them and lines broken atachar(10);figtextplaces on the canvas instead of in the axes, andtransform="axes"places in fractions of the axes box.connectionstyle="arc3,rad=0.3"bows the leader out of the straight line, andboxstyle="round,pad=0.5"rounds the corners of the boxtextboxplaces a rich MyST Markdown block (bold, italic, inline and displayed TeX math, lists, Knuth–Plass justification and hyphenation) in axes, figure, or data coordinates, with optional roundedfacecolor=,edgecolor=,pad=andradius=for inset cards, wrapped titles, and figure captionsimshowof an (row, column, channel) array paints the colours given, three channels for RGB or four for RGBAimshowwithcmap,extent,origin,vmin/vmaxand square-pixelaspect, pluscolorbar(label=, orientation=, fraction=, pad=, shrink=, aspect=), upright or lying on its sidecontourandcontourfwith automatic or explicitlevels- Colormaps:
viridis,plasma,inferno,magma,gray,coolwarm - Format strings: colors
bgrcmykw/C0–C9, linestyles---:-., markerso x . s ^ v < > * + D - Optional
label=,lw=,color=,marker=,alpha= hatch=on bars, fills and patches:/,\,|,-,+andx, repeated to pack the lines closer, ruled across the shape and clipped to it (matplotlib's dotted hatches are not drawn)- Marker and line detail on
plot:markersize=,markerfacecolor=,markeredgecolor=,markeredgewidth=,markevery=,drawstyle=and adashes=pattern of your own - Title, axis labels, grid,
xlim/ylim,clf/figure(figsize=, dpi=) set_facecolor(color, alpha=)for the background of one axesgrid(on, axis=, which=, color=, linestyle=, lw=, alpha=)for one axis only, for the minor ticks, or in a colour of your own; log axes carry minor ticks without being asked, as matplotlib's dosavefig(file, transparent=, facecolor=); the extension picks the format, one of.svg,.png,.pdfor.eps, anddpi=sizes the PNG rasteraxis("on"|"off"|"equal"|"scaled"|"tight"|"auto")andset_aspectmargins(m)ormargins(x=, y=)for the room left past the data, andautoscale(enable, axis=, tight=);autoscale(.false.)pins the limits where the data has put them so far, so nothing drawn later moves themset_xscale/set_yscalewith"linear","log"or"symlog"tick_params(axis=, direction=, length=, labelsize=, rotation=)andspineslegend(loc=, fontsize=, ncol=, frameon=, title=, bbox_to_anchor=, labels=), placed where the data leaves most room whenlocis left out, as matplotlib's"best"does; a label starting with_keeps its artist out of the legend,figlegend(loc=, fontsize=, ncol=, frameon=, title=)for one legend covering every panel,xticks/ytickswith optional labels,minorticks_on,xticks(vals, minor=.true.)to place minor ticks by hand, andlocator_params(axis=, nbins=, prune=)- Formatters and locators:
tick_format(axis, "percent"/"comma"/"fixed"),tick_locator(axis, base=, nbins=)andticklabel_format(style=, useoffset=, scilimits=). An axis whose labels would otherwise repeat themselves factors the shared offset and power of ten out into a single label at its end, as matplotlib's ScalarFormatter does - Font sizes:
fontsize=ontitle/xlabel/ylabel/suptitle, andset_fontsize(size=, title=, labels=, ticks=, legend=)to set them globally title(loc="left"|"right")andxlabel/ylabel(labelpad=)savefig(dpi=, bbox_inches="tight", pad_inches=)to crop to the drawing- Several live figures at once:
figure(num=),gcf(),close(num=, all=) - `subplots(m, n, axs, s
(注:更多技术实现细节与完整 API 文档请查阅原项目 README)