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 ...
随机推荐
- JUC详解--【Foam番茄】
1.什么是JUC java.util 工具包 业务:普通的线程代码 Thread Runnable 没有返回值,效率相比于 Callable 相对较低! 2.线程和进程 进程:一个程序,QQ.exe ...
- 编程入门选什么语言好?C 语言还是Python ?为你解析
前面我分享过计算机行业已经成了学校选择排名第一,家长和学生都很看好计算机类专业.现在IT行业也越来越火爆,程序员越来越被人看好.面对相比同龄人高薪资的诱惑,人们很难不心动,即使秃头也值得! 那么问题来 ...
- equals的推荐写法
我们在重写equals的时候必须满足几个原则,否则,类在容器和其他场景下会出现奇怪的行为: 1.A.equals(A)=true, 2.对称性.即A.equals(B)=true;则B.equals( ...
- @RequestParam,@RequestBody,@PathVariable注解还分不清吗?
前言 在使用 SpringMVC 开发时,经常遇到前端传递的各种参数,比如 form 表单,JSON 数据,String[] 数组,再或者是最常见的 String 字符串等等,总之大部分场景都是在标题 ...
- idea使用帮助
IDEA激活码形式,扫码二维码回复 激活码 自提,秒激活,持续更新.回复的是> 激活码 2020.2以上版本的 IDEA 请跳转至该链接:https://t.1yb.co/3ntg 2018.3 ...
- 第11.10节 Python正则表达式的非贪婪模式的重复匹配:'*?', '+?',和 '??'
在<第11.9节 Pytho正则表达式的贪婪模式和非贪婪模式>老猿简单介绍了贪婪模式和非贪婪模式,并说明'', '+',和 '?' 修饰符都是 贪婪的:它们在字符串进行尽可能多的匹配.有时 ...
- PyQt(Python+Qt)学习随笔:clicked和clicked(bool)信号连接同名函数出现的问题
在Qt中,控件中的clicked()信号和clicked(bool)信号是两个不同的信号,映射槽函数时,clicked()信号映射到的槽函数是不带参的,clicked(bool)信号映射到的槽函数是带 ...
- 直接插入排序(python实现)
这篇博文用来介绍直接插入排序 直接插入排序基本思想: 每次将一个待排序的记录插入到已经排好序的数据区中,直到全部插入完为止 直接插入排序算法思路: 在直接插入排序中,数据元素分为了有序区和无序区两个部 ...
- Scrum 冲刺第一天
一.团队信息 1.团队名称 挑战极限队 2.团队成员 张博愉(3118005074) 张润柏(3118005075) 郑堉涵(3118005077) 周伟建(3118005079) 林梓琦(31180 ...
- 【Alpha冲刺阶段】Scrum Meeting Daily5
[Alpha冲刺阶段]Scrum Meeting Daily5 1.会议简述 会议开展时间 2020/5/27 8:30-9:00 PM 会议基本内容摘要 大家讲述了自己的任务完成情况以及遇到的问题 ...