python数据可视化之matplotlib精进_《Python数据可视化之matplotlib实践》 源码 第二篇 精进 第六章...

图 6.1

importmatplotlib.pyplot as pltimportnumpy as np

x=np.linspace(-2*np.pi, 2*np.pi, 200)

y=np.sin(x)

y1=np.cos(x)

plt.subplot(121)

plt.plot(x, y)

plt.subplot(122)

plt.plot(x, y1)

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.2

importmatplotlib.pyplot as pltimportnumpy as np

radii=np.linspace(0, 1, 100)

theta=2*np.pi*radii

ax=plt.subplot(111, polar=True)

ax.plot(theta, radii, color='r', linestyle='-', linewidth=2)

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.3

importmatplotlib as mplimportmatplotlib.pyplot as pltimportnumpy as np

radii=30*np.random.rand(100)

theta=2*np.pi*np.random.rand(100)

colors=np.random.rand(100)

size=50*radii

ax=plt.subplot(111, polar=True)

ax.scatter(theta, radii, s=size, c=colors, cmap=mpl.cm.PuOr, marker='*')

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.4

importmatplotlib.pyplot as pltimportnumpy as np

fig=plt.figure()

x=np.linspace(0.0, 2*np.pi)

y=np.cos(x)*np.sin(x)

ax1=fig.add_subplot(121)

ax1.margins(0.03)

ax1.plot(x, y, ls='-', lw=2, color='b')

ax2=fig.add_subplot(222)

ax2.margins(0.7, 0.7)

ax2.plot(x, y, ls='-', lw=2, color='r')

ax3=fig.add_subplot(224)

ax3.margins(x=0.1, y=0.3)

ax3.plot(x, y, ls='-', lw=2, color='g')

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.5

importmatplotlib as mplimportmatplotlib.pyplot as pltimportnumpy as np

mpl.rcParams["font.sans-serif"]=['SimHei']

mpl.rcParams["axes.unicode_minus"]=False

plt.subplot2grid((2, 3), (0, 0), colspan=2)

x=np.linspace(0.0, 4.0, 100)

y=np.random.randn(100)

plt.scatter(x, y, c="c")

plt.title("散点图", fontsize=10)

plt.subplot2grid((2, 3), (0, 2))

plt.title("空白绘图区域", fontsize=10)

plt.subplot2grid((2, 3), (1, 0), colspan=3)

x=np.linspace(0.0, 4.0, 100)

y1=np.sin(x)

plt.plot(x, y1, lw=2, ls="-")

plt.xlim(0,3)

plt.grid(True, ls=":", c='r')

plt.title("折线图", fontsize=10)

plt.suptitle("subplot2grid()函数的实例展示", fontsize=15)

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.6

importmatplotlib.pyplot as pltimportnumpy as npfrom matplotlib.gridspec importGridSpec

fig=plt.figure()

gs=GridSpec(2, 2)

box={"facecolor":"lightgreen", "pad":3, "alpha":0.2}

x1=np.arange(0, 1e5, 500)

ax1=fig.add_subplot(gs[0, :], facecolor="yellowgreen")

ax1.plot(x1,"k--", lw=2)

ax1.set_ylabel("YLabel0,0-1", bbox=box)

ax1.set_xlabel("XLabel0,0-1", bbox=box)

ax1.yaxis.set_label_coords(-0.1, 0.5)

x2=np.linspace(0, 1000, 10)

y2=np.arange(1, 11, 1)

ax2=fig.add_subplot(gs[1, 0], facecolor="cornflowerblue")

ax2.scatter(x2, y2, s=20, c="grey", marker="s", linewidths=2, edgecolors="k")

ax2.set_ylabel("YLabel10", bbox=box)

ax2.set_xlabel("XLabel10", bbox=box)for ticklabel inax2.get_xticklabels():

ticklabel.set_rotation(45)

ax2.yaxis.set_label_coords(-0.25, 0.5)

ax2.xaxis.set_label_coords(0.5, -0.25)

x3=np.linspace(0, 10, 100)

y3=np.exp(-x3)

ax3=fig.add_subplot(gs[1, 1])

ax3.errorbar(x3, y3, fmt="b-", yerr=0.6*y3, ecolor="lightsteelblue",

elinewidth=2, capsize=0, errorevery=5)

ax3.set_ylabel("YLabel11", bbox=box)

ax3.set_xlabel("XLabel11", bbox=box)

ax3.xaxis.set_label_coords(0.5, -0.25)

ax3.set_ylim(-0.1, 1.1)

ax3.set_yticks(np.arange(0,1.1, 0.1))

gs.tight_layout(fig)

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.7

importmatplotlib as mplimportmatplotlib.pyplot as pltimportnumpy as np

mpl.rcParams["font.sans-serif"]=["FangSong"]

mpl.rcParams["axes.unicode_minus"]=False

font_style=dict(fontsize=18, weight="black")

x=np.linspace(0, 2*np.pi, 500)

y=np.sin(x)*np.cos(x)

fig, ax=plt.subplots(1, 1, subplot_kw=dict(facecolor="cornflowerblue"))

ax.plot(x, y,"k--", lw=2)

ax.set_xlabel("时间(秒)", **font_style)

ax.set_ylabel("振幅", **font_style)

ax.set_title("简单折线图", **font_style)

ax.set_xlim(0,2*np.pi)

ax.set_ylim(-0.65, 0.65)

ax.grid(ls=":", lw=1, color="gray", alpha=0.8)

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.8

importmatplotlib as mplimportmatplotlib.pyplot as pltimportnumpy as np

mpl.rcParams["font.sans-serif"]=["FangSong"]

mpl.rcParams["axes.unicode_minus"]=False

font_style=dict(fontsize=18, weight="black")

x=np.linspace(0, 2*np.pi, 500)

y=np.sin(x)*np.exp(-x)

fig, ax=plt.subplots(1, 2, sharey=True)

ax1=ax[0]

ax1.plot(x, y,"k--", lw=2)

ax1.set_title("折线图")

ax1.grid(ls=":", lw=1, color="gray", alpha=0.8)

ax2=ax[1]

ax2.scatter(x, y, s=10, c="skyblue", marker="o")

ax2.set_title("散点图")

plt.suptitle("创建一张画布和两个子区的绘图模式", **font_style)

plt.show()

View Code

---------------------------------------------------------------------------------------

图 6.9

importmatplotlib.pyplot as pltimportnumpy as np

fig, ax=plt.subplots(2, 3)

colors=["#8dd3c7", "#ffffb3", "#bebada"]

ax[0, 0].bar([1, 2, 3], [0.6, 0.2, 0.8], color=colors,

width=0.5, hatch="///", align="center")

ax[0, 0].errorbar([1, 2, 3], [0.6, 0.2, 0.8], yerr=0.1,

capsize=0, ecolor="#377eb8", fmt="o:")

ax[0, 0].set_ylim(0,1.0)

ax[0,1].errorbar([1, 2, 3], [20, 30, 36], xerr=2, ecolor="#4daf4a",

elinewidth=2, fmt="s", label="ETN")

ax[0,1].legend(loc=3, fancybox=True, shadow=True,

fontsize=10, borderaxespad=0.4)

ax[0,1].set_ylim(10, 40)

ax[0,1].set_xlim(-2, 6)

ax[0,1].grid(ls=":", lw=1, color="grey", alpha=0.5)

x3=np.arange(1, 10, 0.5)

y3=np.cos(x3)

ax[0,2].stem(x3, y3, basefmt="r-", linefmt="b-.",

markerfmt="bo", label="life signal")

ax[0,2].legend(loc=2, fontsize=7, frameon=False, borderpad=0.0, borderaxespad=0.6)

ax[0,2].set_xlim(0, 11)

ax[0,2].set_ylim(-1.1, 1.1)

x4=np.linspace(0, 2*np.pi, 500)

x4_1=np.linspace(0, 2*np.pi, 1000)

y4=np.cos(x4)*np.exp(-x4)

y4_1=np.sin(2*x4_1)

line1, line2=ax[1, 0].plot(x4, y4, "k--", x4_1, y4_1, "r-", lw=2)

ax[1, 0].legend((line1, line2), ("energy", "patience"), loc="upper center",

fontsize=6, ncol=2, framealpha=0.3, mode="expand",

columnspacing=2, borderpad=0.1)

ax[1, 0].set_ylim(-2, 2)

ax[1, 0].set_xlim(0, 2*np.pi)

x5=np.random.rand(100)

ax[1, 1].boxplot(x5, vert=False, showmeans=True, meanprops=dict(color="g"))

ax[1, 1].set_yticks([])

ax[1, 1].set_xlim(-1.1, 1.1)

ax[1, 1].set_ylabel("Micro SD Card")

ax[1, 1].text(-1.0, 1.2, "net weight", fontsize=15, style="italic",

weight="black", family="monospace")

mu=0.0sigma=1.0x6=np.random.randn(10000)

n, bins, patches=ax[1, 2].hist(x6, bins=30, histtype="stepfilled",

cumulative=True, normed=True, color="cornflowerblue", label="Test")

y=((1/(np.sqrt(2*np.pi)*sigma))*np.exp(-0.5*(1/sigma*(bins-mu))**2))

y=y.cumsum()

y/=y[-1]

ax[1, 2].plot(bins, y, "r--", linewidth=1.5, label="Theory")

ax[1, 2].set_ylim(0.0, 1.1)

ax[1, 2].grid(ls=":", lw=1, color="grey", alpha=0.5)

ax[1, 2].legend(loc="upper left", fontsize=8, shadow=True, fancybox=True, framealpha=0.8)

plt.subplots_adjust()

plt.show()

View Code

--------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值