简体   繁体   English

使用matplotlib在X轴上添加矩阵

[英]Add matrix in X-axis using matplotlib

I try to add matrices in X-axis using matplotlib. 我尝试使用matplotlib在X轴上添加矩阵。 The code I wrote is: 我写的代码是:

#!/bin/python

import sys
import numpy as np
import math
import decimal 
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab 
from matplotlib import rcParams

def plot():           
    N = 6
    ind = np.arange(N)
    ind_label = ['1X', '2X' , '3X' , '4X', '5X', '6X']

    y = [1.60, 1.65, 1.70, 1.75, 1.80]
    m1 = [1.62, 1.64, 1.64, 1.71, 1.7, 1.68]
    m2 = [1.61 , 1.7, 1.7, 1.8, 1.75, 1.75]
    m3 = [1.63 , 1.69, 1.7, 1.67, 1.64, 1.61]

    width = 0.2

    fig = plt.figure()
    ax = fig.add_subplot(111)

    rec_m1 = ax.bar(ind, m1, width, color='r', align='center')
    rec_m2 = ax.bar(ind+width, m2, width, color='g', align='center')
    rec_m3 = ax.bar(ind+width*2, m3, width, color='b', align='center')

    ax.set_ylabel('Value',fontsize=20)
    ax.set_xlabel('Matrix', fontsize=20)
    ax.tick_params(axis='both', labelsize=17)
    ax.set_xticks(ind+width)
    ax.set_xticklabels(ind_label, fontsize=18)
    ax.axis([-0.2, 5.6, 1.58, 1.82])

    ax.legend((rec_m1[0],rec_m2[0],rec_m3[0]),('Method 1','Method 2','Method 3'), loc="upper right",shadow=True)

    plt.grid()
    plt.tight_layout()
    plt.show()

if __name__ == '__main__':
    plot()

The current output figure is: http://i.stack.imgur.com/DJVxA.png 当前输出图是: http : //i.stack.imgur.com/DJVxA.png

However, the most painful part is to add X-labels. 但是,最痛苦的部分是添加X标签。 I show the expected output in the figure. 我在图中显示了预期的输出。 http://i.stack.imgur.com/b6h3U.png http://i.stack.imgur.com/b6h3U.png

I tried the method mentioned in this post How to display a matrix in the Matplotlib annotations . 我尝试了本文中提到的方法如何在Matplotlib批注中显示矩阵

But it does not work in my case. 但这对我来说不起作用。 Any help or thoughts would be appreciated. 任何帮助或想法将不胜感激。 Thanks a lot! 非常感谢!

You were almost there; 你快到了; with the links provided in your question, you can solve it as follows: 使用问题中提供的链接,可以按以下步骤解决问题:

ax.set_xticklabels([r"$\left[ \begin{array}{cc} 0 & 1 \\ 1 & 0 \end{array}\right]$",
                    r"$\left[ \begin{array}{cc} 0 & 1 \\ 5 & 0 \end{array}\right]$",
                    r"$\left[ \begin{array}{cc} 0 & 1 \\ 10 & 0 \end{array}\right]$",
                    r"$\left[ \begin{array}{cc} 0 & 1 \\ 30 & 0 \end{array}\right]$",
                    r"$\left[ \begin{array}{cc} 0 & 1 \\ 50 & 0 \end{array}\right]$",
                    r"$\left[ \begin{array}{cc} 0 & 1 \\ 100 & 0 \end{array}\right]$"])

The total code becomes: 总代码为:

#!/bin/python

import sys
import numpy as np
import math
import decimal
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib import rcParams

def plot():
    N = 6
    ind = np.arange(N)
    ind_label = ['1X', '2X' , '3X' , '4X', '5X', '6X']

    y = [1.60, 1.65, 1.70, 1.75, 1.80]
    m1 = [1.62, 1.64, 1.64, 1.71, 1.7, 1.68]
    m2 = [1.61 , 1.7, 1.7, 1.8, 1.75, 1.75]
    m3 = [1.63 , 1.69, 1.7, 1.67, 1.64, 1.61]

    width = 0.2

    rcParams['text.usetex'] = True
    fig = plt.figure()
    ax = fig.add_subplot(111)

    rec_m1 = ax.bar(ind, m1, width, color='r', align='center')
    rec_m2 = ax.bar(ind+width, m2, width, color='g', align='center')
    rec_m3 = ax.bar(ind+width*2, m3, width, color='b', align='center')

    ax.set_ylabel('Value',fontsize=20)
    ax.set_xlabel('Matrix', fontsize=20)
    ax.tick_params(axis='both', labelsize=17)
    ax.set_xticks(ind+width)
    ax.set_xticklabels(ind_label, fontsize=18)
    ax.axis([-0.2, 5.6, 1.58, 1.82])

    ax.legend((rec_m1[0],rec_m2[0],rec_m3[0]),('Method 1','Method 2','Method 3'), loc="upper right",\
shadow=True)
    ax.set_xticklabels([r"$\left[ \begin{array}{cc} 0 & 1 \\ 1 & 0 \end{array}\right]$",
                        r"$\left[ \begin{array}{cc} 0 & 1 \\ 5 & 0 \end{array}\right]$",
                        r"$\left[ \begin{array}{cc} 0 & 1 \\ 10 & 0 \end{array}\right]$",
                        r"$\left[ \begin{array}{cc} 0 & 1 \\ 30 & 0 \end{array}\right]$",
                        r"$\left[ \begin{array}{cc} 0 & 1 \\ 50 & 0 \end{array}\right]$",
                        r"$\left[ \begin{array}{cc} 0 & 1 \\ 100 & 0 \end{array}\right]$"])
    ax.xaxis.set_tick_params(pad=15)

    plt.grid()
    plt.tight_layout()
    plt.show()

if __name__ == '__main__':
    plot()

A few notes: 一些注意事项:

  • You will require LaTeX on your system. 您将在系统上需要LaTeX。

  • This can take a while to render: this is because Matplotlib was developed to create high quality plots, plus the additional LaTeX rendering underneath for each label. 渲染可能要花一些时间:这是因为Matplotlib是为创建高质量图而开发的,另外还为每个标签添加了额外的LaTeX渲染。

  • I have offset the tick labels using xaxis.set_tick_params(pad=15) , because with the brackets around the matrix, the tick labels ended up inside the plot 我已经使用xaxis.set_tick_params(pad=15)抵消了刻度线标签,因为随着矩阵周围的括号,刻度线标签最终出现在绘图内

  • They are probably ways (eg using more rcParams ) to change the font size or the used LaTeX font. 它们可能是更改字体大小或使用的LaTeX字体的方式(例如,使用更多的rcParams )。

The resulting figure is: 结果数字为:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM