How to draw cool professional illustrations using Python? This sums it up, absolutely!

picture

method one

Author|Feng Yuyao

https://www.zhihu.com/question/21664179/answer/18928725

Strongly recommend Python's plotting module matplotlib: python plotting. The drawn picture is really high-end and high-grade, low-key luxury and connotation~ It is suitable for all kinds of drawings from 2D to 3D, from scalar to vector. It can be saved in various formats from eps, pdf to svg, png, jpg.

Moreover, the drawing functions of Matplotlib basically have the same names as those of Matlab, and the learning cost of migration is relatively low. Open source and free. As shown in the picture (the picture in the title description is at the end): (The following pictures are all quoted from Thumbnail gallery)

 Recommended reading (click title to view)

1. Python data mining and machine learning practice technology application

2. R-Meta analysis and [bibliometric analysis, Bayesian, machine learning, etc.] multi-technology integration practice and expansion

3. The latest machine learning and deep learning based on MATLAB 2023a

4. [Eight Days] Summer Camp "Comprehensively Assist AI Research, Teaching and Practical Skills"

A generic function graph like this:

picture

 
 

plt.fill(x, y1, 'b', x, y2, 'r', alpha=0.3)


And this kind of Scatter graph (I don’t know how to say it in Chinese...):

picture

 
 

plt.scatter(x, y, s=area, alpha=0.5)

Delicate curves, translucent colors. Show your noble and glamorous personality, the most important thing is that you can do it with just one line of code. From then on, I no longer have to endure the painful color matching in Matlab and GNUPlot.

Want to draw 3D data? No problem (mayavi may be more convenient):

picture

 
 

ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

You can have four lines of code (the last three lines are to draw contour lines on the coordinate plane, strictly speaking, it is still one line).

In addition, you are a vector field, and you can handle any weird needs of the network:

picture

 
 

plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)plt.colorbar()

picture

 
 

plt.triplot(x, y, triangles, 'go-')plt.title('triplot of user-specified triangulation')plt.xlabel('Longitude (degrees)')plt.ylabel('Latitude (degrees)')

picture

 
 

ax = plt.subplot(111, polar=True)bars = ax.bar(theta, radii, width=width, bottom=0.0)

This is not over yet, Matplotlib also supports the insertion of Latex formulas, when other people's drawings are still like this (the following pictures are quoted from Matplotlib Tutorial (translation))

picture

You can turn it into this:

picture

If you use IPython as the running terminal (this picture is drawn by yourself~):

picture


It is simply a divine tool, is there any!

Action is worse than heartbeat, what are you waiting for?

Reminded by classmate @许哖, I would like to add that matplotlib can also use xkcd style graphs~

picture

(The picture is quoted from the Internet)

In addition, for more exciting content combined with IPython Notebook, please see http://nbviewer.ipython.org/

If you find it troublesome to install and you happen to be on a Windows system, you can try a distribution of Python winpython - Portable Scientific Python 2/3 32/64bit Distribution for Windows.

In view of @van li's question whether matplotlib can draw the image shown in the title, I will draw the image in the title with matplotlib as follows:

picture

The code is here:

https://gist.github.com/coldfog/c479124328fc6bb8b789

picture

The code is here:

https://gist.github.com/coldfog/5da63a6958fc0a949b52

I saw someone downstairs saying that the color scheme will look good, alas.... Then I will post a few... But I didn't write it because of the space limit.

First of all, python has a special color matching package jiffyclub/brewer2mpl, which provides exquisite color matching from an artistic point of view (click here to experience ColorBrewer: Color Advice for Maps).


In addition, there are some libraries dedicated to beautifying plots, which are very convenient to use, such as olgabot/prettyplotlib.

Not much nonsense, the picture above is king. (The picture below comes from the Internet)

picture

picture

picture


Some people may say that complex settings are required, but it is not necessary. For example, in the picture above, you only need to add one more parameter:

 
 

cmap=brewer2mpl.get_map('RdBu', 'diverging', 8, reverse=True).mpl_colormap,


Downstairs is about statistical drawing. Well seaborn is a statistical drawing library that calls matplotlib, above:

(https://github.com/mwaskom/seaborn) 

picture

One line of code, almost all the following lines, no other settings, the default is like this. I will not post other codes:

g = sns.jointplot(x1, x2, kind="kde", size=7, space=0)

picture

picture

picture

picture

picture

There is also a more cool interactive drawing, let’s poke and see for yourself:

http://nbviewer.ipython.org/github/plotly/python-user-guidechaocc/blob/master/s0_getting-started/s0_getting-started.ipynb

Humph, it's over~~~~\(≧▽≦)/~

---

If you encounter installation problems, please try Anaconda, a Python distribution. It can be used directly after downloading and installing. It is pre-installed with almost all scientific computing and visualization libraries to be used.

Some friends said in the comments that they hope to have a complete tutorial. Indeed, as far as this answer is concerned, there is still a long way to go from actual use, and there are not many related Chinese materials on the Internet. But if I really want to write this answer, I can't hold it, and it's not very appropriate to write it under this question. I'll wait until that day when I have a column, maybe I'll write a tutorial series on visualization then.

Method Two

Author|Akun

https://www.zhihu.com/question/21664179/answer/1182984311

Searching through all the answers under this question, I found that those who mention Matlab often have tags such as 'sawtooth', 'rookie', 'ugly', 'can't bear to look straight' in their evaluations.

However, in 2020, technology has improved and concepts have improved. When some basic problems are solved, is Matlab still so "unbearable"?

picture

Observe the data and result graphs drawn by software such as Mathematica, Origin, Python/matplotlib, R/ggplot2, etc. The difference between them and Matlab graphs is mainly reflected in the differences in object attributes (position, size, color, etc.) such as points, lines, and surfaces .

Since it is only a difference in attributes, is it possible to realize the conversion between various software drawing styles by modifying these information?

The answer is yes.

For example, this is a picture drawn by Gao Zan's answer to @冯玉瑰 using Python/matplotlib:

picture

We use the default properties of Matlab to draw, and the effect is like this (without error bars):

picture

Then, you only need to modify the position, size, color and other information to get a picture with a similar style (without error bars):

picture

When we use this idea to think about how to draw illustrations, it is easy to realize our own little ideas, imitate or even create ideal illustrations.

For example, one day, I found that the color of the evening sky was beautiful, and I thought: Why can’t I draw it in the illustration of the thesis? (See: Matlab Paper Illustration Color Matching 2 - Natural Gradient)

then,

picture

picture

For another example, one day, when I saw a photo of my girlfriend, I thought it was beautiful, and thought: Why can’t I draw her in the illustration of my thesis? (See: Matlab Paper Illustration Color Matching 1 - It's Girlfriend's Color)

then,

picture

At this time, a friend is about to say: "Oh my master, you have to type out all these fancy things, line by line of code, it's too troublesome."

This statement is bad.

Just like R has ggplot2, Python has matplotlib, Matlab actually has many ready-made drawing toolkits, and you don't need to develop them yourself.

for example,

Pierre Morel [1] combined with ggplot2, developed the gramm tool for drawing complex graphics.

Inspired by ggplot2 (Wickham 2009), the R implementation of “grammar of graphics” principles (Wilkinson 1999), gramm improves Matlab’s plotting functionality, allowing to generate complex figures using high-level object-oriented code.

The example effect is as follows:

picture

picture

picture

picture

picture

picture

picture

picture

picture

picture

picture

Similarly, Stephen Cobeldick [2] ported the matplotlib color scheme to Matlab.

In other words, you can directly use the matplotlib color scheme in Matlab, so you don't have to always use 'jet'.

The MatPlotLib 2.0 default colormaps ported to MATLAB. This submission also includes the Line ColorOrder colormaps!

The example effect is as follows:

picture

picture

There are also many toolkits specifically for paper illustrations, so I won’t introduce them one by one here.

In general, tools are just tools, and there is no distinction between high and low.

If you want to draw good-looking illustrations, the key lies in the people who use the tools .

Concentrate on one point and reach the top.

Guess you like

Origin blog.csdn.net/cyd20161117/article/details/131898382