matplotlib学习日记(九)-图形样式
(一)刻度线定位器和刻度格式器的使用方法
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)) #生成8x8的画布
ax = fig.add_subplot(111) #向画布添加1行1列的子区,并且生成Axes实例ax ax.xaxis.set_major_locator(MultipleLocator(1.0))
'''ax.xaxis是ax的x轴实例,语句会在x轴的1倍处
分别设置主刻度线,其中参数MultipleLocator(1.0)
就是设置主刻度线的显示位置
'''
ax.yaxis.set_major_locator(MultipleLocator(1.0))
'''
次刻度线的显示位置,MultipleLocator(1.0)表示
将每一份主刻度线区间等分4份
'''
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))
#set_minor_formatter设置次刻度线精度,FuncFormatter(minor_tick)控制位置精度 ax.tick_params("y", which="major", length = 15, width=2.0, color="r")#刻度线样式的设置
ax.tick_params(which="manor", 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.grid(linestyle="-", linewidth=0.5, color="r", zorder=0) plt.show()
(二)刻度标签和刻度线样式的定制化
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():
#ax.xaxis.get_ticklabels()是使用get_ticklabels()方法获得Text实例列表,用for循环遍历设置
ticklabel.set_color("slateblue")
ticklabel.set_fontsize(18)
ticklabel.set_rotation(30) for tickline in ax.yaxis.get_ticklines():
tickline.set_color("lightgreen")
tickline.set_markersize(20)
tickline.set_markeredgewidth(2)
plt.show()
(三)货币和时间序列样式的刻度标签
import matplotlib.pyplot as plt
import numpy as np
from calendar import month_name, day_name
#日期标签通过导入标准库calender中的day_name实现日期的刻度标签
from matplotlib.ticker import FormatStrFormatter
'''货币标签是通过FormatStrFormatter(r"$\yen%1.1f$")
作为参数值代入实例方法Axes.set_major_formmatter()中
实现格式化坐标轴标签,r"$\yen%1.1f$"是用来生成保留两位有效数字的人民币计量的刻度标签'''
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="c") 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()
(四)有指示注释与无指示注解(annotate)
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"))
'''
有指示注释使用annotate函数,ax.annotate(str, xy, xycoords, xytext, textcoords, weight, color, arrowprops)
s------>注释内容
xy------>被解释内容的位置
xycoords------>xy的坐标系统,参数值“data”表示与折线图使用相同坐标系统
xytext-------->注释内容所在位置,如果是矩形,左下角所在位置
textcoords----->xytext的坐标系统
weight--------->注释内容的显示风格
color--------->注释内容的颜色
arrowprops---->指示箭头的属性,箭头风格,颜色等等
'''
ax.text(2.8, 0.4, "$y=\sin(x)$", fontsize=20, color="b",
bbox=dict(facecolor="y", alpha=.5))
#无指示注释
plt.show()
(五)圆角文本框的设置
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=.6) plt.grid(ls=":", color="gray", alpha=.5) plt.text(6, 0, "Matplotlib", size=30, rotation=30,
bbox=dict(boxstyle="round", ec="#8968CD", fc="#FFE1FF"))
#boxstyle="round"控制着圆角,还可改成square,circle等
plt.show()
(六)文本的水印效果
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=.6) plt.grid(ls=":", color="gray", alpha=.5) plt.text(1, 2, "Matplotlib", fontsize=50, color="gray",alpha=.5)
#水印通过alpha控制
plt.show
(七)圆角线框的有弧度指示注解
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="-|>", connectionstyle="angle, angleA=0, angleB=45, rad=10", color="r")
#connectionstyle控制着箭头的走向
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=.5) plt.show()
(八)有箭头指示的趋势线
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")
#connectionstyle控制着箭头的走向
ax.annotate("", (3*np.pi/2, np.sin(3*np.pi/2)+0.5), xytext=(np.pi/2, np.sin(np.pi/2)+0.5),
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")
#arrow(起点,xy增量,样式)
ax.grid(ls=":", color="gray", alpha=.5) plt.show()
matplotlib学习日记(九)-图形样式的更多相关文章
- matplotlib学习日记(四)-绘制直方统计图形
(一)柱状图-应用在定性数据的可视化场景或者离散型数据,条形图和柱状图相似,只不过是函数barh import matplotlib as mpl import matplotlib.pyplot a ...
- matplotlib学习日记(三)------简单统计图
(一)函数bar()---------绘制柱状图 import matplotlib as mpl import matplotlib.pyplot as plt mpl.rcParams[" ...
- matplotlib学习日记(一)------图表组成元素
1.使用函数绘制matplotlib的图表组成元素 (1)函数plot---变量的变化趋势 import matplotlib.pyplot as plt import numpy as np x ...
- matplotlib学习日记(十)-划分画布的主要函数
(1)函数subplot()绘制网格区域中的几何形状相同的子区布局 import matplotlib.pyplot as plt import numpy as np '''函数subplot的介绍 ...
- matplotlib学习日记(十)-共享绘图区域的坐标轴
(1)共享单一绘图区域的坐标轴 ''' 上一讲介绍了画布的划分,有时候想将多张图放在同一个绘图区域, 不想在每个绘图区域只绘制一幅图形,这时候借助共享坐标轴的方法实现在一个绘图区 绘制多幅图形的目的. ...
- matplotlib学习日记(七)---误差棒图
(一)误差棒图----误差置信区间的表示 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.1, 0.6, 10 ...
- matplotlib学习日记(二)----图表组成练习
''' 将前面的知识进行练习 plot,scatter,legend等 ''' import matplotlib.pyplot as plt import numpy as np from matp ...
- matplotlib学习日记(十一)---坐标轴高阶应用
(一)设置坐标轴的位置和展示形式 (1)向画布中任意位置添加任意数量的坐标轴 ''' 通过在画布的任意位置和区域,讲解设置坐标轴的位置和坐标轴的展示形式的实现方法, 与subplot,subplots ...
- matplotlib学习日记(八)----完善统计图
(一)再说legend() import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 2.1, 0.1) y = np.p ...
随机推荐
- 方格取数(number) 题解(dp)
题目链接 题目大意 给你n*m个方格,每个格子有对应的值 你从(1,1)出发到(n,m)每次只能往下往上往右,走过的点则不能走 求一条路线使得走过的路径的权值和最大 题目思路 如果只是简单的往下和往右 ...
- zk与eureka区别
cap永远的神!
- Java数据结构(十二)—— 霍夫曼树及霍夫曼编码
霍夫曼树 基本介绍和创建 基本介绍 又称哈夫曼树,赫夫曼树 给定n个权值作为n个叶子节点,构造一棵二叉树,若该树的带权路径长度(wpl)达到最小,称为最优二叉树 霍夫曼树是带权路径长度最短的树,权值较 ...
- Spring Boot 2.4.0 发布,配置文件重大调整,不要乱升级!!
前段时间 Spring Boot 2.4.0 发布了,栈长作了一个新特性全盘解读,其中介绍了一个很重要的变革,那就是配置文件. 配置文件可是每个框架的核心,不得不搞清楚,所以,这篇栈长就带大家深入实战 ...
- 从JMM透析volatile与synchronized原理,图文并茂
在面试.并发编程.一些开源框架中总是会遇到 volatile 与 synchronized .synchronized 如何保证并发安全?volatile 语义的内存可见性指的是什么?这其中又跟 JM ...
- 大数据开发-Hive-常用日期函数&&日期连续题sql套路
前面是常用日期函数总结,后面是一道连续日期的sql题目及其解法套路. 1.当前日期和时间 select current_timestamp -- 2020-12-05 19:16:29.284 2.获 ...
- Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析
Python使用property函数和使用@property装饰器都能定义属性的get.set及delete的访问方法,他们的相同点主要如下三点: 1.定义这些方法后,代码中对相关属性的访问实际上都会 ...
- PyQt(Python+Qt)学习随笔:QTableWidgetItem项文本和项对齐的setText、setTextAlignment方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget部件中的QTableWidgetItem项的文本可以通过text()和set ...
- ⑤SpringCloud 实战:引入Zuul组件,开启网关路由
这是SpringCloud实战系列中第4篇文章,了解前面第两篇文章更有助于更好理解本文内容: ①SpringCloud 实战:引入Eureka组件,完善服务治理 ②SpringCloud 实战:引入F ...
- (window)Docker的镜像使用
镜像加速 镜像默认是通过 DockerHub 拉取的,国内可能有些困难,会报以下错误: net/http: TLS handshake timeout 所以,需要配置国内的加速服务地址: 官方地址:h ...