2⃣️. 数据图片性质

1. Markers and line styles

plt.plot(X, Y, '<format>', ...)

controlling color:

b(blue); c(cyan); g(green); k(black); m(magenta); r(red); w(white); y(yellow).

Specifying styles in multiline plots---specifying all the lines in a single function call

plt.plot(x1, y1, fmt1, x2, y2, fmt2, ...)

Controlling line styles

'-'solid line ; '--'dashed line;  '-.' dash-dot line;  ':' dotted line.

controlling marker styles---several markers can be used in Matplotlb;

In the following example, we try to group up all the customization available for colors, lines, and markers in the following way:

y = np.arange(1, 3, 0.3)

plt.plot(y, 'cx--', y+1, 'mo:', y+2, 'kp-.')


2. Finer control with keyword arguments

plot() is a really rich function, and there are some keyword arguments to configure colors, markers, and line styles:

keyword arguments !!!

3. Handling X and Y axis

Matplotlib provides two basic functions to manage them—xticks() and yticks(). They behave in the same way, so the description for one function will apply to the other too.

4. Plot types

1) Histogram charts---Histogram charts are a graphical display of frequencies, represented as bars. They show what portion of the dataset falls into each category, usually specified as non-overlapping intervals. Matplotlib calls those categories bins.

By default, hist() uses a bin value of 10 (so only ten categories, or bars, are computed), but we can customize it, either by passing an additional parameter, for example, in hist(y, <bins>), or using the bin keyword argument as hist(y, bin=<bins>).

2) Error bar charts

Using the errorbar() function, Matplotlib allows us to create such a graph type.

plt.errorbar(x, y, yerr=e1, fmt='.-');


3) Scatter plots

A scatter plot is often used to identify potential association between two variables, and it's often drawn before working on a fitting regression function. It gives a good visual picture of the correlation, in particular for nonlinear relationships. Matplotlib provides the scatter() function to plot X versus Y unidimensional array of the same length as scatter plot.

4) Polar charts

A polar system is a two-dimensional coordinate system, where the position of a point is expressed in terms of a radius and an angle. This system is used where the relationship between two points is better expressed using those information. The angular coordinate can be expressed either in radians or in degrees. Though, Matplotlib uses degrees. The polar() Matplotlib function plots polar charts. Its parameters are two lists (of the same length)—theta for the angular coordinates and r for the radial coordinates.

There are two functions to control the radial and the angular grids: rgrid() and thetagrid() functions respectively.

theta = np.arange(0., 2., 1./180.)*np.pi

r = np.abs(np.sin(5*theta) - 2.*np.cos(theta))

plt.polar(theta, r);    #1

plt.thetagrids(range(45, 360, 90));  #2

plt.rgrids(np.arange(0.2, 3.1, .7), angle=0);  #3

5. Text inside figure, annotations, and arrows

1) adding text inside the figure

plt.text(x, y, text)

2) adding annotations

The annotate() function provides functionality to make annotation easy. In annotation, we have to consider two points—the graph point we want to annotate (represented by an xy keyword argument) and the plot position where we want to place the annotation text (represented by xytext). Both are expressed in an (x,y) format in data coordinate positions; Moreover, there is an additional argument to specify the arrow properties, that's the fundamental difference between text() and annotate(). We connect the annotation text to the annotated point with an arrow.

plt.plot(y);

plt.ylim(ymax=35);

plt.annotate('this spot must really\nmean something', xy=(6, 30), xytext=(8, 31.5), arrowprops=dict(facecolor='black', shrink=0.05));

Also note that we had to use ylim() to adjust the Y limits, as annotate() does not do it automatically.

3) adding an arrow

Matplotlib provides an arrow() function. It takes the coordinates of the arrow origin (x,y), and the delta distance (dx, xy), the distance at which the head is to be placed. This implies that the arrow starts at (x,y) and ends at (x + dx, y + dy)

plt.arrow(x, y, dx, dy)

Sadly, this function is quite hard to use and presents several difficulties.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 198,603评论 5 465
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,422评论 2 375
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 145,478评论 0 327
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,116评论 1 268
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,037评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,832评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,075评论 3 389
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,685评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,976评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,028评论 2 315
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,830评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,584评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,101评论 3 301
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,229评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,521评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,121评论 2 343
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,314评论 2 339