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


import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator, 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)) def minor_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()
-------------------------------------------------------------------------------------
图 5.2


import matplotlib.pyplot as plt
import numpy 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 in ax.xaxis.get_ticklabels():
ticklabel.set_color("slateblue")
ticklabel.set_fontsize(18)
ticklabel.set_rotation(30) for ticklabel in ax.yaxis.get_ticklabels():
ticklabel.set_color("lightgreen")
ticklabel.set_fontsize(20)
ticklabel.set_rotation(2) plt.show()
-------------------------------------------------------------------------------------
图 5.3


import matplotlib.pyplot as plt
import numpy as np from calendar import month_name, day_name
from matplotlib.ticker import FormatStrFormatter 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()
-------------------------------------------------------------------------------------
图 5.4


import matplotlib.pyplot as plt
import numpy 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()
-------------------------------------------------------------------------------------
图 5.5


import matplotlib.pyplot as plt
import numpy 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()
-------------------------------------------------------------------------------------
图 5.6


import matplotlib.pyplot as plt
import numpy 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()
-------------------------------------------------------------------------------------
图 5.7


import matplotlib.pyplot as plt
import numpy 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()
-------------------------------------------------------------------------------------
图 5.8


import matplotlib.pyplot as plt
import numpy 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()
-------------------------------------------------------------------------------------
图 5.9


import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np from matplotlib.sankey import Sankey 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()
-------------------------------------------------------------------------------------
图 5.10


import matplotlib.pyplot as plt
import matplotlib.patheffects as pes import numpy as np x=np.linspace(0.5, 3.5, 100)
y=np.sin(x) fontsize=23 plt.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()
-------------------------------------------------------------------------------------
图 5.11


import matplotlib.pyplot as plt
import numpy 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()
-------------------------------------------------------------------------------------
《Python数据可视化之matplotlib实践》 源码 第二篇 精进 第五章的更多相关文章
- Python数据可视化——使用Matplotlib创建散点图
Python数据可视化——使用Matplotlib创建散点图 2017-12-27 作者:淡水化合物 Matplotlib简述: Matplotlib是一个用于创建出高质量图表的桌面绘图包(主要是2D ...
- python 数据可视化(matplotlib)
matpotlib 官网 :https://matplotlib.org/index.html matplotlib 可视化示例:https://matplotlib.org/gallery/inde ...
- Python数据可视化库-Matplotlib(一)
今天我们来学习一下python的数据可视化库,Matplotlib,是一个Python的2D绘图库 通过这个库,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率图,条形图,错误图,散点图等等 废 ...
- Python数据可视化之Matplotlib实现各种图表
数据分析就是将数据以各种图表的形式展现给领导,供领导做决策用,因此熟练掌握饼图.柱状图.线图等图表制作是一个数据分析师必备的技能.Python有两个比较出色的图表制作框架,分别是Matplotlib和 ...
- 重温《STL源码剖析》笔记 第五章
源码之前,了无秘密 ——侯杰 序列式容器 关联式容器 array(build in) RB-tree vector set heap map priority-queue multiset li ...
- Python数据可视化利器Matplotlib,绘图入门篇,Pyplot介绍
Pyplot matplotlib.pyplot是一个命令型函数集合,它可以让我们像使用MATLAB一样使用matplotlib.pyplot中的每一个函数都会对画布图像作出相应的改变,如创建画布.在 ...
- Python数据可视化库-Matplotlib(二)
我们接着上次的继续讲解,先讲一个概念,叫子图的概念. 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.a ...
- Python数据可视化之matplotlib
常用模块导入 import numpy as np import matplotlib import matplotlib.mlab as mlab import matplotlib.pyplot ...
- python数据可视化(matplotlib)
- python数据可视化-matplotlib入门(7)-从网络加载数据及数据可视化的小总结
除了从文件加载数据,另一个数据源是互联网,互联网每天产生各种不同的数据,可以用各种各样的方式从互联网加载数据. 一.了解 Web API Web 应用编程接口(API)自动请求网站的特定信息,再对这些 ...
随机推荐
- 像 Google SRE 一样 OnCall
在 Google SRE 的著作<Google运维解密>(原作名:Site Reliability Engineering: How Google Runs Production Syst ...
- (六)基于Scrapy爬取网易新闻中的新闻数据
需求:爬取这国内.国际.军事.航空.无人机模块下的新闻信息 1.找到这五个板块对应的url 2.进入每个模块请求新闻信息 我们可以明显发现''加载中'',因此我们判断新闻数据是动态加载出来的. 3. ...
- 如果你也用过 struts2.简单介绍下 springMVC 和 struts2 的区别有哪些?
a.springmvc 的入口是一个 servlet 即前端控制器,而 struts2 入口是一个 filter 过虑器. b.springmvc 是基于方法开发(一个 url 对应一个方法),请求参 ...
- spring jpa restful请求示例
创建项目 导入jar包mysql 数据库和连接池jar <dependency> <groupId>org.springframework.boot</groupId&g ...
- Navicat Premium v16.0.6 绿色破解版
这里版本:Navicat Premium v16.0.6.0 ,这个是绿色版,不需要安装,启动Navicat.exe即可用 破解工具:NavicatKeygenPatch(其它版本也能破解) 1.下载 ...
- Windows CSC提权漏洞复现(CVE-2024-26229)
漏洞信息 Windows CSC服务特权提升漏洞. 当程序向缓冲区写入的数据超出其处理能力时,就会发生基于堆的缓冲区溢出,从而导致多余的数据溢出到相邻的内存区域.这种溢出会损坏内存,并可能使攻击者能够 ...
- python 注册nacos 进行接口规范定义
背景: 一般场景 python服务经常作为java下游的 算法服务或者 数据处理服务 但是使用http 去调用比较不灵活,通过注册到nacos上进行微服务调用才是比较爽的 1.定义feginapi的接 ...
- HBCK2修复hbase2的常见场景
上一文章已经把HBCK2 怎么在小于hbase2.0.3版本的编译与用法介绍了,解决主要场景 查看hbase存在的问题 一.使用hbase hbck命令 hbase hbck命令是对hbase的元数据 ...
- IS-IS总结
IS-IS 管理距离115 ISIS是链路状态协议 封装在数据链路层,所以没有协议号 使用SPF算法计算最短路径 没有骨干区的概念 使用IIH(ISIS ...
- MySQL自定义函数(User Define Function)开发实例——发送TCP/UDP消息
开发背景 当数据库中某个字段的值改为特定值时,实时发送消息通知到其他系统. 实现思路 监控数据库中特定字段值的变化可以用数据库触发器实现.还需要实现一个自定义的函数,接收一个字符串参数,然后将这个字符 ...