美文网首页
matplotlib画3d图形时候如何设置z轴刻度格式

matplotlib画3d图形时候如何设置z轴刻度格式

作者: 凌烟阁主5221 | 来源:发表于2019-09-29 13:03 被阅读0次

    最近一直忙着做实验,最后需要将实验结果用图表的方式展示出来。

    其中有一个二维表,我打算用3d柱状图来表示,并且为了与其他实验图的画风一致,需要修改各轴的格式,刻度等

    其中x y轴的格式容易改,z轴的格式改起来很麻烦,期待matplotlib下次更新吧。

    这里需要用到Axes3D 模块

    import numpy as np

    import matplotlib.pyplot as plt

    from matplotlib.ticker import MultipleLocator, FormatStrFormatter,FuncFormatter

    # This import registers the 3D projection, but is otherwise unused.

    from mpl_toolkits.mplot3d import Axes3D

    '''

    "#9467bd"紫色

    "#ff7f0e"橙色

    '#1f77b4'蓝色

    '#2ca02c'绿色

    '#d62728'红色

    '''

    def to_zoomX1(temp, position):

        return int(50*temp)

    def to_zoomX2(temp, position):

        return int(-50*temp)

    fig = plt.figure(figsize=(5, 5))  # 画布宽长比例

    axes3d = Axes3D(fig)

    _x = np.arange(1, 5)

    _y = np.arange(1, 5)

    print(_x, _y)

    _xx, _yy = np.meshgrid(_x, _y)

    x, y = _xx.ravel(), _yy.ravel() #ravel扁平化

    top = [这里是设置的16个数据,表示柱子的高度如,1.2,2.3等]

    bottom = np.zeros_like(top)#每个柱的起始位置

    width = depth = 0.34 #x,y方向的宽厚

    new_colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#9467bd']

    for fail in range(1, 5):

        color = new_colors[fail-1]#每一排的颜色一样

        for success in range(1, 5):

            axes3d.bar3d(success-0.1, fail-0.25, 0, width, depth,

                      top[(fail-1)*4 + (success-1)], color=color, shade=True)  #每次画一个柱

    xmajorLocator = MultipleLocator(1) #将x主刻度标签设置为1的倍数

    ymajorLocator = MultipleLocator(1) #将x主刻度标签设置为1的倍数

    axes3d.xaxis.set_major_locator(xmajorLocator)

    axes3d.yaxis.set_major_locator(ymajorLocator)

    axes3d.xaxis.set_major_formatter(FuncFormatter(to_zoomX1))#将X,Y,轴的坐标轴放大50倍,原来是1234,这样就会显示50,100,150,200

    axes3d.yaxis.set_major_formatter(FuncFormatter(to_zoomX2))

    plt.tick_params(labelsize=12)

    labels = axes3d.get_xticklabels() + axes3d.get_yticklabels() + axes3d.get_zticklabels()

    [label.set_fontsize(12) for label in labels]

    [label.set_fontweight('bold') for label in labels]#主要是这三行代码,将三个轴的格式统一设置为大小为12,粗体

    axes3d.set_xlabel('Postive Rewards',fontsize=12, fontweight="bold", labelpad=4.2)#labelpad用于调整标签到x轴之间的距离,越大则标签离轴越远

    axes3d.set_ylabel('Negative Rewards',fontsize=12, fontweight="bold", labelpad=4.2)

    axes3d.set_zlabel('Average Turns',fontsize=12, fontweight="bold")

    plt.savefig("3d.pdf")

    plt.show()

    如果要画其他的3d图形,推荐官网api

    https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

    相关文章

      网友评论

          本文标题:matplotlib画3d图形时候如何设置z轴刻度格式

          本文链接:https://www.haomeiwen.com/subject/lsglyctx.html