Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

📊 More styles and useful extensions for Matplotlib

License

NotificationsYou must be signed in to change notification settings

nschloe/matplotx

Repository files navigation

matplotx

Some useful extensions forMatplotlib.

PyPi VersionAnaconda CloudPyPI pyversionsDOIGitHub starsDownloads

gh-actionscodecovCode style: black

This package includes some useful or beautiful extensions toMatplotlib. Most of those features could be inMatplotlib itself, but I haven't had time to PR yet. If you're eager, let meknow and I'll support the effort.

Install with

pip install matplotx[all]

and use in Python with

importmatplotx

See below for what matplotx can do.

Clean line plots (dufte)

matplotlibmatplotx.styles.dufte,matplotx.ylabel_top,matplotx.line_labelsmatplotx.styles.duftify(matplotx.styles.dracula)

The middle plot is created with

importmatplotlib.pyplotaspltimportmatplotximportnumpyasnp# create datarng=np.random.default_rng(0)offsets= [1.0,1.50,1.60]labels= ["no balancing","CRV-27","CRV-27*"]x0=np.linspace(0.0,3.0,100)y= [offset*x0/ (x0+1)+0.1*rng.random(len(x0))foroffsetinoffsets]# plotwithplt.style.context(matplotx.styles.dufte):foryy,labelinzip(y,labels):plt.plot(x0,yy,label=label)plt.xlabel("distance [m]")matplotx.ylabel_top("voltage [V]")# move ylabel to the top, rotatematplotx.line_labels()# line labels to the rightplt.show()

The three matplotx ingredients are:

  • matplotx.styles.dufte: A minimalistic style
  • matplotx.ylabel_top: Rotate and move the the y-label
  • matplotx.line_labels: Show line labels to the right, with the line color

You can also "duftify" any other style (see below) with

matplotx.styles.duftify(matplotx.styles.dracula)

Further reading and other styles:

Clean bar plots

matplotlibduftedufte withmatplotx.show_bar_values()

The right plot is created with

importmatplotlib.pyplotaspltimportmatplotxlabels= ["Australia","Brazil","China","Germany","Mexico","United\nStates"]vals= [21.65,24.5,6.95,8.40,21.00,8.55]xpos=range(len(vals))withplt.style.context(matplotx.styles.dufte_bar):plt.bar(xpos,vals)plt.xticks(xpos,labels)matplotx.show_bar_values("{:.2f}")plt.title("average temperature [°C]")plt.show()

The two matplotx ingredients are:

  • matplotx.styles.dufte_bar: A minimalistic style for bar plots
  • matplotx.show_bar_values: Show bar values directly at the bars

Extra styles

matplotx contains numerous extra color schemes, e.g.,Dracula,Nord,gruvbox, andSolarized,the revised Tableau colors.

importmatplotlib.pyplotaspltimportmatplotx# use everywhere:plt.style.use(matplotx.styles.dracula)# use with context:withplt.style.context(matplotx.styles.dracula):pass
Many more styles here...

Other styles:

Smooth contours

plt.contourfmatplotx.contours()

Sometimes, the sharp edges ofcontour[f] plots don't accurately represent thesmoothness of the function in question. Smooth contours,contours(), serves as a drop-in replacement.

importmatplotlib.pyplotaspltimportmatplotxdefrosenbrock(x):return (1.0-x[0])**2+100.0* (x[1]-x[0]**2)**2im=matplotx.contours(rosenbrock,    (-3.0,3.0,200),    (-1.0,3.0,200),log_scaling=True,cmap="viridis",outline="white",)plt.gca().set_aspect("equal")plt.colorbar(im)plt.show()

Contour plots for functions with discontinuities

plt.contourmatplotx.contour(max_jump=1.0)

Matplotlib has problems with contour plots of functions that have discontinuities. Thesoftware has no way to tell discontinuities and very sharp, but continuous cliffs apart,and contour lines will be drawn along the discontinuity.

matplotx improves upon this by adding the parametermax_jump. If the difference betweentwo function values in the grid is larger thanmax_jump, a discontinuity is assumedand no line is drawn. Similarly,min_jump can be used to highlight the discontinuity.

As an example, take the functionimag(log(Z)) for complex values Z. Matplotlib'scontour lines along the negative real axis are wrong.

importmatplotlib.pyplotaspltimportnumpyasnpimportmatplotxx=np.linspace(-2.0,2.0,100)y=np.linspace(-2.0,2.0,100)X,Y=np.meshgrid(x,y)Z=X+1j*Yvals=np.imag(np.log(Z))# plt.contour(X, Y, vals, levels=[-2.0, -1.0, 0.0, 1.0, 2.0])  # draws wrong linesmatplotx.contour(X,Y,vals,levels=[-2.0,-1.0,0.0,1.0,2.0],max_jump=1.0)matplotx.discontour(X,Y,vals,min_jump=1.0,linestyle=":",color="r")plt.gca().set_aspect("equal")plt.show()

Relevant discussions:

spy plots (betterspy)

Show sparsity patterns of sparse matrices or write them to image files.

Example:

importmatplotxfromscipyimportsparseA=sparse.rand(20,20,density=0.1)# show the matrixplt=matplotx.spy(A,# border_width=2,# border_color="red",# colormap="viridis")plt.show()# or save it as pngmatplotx.spy(A,filename="out.png")
no colormapviridis

There is a command-line tool that can be used to showmatrix-market orHarwell-Boeing files:

matplotx spy msc00726.mtx [out.png]

Seematplotx spy -h for all options.

License

This software is published under theMIT license.

About

📊 More styles and useful extensions for Matplotlib

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp