【Python数据可视化(四)】定制图表

设置坐标轴标签的透明度和大小

  matplotlib组织图表过程:
  最上层是一个Figure实例,包含了所有可见的和其他一些不可见的内容。该Figure实例包含了一个Axes实例字段Figure.axes。Axes实例几乎包含了我们所关心的所有东西,如所有的线、点、刻度和标签。因此,当调用plot()方法时,就会向Axes.lines列表加一个线条的实例。Axes实例也包含了XAxis和YAxis实例的引用,分别指向相应的x轴和y轴。XAxis和YAxis管理坐标轴、标签、刻度、刻度标签、定位器和格式器的绘制。matplotlib提供了一个helper方法来迭代这些标签,分别是matplotlib.pyplot.xlabel()和matplotlib.pyplot.ylabel()。

  设置阴影和透明度
    title=plt.title('title',fontsize=18,verticalalignment='bottom)
    pe=patheffects.withSimplePatchShadow(offset_xy = (1,-1),shadow_rbgFace=(1.0,0.0,0.0),patch_alpha=0.8
    title.set_path_effects([pe])
  说明:
  添加一个标题,然后设置字体的大小,并设置标题文本的垂直对齐方式为bottom;
  offset_xy——设置阴影的角度(offset,偏移)#;shadow_rgbFace——设置阴影的颜色(1.0,0.0,0.0)(全红色,无绿色,无蓝色);patch_alpha——设置阴影的透明度

为图表线条添加阴影

  为了区分图表中的某一条线条,或者为了保持包含图表在内的所有表格的总体风格,有时需要为图表(或者直方图)添加阴影效果。为了向图表中的线条或者矩形条添加阴影,需要使用,matplotlib内置的transformation框架:

坐标系Transformation对象描述
DataAxes.transData表示用户的数据坐标系
AxesAxes.transAxes表示Axes坐标系,其中(0,0)表示轴的左下角,(1,1)表示轴的右上角
FigureFigure.transFigure是Figure的坐标系,其中(0,0)表示轴的左下角,(1,1)表示轴的右上角
DisplayNone表示用户视窗的像素坐标系,其中(0,0)表示视窗的左下角,(width,height)元组表示显示界面的右上角
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as tf

# 解决中文无法显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


def setup(layout=None):
    fig = plt.figure()
    ax = fig.add_subplot(layout)
    return fig, ax


def get_signal():
    t = np.arange(0., 2.5, 0.01)
    s = np.sin(5 * np.pi * t)
    return t, s


def plot_signal(t, s):
    line, = plt.plot(t, s, linewidth=5, color='magenta')
    return line,


def make_shadow(fig, axes, line, t, s):
    delta = 2 / 72.
    offset = tf.ScaledTranslation(delta, -delta, fig.dpi_scale_trans)
    offset_transform = axes.transData + offset
    # 我们绘制相同的数据,但是现在使用偏移变换;zorder——在线下渲染它
    axes.plot(t, s, linewidth=5, color='gray', transform=offset_transform, zorder=0.5 * line.get_zorder())


if __name__ == '__main__':
    fig, axes = setup(111)
    t, s = get_signal()
    line, = plot_signal(t, s)
    make_shadow(fig, axes, line, t, s)
    axes.set_title('添加阴影')
    plt.show()

在这里插入图片描述

  说明:
  首先通过setup()创建figure和axes,然后得到一个信号(或者生成一个正弦波数据),在plot_signal()方法中绘制出基本的信号图,最后进行阴影坐标转换并在make_shadow()方法中绘制出阴影。使用偏移效果创建一个偏移对象,把偏移放置在原始对象之下并偏移几个点的距离,dx和dy的值以点为单位,因为点是1/72英寸,向右移动偏移对象2pt,向下移动偏移对象2pt。

向图表添加数据表

import matplotlib.pyplot as plt
import numpy as np
plt.figure()
ax = plt.gca()
y = np.random.randn(9)
col_labels = ['coll','col2','col3']
row_labels = ['row1','row2','row3']
table_vals = [[11,12,13],[21,22,23],[28,29,30]]
row_colors = ['red','gold','green']

my_table = plt.table(cellText=table_vals,
                     colWidths=[0.1]*3,
                     rowLabels=row_labels,
                     colLabels=col_labels,
                     rowColours=row_colors,
                     loc='upper right')
plt.plot(y)
plt.show()

在这里插入图片描述

  使用plt.table()方式创建一个带单元格的表格,并把它添加到当前坐标轴中。表格可以有(可选的)行标题和列标题。每个单元格包含文本,表格的列宽和行高时可以指定的。基本的函数签名如下:
在这里插入图片描述

使用subplot(子区)

import matplotlib.pyplot as plt

plt.figure(0)
axes1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
axes2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
axes3 = plt.subplot2grid((3, 3), (1, 2))
axes = plt.subplot2grid((3, 3), (2, 0))
axes = plt.subplot2grid((3, 3), (2, 1), colspan=2)
# 整理标签大小
all_axes = plt.gcf().axes
for ax in all_axes:
    for ticklabel in ax.get_xticklabels() + ax.get_yticklabels():
        ticklabel.set_fontsize(10)
plt.suptitle('Demo')
plt.show()

在这里插入图片描述

定制化网格

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid
from matplotlib.cbook import get_sample_data


def get_demo_image():
    f = get_sample_data('axes_grid/bivariate_normal.npy', asfileobj=True)
    z = np.load(f)
    return z, (-3, 4, -4, 3)


def get_grid(fig=None, layout=None, nrows_ncols=None):
    assert fig is not None
    assert layout is not None
    assert nrows_ncols is not None
    grid = ImageGrid(fig, layout, nrows_ncols=nrows_ncols, axes_pad=0.05,
                     add_all=True, label_mode='L')
    return grid


def load_images_to_grid(grid, Z, *images):
    min, max = Z.min(), Z.max()
    for i, image in enumerate(images):
        axes = grid[i]
        axes.imshow(image, origin='lower', vmin=min, vmax=max, interpolation='nearest')


if __name__ == '__main__':
    fig = plt.figure(1, (8, 6))
    grid = get_grid(fig, 111, (1, 3))
    Z, extent = get_demo_image()
    image1 = Z
    image2 = Z[:, :10]
    image3 = Z[:, 10:]
    load_images_to_grid(grid, Z, image1, image2, image3)
    plt.draw()
    plt.show()

在这里插入图片描述

  补充说明:
  在函数get_demo_image中,从matplotlib的样本目录中加载数据;
  grid列表保存了axes网格
  变量image1、image2、image3保存了Z的切片数据,这些数据都是根据grid列表的多个坐标轴切分的;
  循环遍历所有的网格,调用标准的imshow()方法绘制出image1,image2,image3的数据,matplotlib确保所有图形的渲染是整洁的,排列时整齐的。

创建等高线图

  等值线使用数值相等的各点连成的曲线。Z矩阵的等高线图由许多等高线表示,这里的Z被视为相对于X-Y平面的高度,Z的最小值为2,并且必须包含至少两个不同的值。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
# 解决中文无法显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 模拟信号处理器
def process_signals(x, y):
    return (1 - (x ** 2 + y ** 2)) * np.exp(-y ** 3 / 3)


# 线性信号数据
x = np.arange(-1.5, 1.5, 0.1)
y = np.arange(-1.5, 1.5, 0.1)
X, Y = np.meshgrid(x, y)
Z = process_signals(X, Y)
# 等值线的数量,前两个参数是等值线的取值范围,第三个参数越小,数量越多
N = np.arange(-1, 1.5, 0.3)
# 添加等值线标签
CS = plt.contour(Z, N, linewidths=2, cmap=mpl.cm.jet)
plt.clabel(CS, inline=True, fmt='%1.1f', fontsize=10)
plt.colorbar(CS)
plt.title('函数:$z=(1-x^2+y^2)e^{-y^3/3}$')
plt.show()

在这里插入图片描述
  contour函数的属性:

调用描述
contour(Z)绘制Z(数组)的等高线。自动选择水平值
contour(X,Y,Z)绘制X,Y和Z的等高线。X和Y数组为(x,y)平面坐标
contour(Z,N)绘制Z的等高线,其中水平数由N决定。自动选择水平值
contour(X,Y,Z,N)
contour(Z,V)绘制等高线,水平值在V中指定
contour(X,Y,Z,V)
contour(...,V)填充V序列中的水平值之间的len(V)-1个区域
contour(Z,**kwargs)使用关键字参数控制一般线条属性(颜色、线宽、起点、颜色映射表等)

填充图表底层区域

import matplotlib.pyplot as plt
from math import sqrt
t = range(1000)
y = [sqrt(i) for i in t]
plt.plot(t,y,color='red',lw=2)
plt.fill_between(t,y,color='green')
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(np.pi * x)
y2 = 1.7 * np.sin(4 * np.pi * x)
fig = plt.figure()
axes1 = fig.add_subplot(211)
axes1.fill_between(x, y1, y2, where=y2 <= y1, facecolor='blue', interpolate=True)
axes1.fill_between(x, y1, y2, where=y2 >= y1, facecolor='gold', interpolate=True)
axes1.set_title('Blue->y2<=y1,gold->y2>=y1')
axes1.set_ylim(-2, 2)
# 屏蔽y2中大于1.0的值
y2 = np.ma.masked_greater(y2, 1.0)
axes2 = fig.add_subplot(212, sharex=axes1)
axes2.plot(x, y1, x, y2, color='black')
axes2.fill_between(x, y1, y2, where=y2 <= y1, facecolor='blue', interpolate=True)
axes2.fill_between(x, y1, y2, where=y2 >= y1, facecolor='gold', interpolate=True)
axes2.set_title('Same as above')
axes2.set_ylim(-2, 2)
# 网格
axes2.grid('on')
plt.show()

在这里插入图片描述

绘制极线图

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

figsize = 7
colormap = lambda r: cm.Set2(r / 20.)
# 柱的数量
N = 18
fig = plt.figure(figsize=(figsize, figsize))
ax = fig.add_axes([0.2, 0.2, 0.7, 0.7], polar=True)
theta = np.arange(0.0, 2 * np.pi, 2 * np.pi / N)
radii = 20 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
bars = ax.bar(theta, radii, width=width, bottom=0.0)
for r, bar in zip(radii, bars):
    bar.set_facecolor(colormap(r))
    bar.set_alpha(0.6)
plt.show()

在这里插入图片描述

  说明:
  首先,创建了一个正方形的图表,并向其添加极限坐标轴。其实图表不必是正方形的,但是如果不这样的话,极线图就是椭圆形的了。然后为角度(theta)集合和极线距离(radii)生成随机值。因为绘制的是极线条,需要为每一个极线条提供宽度集合,因此需要生成一些宽度值。因为matplotlib.axes.bar接收值数组,所以不必在这个生成的数据集合上做循环遍历,只需要调用一次bar函数,并传入所有的参数。为了能够容易区分每一个极线条,需要循环遍历添加到ax(坐标轴)的每一个极线条,并定制化其外观(表面颜色和透明度)。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZoomToday

给作者倒一杯卡布奇诺

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值