画表格库matplotlib的基本应用

import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np


Matplotlib在Figures(画布)上绘制数据,他们每个都可以包含一个或多个轴空间Axes,是一个可以在 x-y 坐标系中绘制点的区域(或者其他坐标系)。创建带有轴空间的 Figure 的最简单的方式就是使用 pyplot.subplots。然后我们可以使用 Axes.plot 来基于坐标轴绘制一些数据

fig, ax = plt.subplots() # Create a figure containing a single axes.

ax.plot([1, 2, 3, 4], [1, 4, 2, 3]); # Plot some data on the axes.


画表格库matplotlib的基本应用


from matplotlib import pyplot as plt

x=range(2,26,2)

y=[15,13,14.5,17,20,25,26,26,24,22,18,15]

plt.plot(x,y)

plt.show()


画表格库matplotlib的基本应用


每一个Axes的绘图方法在matplotlib.pyplot模块中都有一个对应的函数,这个函数在“当前”坐标空间上完成绘制,如果当前没有坐标空间,便创建一个坐标空间及其所依附的图。上面的代码我们可以写得更简单一点


绘图函数希望数据是 numpy.array 或者 numpy.ma.masked_array 类型的,或者能够作为numpy.asarray 输入的对象。一些类似于数组的类,如pandas数据对象和numpy.matrix可能不会按预期工作,所以我们应该先将他们转成 numpy.array 类型。、


np.random.seed(19680801) # seed the random number generator.

data = {'a': np.arange(50),

'c': np.random.randint(0, 50, 50),

'd': np.random.randn(50)}

data['b'] = data['a'] + 10 * np.random.randn(50)

data['d'] = np.abs(data['d']) * 100


fig, ax = plt.subplots(figsize=(5, 2.7))

ax.scatter('a', 'b', c='c', s='d', data=data)

ax.set_xlabel('entry a')

ax.set_ylabel('entry b')

plt.show( )


画表格库matplotlib的基本应用


网格线——设置格式


grid(color = 'color', linestyle = 'linestyle', linewidth = number)

参数说明:


color:'b' 蓝色,'m' 洋红色,'g' 绿色,'y' 黄色,'r' 红色,'k' 黑色,'w' 白色,'c' 青绿色,'#008000' RGB 颜色符串。


linestyle:'‐' 实线,'‐‐' 破折线,'‐.' 点划线,':' 虚线。


linewidth:设置线的宽度,可以设置一个数字。


画表格库matplotlib的基本应用


x = np.linspace(0, 10, 200)

data_obj = {'x': x,

'y1': 2 * x + 1,

'y2': 3 * x + 1.2,

'mean': 0.5 * x * np.cos(2*x) + 2.5 * x + 1.1}

fig, ax = plt.subplots()

#填充两条线之间的颜色

ax.fill_between('x', 'y1', 'y2', color='yellow', data=data_obj)

# Plot the "centerline" with `plot`

ax.plot('x', 'mean', color='black', data=data_obj)

plt.show()


画表格库matplotlib的基本应用


散点图

x = np.arange(10)

y = np.random.randn(10)

plt.scatter(x, y, color='red', marker='+')

plt.show()


画表格库matplotlib的基本应用


条形图

np.random.seed(1)

x = np.arange(5)

y = np.random.randn(5)


fig, axes = plt.subplots(ncols=2, figsize=plt.figaspect(1./2))


vert_bars = axes[0].bar(x, y, color='lightblue', align='center')

horiz_bars = axes[1].barh(x, y, color='lightblue', align='center')

#在水平或者垂直方向上画线

axes[0].axhline(0, color='gray', linewidth=2)

axes[1].axvline(0, color='gray', linewidth=2)

plt.show()

————————————————


画表格库matplotlib的基本应用


图 Artists 数组的大小为5,我们可以通过这些 Artists 对条形图的样式进行更改,如下例:fig, ax = plt.subplots()

vert_bars = ax.bar(x, y, color='lightblue', align='center')


# We could have also done this with two separate calls to `ax.bar` and numpy boolean indexing.

for bar, height in zip(vert_bars, y):

if height < 0:

bar.set(edgecolor='darkred', color='salmon', linewidth=3)

plt.show()


画表格库matplotlib的基本应用


直方图

np.random.seed(19680801)

n_bins = 10

x = np.random.randn(1000, 3)

fig, axes = plt.subplots(nrows=2, ncols=2)

ax0, ax1, ax2, ax3 = axes.flatten()

colors = ['red', 'tan', 'lime']

ax0.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors)

ax0.legend(prop={'size': 10})

ax0.set_title('bars with legend')

ax1.hist(x, n_bins, density=True, histtype='barstacked')

ax1.set_title('stacked bar')

ax2.hist(x, histtype='barstacked', rwidth=0.9)

ax3.hist(x[:, 0], rwidth=0.9)

ax3.set_title('different sample sizes')

fig.tight_layout()


画表格库matplotlib的基本应用


饼图

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'

sizes = [15, 30, 45, 10]

explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')


fig1, (ax1, ax2) = plt.subplots(2)

ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True)

ax1.axis('equal')

ax2.pie(sizes, autopct='%1.2f%%', shadow=True, startangle=90, explode=explode,

pctdistance=1.12)

ax2.axis('equal')

ax2.legend(labels=labels, loc='upper right')

————————————————

饼图自动根据数据的百分比画饼.。labels是各个块的标签,如子图一。autopct=%1.1f%%表示格式化百分比精确输出,explode,突出某些块,不同的值突出的效果不一样。pctdistance=1.12百分比距离圆心的距离,默认是0.6


画表格库matplotlib的基本应用

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章