Home  >  Article  >  Backend Development  >  Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

王林
王林forward
2023-04-13 15:01:061294browse

Draw such beautiful professional illustrations in Python? So easy!

Text

Hello everyone, I am Python artificial intelligence technology

Method 1

Highly recommended Python's plotting module matplotlib: python plotting. The pictures drawn are really high-end and classy, ​​low-key, luxurious and connotative~ It is suitable for all kinds of drawings from 2D to 3D, from scalar to vector. Can be saved in various formats from eps, pdf to svg, png, jpg. Moreover, the drawing functions of Matplotlib basically have similar names to those of Matlab, so 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 quoted from Thumbnail gallery)

Ordinary function images like this:

Draw such beautiful professional illustrations in Python? So easy!

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

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

Draw such beautiful professional illustrations in Python? So easy!

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

Exquisite curves, translucent color matching. Show your noble and cool personality. The most important thing is that it only takes one line of code to do it. From now on, you no longer have to endure the painful color matching in Matlab and GNUPlot.

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

Draw such beautiful professional illustrations in Python? So easy!

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 it with four lines of code (the last three lines are for drawing contour lines on the coordinate plane, Strictly speaking, it is still one line).

In addition, as long as you are a vector field, the network or any other weird needs can be handled:

Draw such beautiful professional illustrations in Python? So easy!

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

Draw such beautiful professional illustrations in Python? So easy!

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

Draw such beautiful professional illustrations in Python? So easy!

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 the picture drawn by others still looks like this (the following picture is quoted from Matplotlib Tutorial (translation) )

Draw such beautiful professional illustrations in Python? So easy!

You can turn it into something like this:

Draw such beautiful professional illustrations in Python? So easy!

If you match it IPython is used as the running terminal (this picture was drawn by myself~):

Draw such beautiful professional illustrations in Python? So easy!

It is simply an artifact, does it exist?

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

As reminded by @Xu Cheng, I would like to add that matplotlib can also produce xkcd-style graphics~

Draw such beautiful professional illustrations in Python? So easy!

(picture 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 it happens If you are under Windows system, you can try winpython, a distribution version of Python - Portable Scientific Python 2/3 32/64bit Distribution for Windows.

Since @van li questioned whether matplotlib can draw the image shown in the question, I will use matplotlib to draw the image in the question here as follows:

Draw such beautiful professional illustrations in Python? So easy!

The code is here:

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

Draw such beautiful professional illustrations in Python? So easy!

Code here:

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

看到楼下有人说配色和好看,唉....那我也贴几个吧...只不过当初限于篇幅没有写而已。另外,搜索公众号顶级python后台回复“进阶”,获取一份惊喜礼包。

首先,python有一个专门的配色包jiffyclub/brewer2mpl,提供了从美术角度来讲的精美配色(戳这里感受ColorBrewer: Color Advice for Maps)。

此外还有一些致力于美化绘图的库,用起来也都非常方便,比如olgabot/prettyplotlib 。

废话不多说,上图就是王道。(下面图片来源网络)

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

有人可能会说需要复杂的设置,其实也不用。比如上边这幅图,只需要多加一个参数就好:

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

楼下说到统计绘图。嘛seaborn 是一个调用 matplotlib 的统计绘图库,上图:

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

Draw such beautiful professional illustrations in Python? So easy!

代码一行,后边的几乎都是一行,没做其他设置,默认就这样。我就不贴其他的代码了:

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

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

还有个更炫酷的可交互式绘图,大家自己戳开看吧:

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

遇到安装问题的请尝试Anaconda这个Python发行版。下载安装后直接使用即可,它几乎预装了所有要用到的科学计算及可视化的库。

有盆友在评论里说希望能有完整的教程,确实就这个答案来说,离实际使用还有很大的距离,网上相关的中文资料也不多。不过真要写起来这个答案也装不下,况且写在这个问题下也不是很恰当。等到那天我有专栏了再说吧,到时候也许会写一个关于可视化的系列教程。

方法二

翻遍这个问题下的所有回答,发现凡是提到Matlab的,其评价中常有‘锯齿’,‘菜鸟’,‘难看’,‘不忍直视’等标签。

然而,2020年了,技术提升了,观念进步了,当一些基本问题解决后,Matlab还那么‘不堪’吗?

Draw such beautiful professional illustrations in Python? So easy!

观察Mathematica、Origin、Python/matplotlib、R/ggplot2等软件绘制的数据、结果图,其与Matlab图的差异主要体现在点、线、面等对象属性(位置、尺寸、颜色等)的不同上。

既然只是属性的不同,那是不是只要修改一下这些信息,就可以实现各种软件绘图风格之间的转换了呢?

答案是肯定的。

比如,这是高赞回答 @冯昱尧用Python/matplotlib绘制的一幅图:

Draw such beautiful professional illustrations in Python? So easy!

我们用Matlab默认属性来绘制,效果是这样的(没加误差棒):

Draw such beautiful professional illustrations in Python? So easy!

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

Draw such beautiful professional illustrations in Python? So easy!

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

For example, one day, I found that the color of the evening sky was very beautiful, and I thought: Why can’t I draw it into the illustration of the paper? (See: Matlab paper illustration color matching 2 - natural gradient)

So,

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

# #For another example, one day, I saw a photo of my girlfriend and thought it was beautiful. I thought: Why can’t I draw her into the illustrations of my paper? (See: Matlab paper illustration color matching 1 - the color of my girlfriend)


So,


Draw such beautiful professional illustrations in Python? So easy!

At this time, there was a friend I'm about to say: "Oh my answerer, you have to type out all these fancy things line by line. It's too troublesome."


This is wrong. That’s it.


Just like R has ggplot2 and 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:


Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!

Draw such beautiful professional illustrations in Python? So easy!Similarly, Stephen Cobeldick [2] ported the matplotlib color scheme to Matlab.


In other words, you can use matplotlib's color scheme directly in Matlab, and 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 sample effect is as follows:


Draw such beautiful professional illustrations in Python? So easy!

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

In general, tools are just tools, they are not superior or inferior.

If you want to draw good-looking illustrations, the key lies in the person using the tools.

Concentrate and reach the top.

Reference:

​https://www.php.cn/link/b3ddb7c5b10be95dbc3f9152c58becce​​​

​https://www.php.cn/link/171ae1bbb81475eb96287dd78565b38b​

The above is the detailed content of Draw such beautiful professional illustrations in Python? So easy!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete