python数据可视化之matplotlib实践pdf下载_《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第四章...

图 4.1

importmatplotlibimportmatplotlib.pyplot as pltimportnumpy as np#设置matplotlib正常显示中文和负号

matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体显示中文

matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号

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

y=np.sin(x)

y1=np.cos(x)

plt.plot(x,y, label=r"$\sin(x)$")

plt.plot(x,y1,label=r"$\cos(x)$")

plt.legend(loc="lower left")

plt.title("正弦函数和余弦函数的折线图")

plt.show()

View Code

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

图 4.2

importmatplotlibimportmatplotlib.pyplot as pltimportnumpy as np#设置matplotlib正常显示中文和负号

matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体显示中文

matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号

x=np.arange(0, 2.1, 0.1)

y=np.power(x, 3)

y1=np.power(x,2)

y2=np.power(x, 1)

plt.plot(x, y, ls='-', lw=2, label='$x^{3}$')

plt.plot(x, y1, ls='-', lw=2, label='$x^{2}$', c='r')

plt.plot(x, y2, ls='-', lw=2, label='$x^{1}$', c='y')

plt.legend(loc='upper left', bbox_to_anchor=(0.05, 0.95), ncol=3,

title="power function", shadow=True, fancybox=True)

plt.show()

View Code

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

图 4.3

importmatplotlibimportmatplotlib.pyplot as pltimportnumpy as np#设置matplotlib正常显示中文和负号

matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体显示中文

matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号

x=np.linspace(-2, 2, 1000)

y=np.exp(x)

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

plt.title("center demo")

plt.title("Left Demo", loc="left", fontdict={"size":"xx-large","color":"r","family":"Times New Roman"})

plt.title("right demo", loc="right", family="Comic Sans MS", size=20, style="oblique",

color="c")

plt.show()

View Code

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

图 4.4

importmatplotlibimportmatplotlib.pyplot as pltimportnumpy as np#设置matplotlib正常显示中文和负号

matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体显示中文

matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号

elements=["面粉", "砂糖", "奶油", "草莓酱", "坚果"]

weight=[40, 15, 20, 10, 15]

colors=["#1b9e77", "#d95f02", "#7570b3", "#66a61e", "#e6ab02"]

wedges, texts, autotexts=plt.pie(weight, autopct="%3.1f%%", textprops=dict(color="w"),

colors=colors)

plt.legend(wedges, elements, fontsize=12, title="配料表", loc="center left",

bbox_to_anchor=(0.91, 0, 0.3, 1))#调整百分比字体类型和大小

plt.setp(autotexts, size=15, weight="bold")#调整标签字体类型和大小#plt.setp(texts, size=32)

plt.title("果酱面包配料比例表")

plt.show()

View Code

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

图 4.5

importmatplotlibimportmatplotlib.pyplot as pltimportnumpy as np#设置matplotlib正常显示中文和负号

matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体显示中文

matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号

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

y=np.sin(x)

plt.subplot(211)

plt.plot(x, y)

plt.subplot(212)

plt.xlim(-2*np.pi, 2*np.pi)

plt.xticks(np.pi*np.arange(-4, 5)/2,

[r"$-2\pi$", r"$-3\pi/2$", r"$-2\pi$", r"$-\pi$", r"$0$",

r"$\pi/2$", r"$\pi$", r"$3\pi/2$", r"$2\pi$", ])

plt.plot(x, y)

plt.show()

View Code

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

图 4.6

importmatplotlibimportmatplotlib.pyplot as pltimportnumpy as np#设置matplotlib正常显示中文和负号

matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体显示中文

matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号

time=np.arange(1, 11, 0.5)

machinePower=np.power(time, 2)+0.7plt.plot(time, machinePower, linestyle="-", linewidth=2, color="r")#逆序设置坐标轴刻度标签

plt.xlim(10, 1)

plt.xlabel("使用年限")

plt.ylabel("机器功率")

plt.title("机器损耗曲线")

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

plt.show()

View Code

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

图 4.7

importmatplotlibimportmatplotlib.pyplot as pltimportnumpy as np#设置matplotlib正常显示中文和负号

matplotlib.rcParams['font.sans-serif']=['SimHei'] #用黑体显示中文

matplotlib.rcParams['axes.unicode_minus']=False #正常显示负号

labels=["A难度水平", "B难度水平", "C难度水平", "D难度水平"]

students=[0.35, 0.15, 0.20, 0.30]

explode=(0.1, 0.1, 0.1, 0.1)

colors=["#377eb8", "#e41a1c", "#4daf4a", "#984ea3"]

plt.pie(students, explode=explode, labels=labels, autopct="%1.1f%%", startangle=45,

shadow=True, colors=colors)

colLabels=["A难度水平", "B难度水平", "C难度水平", "D难度水平"]

rowLabels=["学生选择试卷人数"]

studentValues=[[350, 150, 200, 300]]

colColors=["#377eb8", "#e41a1c", "#4daf4a", "#984ea3"]

plt.table(cellText=studentValues, cellLoc="center", colWidths=[0.25]*4,

colLabels=colLabels, colColours=colColors, rowLabels=rowLabels,

rowLoc="center", colLoc="center", loc="bottom", rowColours='r')

plt.title("选择不同难度测试试卷的学生占比")

plt.show()

View Code

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

你可能感兴趣的:(python数据可视化之matplotlib实践pdf下载_《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第四章...)