Draw comic-style charts with Matplotlib

Source: Internet
Author: User

Few people have drawn charts by hand since they have computers. Computer-drawn Chart horizontal vertical, you can use a variety of colors, and do not have to worry about painting the wrong need to repeat the problem.

But there is not a feeling, see more neat chart, become some aesthetic fatigue. In the gradual transition to computer graphics in all walks of life, some people began to miss the hand-painted, but in a new way-the touch panel to hand-painted.

1 drawing with Python's Maplotlib package

Usually like Daoteng some data, not less painting various diagrams, the most commonly used is the Python matplotlib package, a few lines of code, you can draw a beautiful picture. Matplotlib package imitate Matlab drawing interface, presumably used matlab people will be very familiar with, this article will not detail the various drawing commands, just show a possibility-add a little graffiti beauty in the chart.

For example, I want to know what the distribution of scores is in a basketball match. So I climbed the American University Athletic Association basketball game scoring data, plus a few lines of code

mean = SUM (data)/len (data) std = STDEV (data) Data_range = (min (data), max (data)) x = Np.linspace (data_range[0], data_range[ 1] norm = stats.norm (mean, STD) plt.hist (data, Normed=true, Bins=max (data) +1, range= ( -0.5,max (data) +0.5), color= ' Green ', label= ' actual distribution ', histtype= ' stepfilled ') plt.axvline (X=mean, color= ' yellow ', linewidth=2) plt.plot (x, Norm.pdf (x) , '--', label= ' standard normal distribution ', color= ' red ', linewidth=2) Plt.xlabel (' Number of scores in a match ') Plt.title (' NCAA Basketball game Score Bar chart ') Plt.legend ()

There's this bar chart below.

Not surprisingly a perfect normal distribution, no more perfect, this is the charm of data visualization.

2 xkcd mode

XKCD is a comic book about romance, satire, maths and language, often geek humor, and I say it's hard to see where the laughter is. xkcd in the Geek circle of influence is not small, that year xkcd painted a python osn, said in Python programming, like in the Cloud walk, the metaphor of the use of Python offers a variety of packages, can be achieved quickly open.

The comic says I JUST jtyped import antigratity, and after a while Python's Open team added a antigravity package to the Python release, the import package will open the comic book directly. This is true, I have checked the information I am sure is the first one of the comic, and then some antigratity bag.

Feel that this hand-painted osn is not an inexplicable beauty. Matplotlib can draw a chart of this style!! Of course not the villains or anything.

All you need to do is add a line of code PLT.XKCD () before drawing, for example, to draw a sine curve:

From NumPy import Linspace, Sinimport Matplotlib.pyplot as Plt plt.xkcd () plt.plot (sin (linspace (0))) plt.title (' Whoo Hoo!!! ')

The effect would be this:

Originally flat drawings, like being soaked in water, become distorted. In addition, they also use, another font, called Humor Sans.

Here is the dry time, if you write in the title of Chinese, you will find the Chinese font, or you originally set up, and did not match the chart to change, this can also be expected, because they cannot find a harmonious font for each text.

After a search, found the founder of the cartoon simplified and installed in the system, but found how can not use this font. After some tossing and finding, the name of the font can only be English, but do not know what the English name of the installation font, and finally by listing matplotlib all supported fonts

Sorted ([F.name for F in Matplotlib.font_manager.fontManager.ttflist])

In turn to find, founder cartoon simplified English name is fzkatong-m19s. Add a line of code matplotlib.rc (' font ', **{' family ': ' fzkatong-m19s '}) set to use the founder cartoon simplified.

Everything is ready, just wait for the draw, still is the score of the sports match, this time is the football

mean = SUM (data)/len (data) Data_range = (min (data), max (data)) Poisson = Stats.poisson (mean) x = Range (data_range[0], Data_ range[1]+1) Poisson_distri = POISSON.PMF (x) plt.xkcd () plt.hist (data, Normed=true, Bins=max (data) +1, range= ( -0.5,max ( Data) +0.5), color= ' green ', label= ' actual distribution ')
Matplotlib.rc (' font ', **{' family ': ' fzkatong-m19s '}) Plt.plot (Poisson_distri, ' o ', label= ' standard Poisson's distribution ') Plt.xlabel (' The number of goals in a match ') Plt.title (' 1998-2014 World Cup goal number goal distribution ') Plt.legend ()

The score of a football match, approximate to the Poisson distribution

is not great. In addition, in xkcd mode, the arrows and other parts have changed.

Let's go Explore! Matplotlib also prepared a website specifically for the XKCD model.

Draw comic-style charts with Matplotlib

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.