Đây là bản cập nhật của tôi từ bài đăng của Sascha. Bây giờ nó có thể:
- Hiển thị xoay, nhãn Tex
- Hiển thị Xticks và Yticks
- Xử lý tốt hơn các điểm đánh dấu
- Lưới bật/tắt
- Trục tốt hơn và xử lý liệt kê huyền thoại
- Duy trì kích thước hình
Mã dưới đây:
from scipy.io import loadmat
import numpy as np
import matplotlib.pyplot as plt
def plotFig[filename,fignr=1]:
d = loadmat[filename,squeeze_me=True, struct_as_record=False]
matfig = d['hgS_070000']
childs = matfig.children
ax1 = [c for c in childs if c.type == 'axes']
if[len[ax1] > 0]:
ax1 = ax1[0]
legs = [c for c in childs if c.type == 'scribe.legend']
if[len[legs] > 0]:
legs = legs[0]
else:
legs=0
pos = matfig.properties.Position
size = np.array[[pos[2]-pos[0],pos[3]-pos[1]]]/96
plt.figure[fignr,figsize=size]
plt.clf[]
plt.hold[True]
counter = 0
for line in ax1.children:
if line.type == 'graph2d.lineseries':
if hasattr[line.properties,'Marker']:
mark = "%s" % line.properties.Marker
if[mark != "none"]:
mark = mark[0]
else:
mark = '.'
if hasattr[line.properties,'LineStyle']:
linestyle = "%s" % line.properties.LineStyle
else:
linestyle = '-'
if hasattr[line.properties,'Color']:
r,g,b = line.properties.Color
else:
r = 0
g = 0
b = 1
if hasattr[line.properties,'MarkerSize']:
marker_size = line.properties.MarkerSize
else:
marker_size = -1
x = line.properties.XData
y = line.properties.YData
if[mark == "none"]:
plt.plot[x,y,linestyle=linestyle,color=[r,g,b]]
elif[marker_size==-1]:
plt.plot[x,y,marker=mark,linestyle=linestyle,color=[r,g,b]]
else:
plt.plot[x,y,marker=mark,linestyle=linestyle,color=[r,g,b],ms=marker_size]
elif line.type == 'text':
if counter == 0:
plt.xlabel["$%s$" % line.properties.String,fontsize =16]
elif counter == 1:
plt.ylabel["$%s$" % line.properties.String,fontsize = 16]
elif counter == 3:
plt.title["$%s$" % line.properties.String,fontsize = 16]
counter += 1
plt.grid[ax1.properties.XGrid]
if[hasattr[ax1.properties,'XTick']]:
if[hasattr[ax1.properties,'XTickLabelRotation']]:
plt.xticks[ax1.properties.XTick,ax1.properties.XTickLabel,rotation=ax1.properties.XTickLabelRotation]
else:
plt.xticks[ax1.properties.XTick,ax1.properties.XTickLabel]
if[hasattr[ax1.properties,'YTick']]:
if[hasattr[ax1.properties,'YTickLabelRotation']]:
plt.yticks[ax1.properties.YTick,ax1.properties.YTickLabel,rotation=ax1.properties.YTickLabelRotation]
else:
plt.yticks[ax1.properties.YTick,ax1.properties.YTickLabel]
plt.xlim[ax1.properties.XLim]
plt.ylim[ax1.properties.YLim]
if legs:
leg_entries = tuple[['$' + l + '$' for l in legs.properties.String]]
py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best','best']
MAT_locs=['North','South','East','West','NorthEast', 'NorthWest', 'SouthEast', 'SouthWest','Best','none']
Mat2py = dict[zip[MAT_locs,py_locs]]
location = legs.properties.Location
plt.legend[leg_entries,loc=Mat2py[location]]
plt.hold[False]
plt.show[]
Các tệp .fig là các tệp .mat [chứa cấu trúc anycodings_matlab], anycodings_matlab Xem //undocumentedmatlab.com/blog/fig-files-pormat/anycodings_matlab struct], anycodings_matlab see //undocumentedmatlab.com/blog/fig-files-format/
Như tài liệu tham khảo bạn cung cấp cho các tiểu bang, các cấu trúc anycodings_matlab chỉ được hỗ trợ cho anycodings_matlab v7.1: //www.scipy.org/cookbook/reading_mat_filesanycodings_matlab structs are only supported up to anycodings_matlab v7.1: //www.scipy.org/Cookbook/Reading_mat_files
Vì vậy, trong matlab tôi lưu bằng -v7:
plot[[1 2],[3 4]]
hgsave[gcf,'c','-v7'];
Sau đó, trong Python 2.6.4 Tôi sử dụng:
>>> from scipy.io import loadmat
>>> x = loadmat['c.fig']
>>> x
{'hgS_070000': array[[[]], dtype=object], '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Nov 18 12:02:31 2011', '__globals__': []}
>>> x['hgS_070000'][0,0].__dict__
{'handle': array[[[1]], dtype=uint8], 'children': array[[[]], dtype=object], '_fieldnames': ['type', 'handle', 'properties', 'children', 'special'], 'type': array[[u'figure'], dtype='