python - Add matrix in X-axis using matplotlib -


i try add matrices in x-axis using matplotlib. code wrote is:

#!/bin/python  import sys import numpy np import math import decimal  import matplotlib.pyplot plt import matplotlib.mlab mlab  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

however, painful part add x-labels. show expected output in figure. http://i.stack.imgur.com/b6h3u.png

i tried method mentioned in post how display matrix in matplotlib annotations .

but not work in case. or thoughts appreciated. lot!

you there; links provided in question, can solve 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 np import math import decimal import matplotlib.pyplot plt import matplotlib.mlab mlab 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 require latex on system.

  • this can take while render: because matplotlib developed create high quality plots, plus additional latex rendering underneath each label.

  • i have offset tick labels using xaxis.set_tick_params(pad=15), because brackets around matrix, tick labels ended inside plot

  • they ways (e.g. using more rcparams) change font size or used latex font.

the resulting figure is:

enter image description here


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -