《Python数据可视化之matplotlib实践》 源码 第三篇 演练 第八章

Posted Hello_BeautifulWorld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Python数据可视化之matplotlib实践》 源码 第三篇 演练 第八章相关的知识,希望对你有一定的参考价值。

图  8.1

 

import matplotlib.pyplot as plt
import numpy as np

plt.axes([0.05, 0.7, 0.3, 0.3], frameon=True, facecolor="y", aspect="equal")
plt.plot(np.arange(3), [0, 1, 0], color="blue", linewidth=2, linestyle="--")


plt.axes([0.3, 0.4, 0.3, 0.3], frameon=True, facecolor="y", aspect="equal")
plt.plot(2+np.arange(3), [0, 1, 0], color="blue", linewidth=2, linestyle="-")


plt.axes([0.55, 0.1, 0.3, 0.3], frameon=True, facecolor="y", aspect="equal")
plt.plot(4+np.arange(3), [0, 1, 0], color="blue", linewidth=2, linestyle=":")



plt.show()
View Code

 

================================================

 

图  8.2

 

 

 

import matplotlib.pyplot as plt
import numpy as np

plt.axes([0.05, 0.7, 0.3, 0.3], frameon=True, facecolor="y", aspect="equal")
plt.plot(np.arange(3), [0, 1, 0], color="blue", linewidth=2, linestyle="--")
plt.ylim(0, 1.5)
plt.axis("image")


plt.axes([0.3, 0.4, 0.3, 0.3], frameon=True, facecolor="y", aspect="equal")
plt.plot(2+np.arange(3), [0, 1, 0], color="blue", linewidth=2, linestyle="-")
plt.ylim(0, 1.5)
plt.axis([2.1, 3.9, 0.5, 1.9])

plt.axes([0.55, 0.1, 0.3, 0.3], frameon=True, facecolor="y", aspect="equal")
plt.plot(4+np.arange(3), [0, 1, 0], color="blue", linewidth=2, linestyle=":")
plt.ylim(0, 1.5)
plt.axis("off")


plt.show()
View Code

 

 

================================================

 

图  8.3

 

 

import matplotlib.pyplot as plt
import numpy as np

plt.axis([3, 7, -0.5, 3])
plt.plot(4+np.arange(3), [0, 1, 0], color="blue", linewidth=2, linestyle="-")


plt.show()
View Code

 

 

================================================

 

图  8.4

 

 

import matplotlib.pyplot as plt

ax1=plt.subplot(121)
ax1.set_xticks(range(0, 251, 50))

plt.grid(True, axis="x")







ax2=plt.subplot(122)
ax2.set_xticks([])
plt.grid(True, axis="x")




plt.show()
View Code

 

================================================

 

图  8.5

 

 

import matplotlib.pyplot as plt


ax1=plt.subplot(221)
plt.setp(ax1.get_xticklabels(), visible=True)
plt.setp(ax1.get_xticklines(), visible=True)
plt.grid(True, axis="x")




ax2=plt.subplot(222)
plt.setp(ax2.get_xticklabels(), visible=True)
plt.setp(ax2.get_xticklines(), visible=False)
plt.grid(True, axis="x")



ax3=plt.subplot(223)
plt.setp(ax3.get_xticklabels(), visible=False)
plt.setp(ax3.get_xticklines(), visible=True)
plt.grid(True, axis="x")


ax4=plt.subplot(224)
plt.setp(ax4.get_xticklabels(), visible=False)
plt.setp(ax4.get_xticklines(), visible=False)
plt.grid(True, axis="x")


plt.show()
View Code

 

 

================================================

 

图  8.6

 

 

import matplotlib.pyplot as plt
import numpy as np

x=np.linspace(0.5, 2*np.pi, 20)
y=np.random.randn(20)


markerline, stemlines, baseline = plt.stem(x, y)


plt.setp(markerline, color="chartreuse", marker="D")
plt.setp(stemlines, linestyle="-.")
baseline.set_linewidth(2)

plt.show()
View Code

 

 

================================================

 

图  8.7

 

 

import matplotlib.pyplot as plt
import numpy as np


from calendar import  day_name
from matplotlib.ticker import FormatStrFormatter


fig=plt.figure()


ax=fig.add_axes([0.2, 0.2, 0.7, 0.7])
ax.spines["bottom"].set_position(("outward", 10))
ax.spines["left"].set_position(("outward", 10))
ax.spines["top"].set_color("none")
ax.spines["right"].set_color("none")

x=np.arange(1, 8, 1)
y=2*x+1


ax.scatter(x, y, c="orange", s=50, edgecolors="orange")


for tickline in ax.xaxis.get_ticklines():
    tickline.set_color("blue")
    tickline.set_markersize(8)
    tickline.set_markeredgewidth(5)
    

for ticklabel in ax.get_xmajorticklabels():
    ticklabel.set_color("slateblue")
    ticklabel.set_fontsize(15)
    ticklabel.set_rotation(20)
    
    
ax.yaxis.set_major_formatter(FormatStrFormatter(r"$\\yen%1.1f$"))
plt.xticks(x, day_name[0:7], rotation=20)
ax.yaxis.set_ticks_position("left")
ax.xaxis.set_ticks_position("bottom")



    
for tickline in ax.yaxis.get_ticklines():
    tickline.set_color("lightgreen")
    tickline.set_markersize(8)
    tickline.set_markeredgewidth(5)
    

for ticklabel in ax.get_ymajorticklabels():
    ticklabel.set_color("green")
    ticklabel.set_fontsize(18)



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

plt.show()  
View Code

 

================================================

 

图  8.8

 

 

import matplotlib.pyplot as plt
import numpy as np

x=np.linspace(-2*np.pi, 2*np.pi, 1000)
y=np.sin(x)

ax1=plt.subplot(221)

ax1.spines["right"].set_color("none")
ax1.spines["top"].set_color("none")
ax1.set_xlim(-2*np.pi, 2*np.pi)
ax1.set_ylim(-1.0, 1.0)
plt.title(r"$a$")
plt.scatter(x, y, marker="+", color="b")



ax2=plt.subplot(222)

ax2.spines["right"].set_color("none")
ax2.spines["top"].set_color("none")
ax2.xaxis.set_ticks_position("bottom")
ax2.set_xlim(-2*np.pi, 2*np.pi)
ax2.set_ylim(-1.0, 1.0)
plt.title(r"$b$")
plt.scatter(x, y, marker="+", color="b")



ax3=plt.subplot(223)

ax3.spines["right"].set_color("none")
ax3.spines["top"].set_color("none")
ax3.yaxis.set_ticks_position("left")
ax3.set_xlim(-2*np.pi, 2*np.pi)
ax3.set_ylim(-1.0, 1.0)
plt.title(r"$c$")
plt.scatter(x, y, marker="+", color="b")


ax4=plt.subplot(224)

ax4.spines["right"].set_color("none")
ax4.spines["top"].set_color("none")
ax4.xaxis.set_ticks_position("bottom")
ax4.yaxis.set_ticks_position("left")
ax4.set_xlim(-2*np.pi, 2*np.pi)
ax4.set_ylim(-1.0, 1.0)
plt.title(r"$d$")
plt.scatter(x, y, marker="+", color="b")



plt.show()
View Code

 

 

 

 

================================================

 

图  8.9

 

 

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np


mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False


x=np.linspace(-2*np.pi, 2*np.pi, 200)
y=np.sin(x)
y1=np.cos(x)

ax=plt.subplot(111)


ax.plot(x, y, ls="-", lw=2, label="$\\sin(x)$")
ax.plot(x, y1, ls="-", lw=2, label="$\\cos(x)$")

ax.legend(loc="lower left")

plt.title("$\\sin(x)$"+""+"$\\cos(x)$"+"函数")

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


plt.xticks([-2*np.pi, -3*np.pi/2, -1*np.pi, -1*(np.pi)/2, 0, 
            (np.pi)/2, np.pi, 3*np.pi/2, 2*np.pi], 
           ["$-2\\pi$", "$-3\\pi/2$", "$-\\pi$", "$-\\pi/2$", 
            "$0$", "$\\pi/2$", "$\\pi$", "$3\\pi/2$", "$2\\pi$"])



ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")


ax.spines["bottom"].set_position(("data", 0))
ax.spines["left"].set_position(("data", 0))


ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")

plt.show()
View Code

 

================================================

 

 

 

以上是关于《Python数据可视化之matplotlib实践》 源码 第三篇 演练 第八章的主要内容,如果未能解决你的问题,请参考以下文章

Python数据可视化之matplotlib实践 源码 第一篇 入门 第三章

Python数据可视化之matplotlib实践 源码 第一篇 入门 第三章

《Python数据可视化之matplotlib实践》 源码 第四篇 扩展 第十一章

Python 数据可视化之-[Matplotlib]

Python 数据可视化之-[Matplotlib]

Python数据可视化之Matplotlib实现各种图表