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

图 5.1

1088037-20200522163805229-192170376.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as npfrom matplotlib.ticker importAutoMinorLocator, MultipleLocator, FuncFormatter

x=np.linspace(0.5, 3.5, 100)

y=np.sin(x)

fig=plt.figure(figsize=(8, 8))

ax=fig.add_subplot(111)

ax.xaxis.set_major_locator(MultipleLocator(1.0))

ax.yaxis.set_major_locator(MultipleLocator(1.0))

ax.xaxis.set_minor_locator(AutoMinorLocator(4))

ax.yaxis.set_minor_locator(AutoMinorLocator(4))defminor_tick(x, pos):if not x%1.0:return ""

return "%.2f"%x

ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick))

ax.tick_params("y", which='major',length=15, width=2.0, colors='r')

ax.tick_params(which='minor', length=5, width=1.0, labelsize=10, labelcolor='0.25')

ax.set_xlim(0,4)

ax.set_ylim(0,2)

ax.plot(x, y, c=(0.25, 0.25, 1.00), lw=2, zorder=10)#ax.plot(x, y, c=(0.25, 0.25, 1.00), lw=2, zorder=0)

ax.grid(linestyle='-', linewidth=0.5, color='r', zorder=0)#ax.grid(linestyle='-', linewidth=0.5, color='r', zorder=10)#ax.grid(linestyle='--', linewidth=0.5, color='0.25', zorder=0)

plt.show()

View Code

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

图 5.2

1088037-20200522173610388-1438089843.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as np

fig=plt.figure(facecolor=(1.0, 1.0, 0.9412))

ax=fig.add_axes([0.1, 0.4, 0.5, 0.5])for ticklabel inax.xaxis.get_ticklabels():

ticklabel.set_color("slateblue")

ticklabel.set_fontsize(18)

ticklabel.set_rotation(30)for ticklabel inax.yaxis.get_ticklabels():

ticklabel.set_color("lightgreen")

ticklabel.set_fontsize(20)

ticklabel.set_rotation(2)

plt.show()

View Code

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

图 5.3

1088037-20200522174606427-223681847.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as npfrom calendar importmonth_name, day_namefrom matplotlib.ticker importFormatStrFormatter

fig=plt.figure()

ax=fig.add_axes([0.2, 0.2, 0.7, 0.7])

x=np.arange(1, 8, 1)

y=2*x

ax.plot(x, y, ls='-', lw=2, color='orange', marker='o',

ms=20, mfc='c', mec='r')

ax.yaxis.set_major_formatter(FormatStrFormatter(r"$\yen%1.1f$"))

plt.xticks(x, day_name[0:7], rotation=20)

ax.set_xlim(0,8)

ax.set_ylim(0,18)

plt.show()

View Code

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

图 5.4

1088037-20200522175656861-764449435.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as np

x=np.linspace(0.5, 3.5, 100)

y=np.sin(x)

fig=plt.figure(figsize=(8, 8))

ax=fig.add_subplot(111)

ax.plot(x, y, c='b', ls='-', lw=2)

ax.annotate("maximum", xy=(np.pi/2, 1.0), xycoords='data',

xytext=((np.pi/2)+0.15, 0.8), textcoords="data",

weight="bold", color='r',

arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r'))

ax.text(2.8, 0.4, "$y=\sin(x)$", fontsize=20, color='b',

bbox=dict(facecolor='y', alpha=0.5))

plt.show()

View Code

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

图 5.5

1088037-20200522184619061-713422213.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as np

x=np.linspace(0.0, 10, 40)

y=np.random.randn(40)

plt.plot(x, y, ls='-', lw=2, marker='o', ms=20, mfc='orange', alpha=0.6)

plt.grid(ls=':', color='gray', alpha=0.5)

plt.text(6, 0, 'Matplotlib', size=30, rotation=30.0,

bbox=dict(boxstyle='round', ec='#8968CD', fc='#FFE1FF'))

plt.show()

View Code

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

图 5.6

1088037-20200522185011746-1684638273.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as np

x=np.linspace(0.0, 10, 40)

y=np.random.randn(40)

plt.plot(x, y, ls='-', lw=2, marker='o', ms=20, mfc='orange', alpha=0.6)

plt.grid(ls=':', color='gray', alpha=0.5)

plt.text(1, 2, 'Matplotlib', size=50, alpha=0.5)

plt.show()

View Code

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

图 5.7

1088037-20200523110510464-995008149.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as np

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

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

fig=plt.figure()

ax=fig.add_subplot(111)

ax.plot(x, y, ls='-', lw=2)

bbox=dict(boxstyle='round', fc='#7EC0EE', ec='#9B30FF')

arrowprops=dict(arrowstyle='-|>', color='r',

connectionstyle='angle, angleA=0, angleB=90, rad=10')

ax.annotate("single point", (5, np.sin(5)*np.cos(5)),

xytext=(3, np.sin(3)*np.cos(3)),

fontsize=12, color='r', bbox=bbox, arrowprops=arrowprops)

ax.grid(ls=":", color='gray', alpha=0.6)

plt.show()

View Code

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

图 5.8

1088037-20200523122403906-601055768.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as np

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

y=np.sin(x)

fig=plt.figure()

ax=fig.add_subplot(111)

ax.plot(x, y, ls='-', lw=2)

ax.set_ylim(-1.5, 1.5)

arrowprops=dict(arrowstyle='-|>', color='r')

ax.annotate("", (3*np.pi/2, np.sin(3*np.pi/2)+0.15),

xytext=(np.pi/2, np.sin(np.pi/2)+0.15), color='r', arrowprops=arrowprops)

ax.arrow(0.0, -0.4, np.pi/2, 1.2, head_width=0.05, head_length=0.1, fc='g', ec='g')

ax.grid(ls=':', color='gray', alpha=0.6)

plt.show()

View Code

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

图 5.9

1088037-20200616093506574-902626269.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportmatplotlib as mplimportnumpy as npfrom matplotlib.sankey importSankey

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

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

flows=[0.2, 0.1, 0.4, 0.3, -0.6, -0.05, -0.15, -0.2]

labels=['', '', '', '', 'family', 'trip', 'education', 'sport']

orientations=[1, 1, 0, -1, 1, -1, 1, 0]

sankey=Sankey()

sankey.add(flows=flows, labels=labels, orientations=orientations, color='c',

fc='lightgreen', patchlabel='Life Cost', alpha=0.7)

diagrams=sankey.finish()

diagrams[0].texts[4].set_color('r')

diagrams[0].texts[4].set_weight('bold')

diagrams[0].text.set_fontsize(20)

diagrams[0].text.set_fontweight('bold')

plt.title("日常生活的成本开支的流量图")

plt.show()

View Code

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

图 5.10

1088037-20200616123000131-1587816678.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportmatplotlib.patheffects as pesimportnumpy as np

x=np.linspace(0.5, 3.5, 100)

y=np.sin(x)

fontsize=23plt.plot(x, y, ls='--', lw=2)

title='$y=\sin({x})$'xaxis_label='$x\_axis$'yaxis_label="$y\_axis$"title_text_obj=plt.title(title, fontsize=fontsize, va='bottom')

xaxis_label_text_obj=plt.xlabel(xaxis_label,

fontsize=fontsize-3, alpha=1.0)

yaxis_label_text_obj=plt.ylabel(yaxis_label,

fontsize=fontsize-3, alpha=1.0)

title_text_obj.set_path_effects([pes.withSimplePatchShadow()])

pe=pes.withSimplePatchShadow(offset=(1, -1), shadow_rgbFace='r', alpha=0.3)

xaxis_label_text_obj.set_path_effects([pe])

yaxis_label_text_obj.set_path_effects([pe])

plt.show()

View Code

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

图 5.11

1088037-20200616125738016-157293798.png

ContractedBlock.gif

ExpandedBlockStart.gif

importmatplotlib.pyplot as pltimportnumpy as np

x=np.linspace(0.5, 3.5, 100)

y=np.sin(x)

fig=plt.figure(figsize=(8, 8))

ax=fig.add_subplot(111)

box=dict(facecolor='#6959CD', pad=2, alpha=0.4)

ax.plot(x, y, c='b', ls='--', lw=2)

title='$y=\sin({x})$'xaxis_label='$x\_axis$'yaxis_label="$y\_axis$"ax.set_xlabel(xaxis_label, fontsize=18, bbox=box)

ax.set_ylabel(yaxis_label, fontsize=18, bbox=box)

ax.set_title(title, fontsize=23, va='bottom')

ax.yaxis.set_label_coords(-0.08, 0.5)

ax.xaxis.set_label_coords(1.0, -0.05)

ax.grid(ls='-.', lw=1, color='gray', alpha=0.5)

plt.show()

View Code

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值