Notebook
# 也可以用关键字参数进行设置 plt.axis(xmin=NNN, ymax=NNN)
fig.savefig("filename.png") # 通过扩展名来确定图片保存的格式fig.savefig('figpath.svg') # 将图表保存为SVG文件,也可以用Figure对象的实例方法savefig
plt.savefig('plot123_2.png', figsize=[8.0, 6.0], dpi=200) # 设置图片尺寸为8英尺*6英尺,dpi为200
fig.savefig('figpath.png', dpi=400, bbox_inches='tight') # dpi控制“每英寸点数”分辨率,bbox_inches控制图表周围的空白部分('tight'最小白边)
from io import BytesIO buffer = BytesIO() plt.savefig(buffer) # savefig可以写入任何文件型的对象,比如StringIO plot_data = buffer.getvalue()
import matplotlib as mpl mpl.rcParams['interactive'] # 查看当前模式mpl.interactive(False) # 取消互动模式 mpl.rcParams['interactive']
In [1]: import matplotlib.pyplot as plt In [2]: plt.plot([1, 2]) Out[2]: [<matplotlib.lines.Line2D object at 0x26abfd0>] In [3]: plt.plot([2, 1]); # 添加;分号,则不会再打印plot对象 In [4]: We can see that at line [2] we got an output from plot(), while on line [3], which is using the semicolon, the output is suppressed.
# 配置语句格式 mpl.rcParams['<param name>'] = <value># 将figure size设置为4*3英尺 mpl.rcParams['figure.figsize'] = (4, 3)
# 一个配置对象 mpl.rc(('figure', 'savefig'), facecolor='r') # 等同于: # mpl.rcParam['figure.facecolor'] = 'r' # mpl.rcParam['savefig.facecolor'] = 'r'# 两个配置对象 mpl.rc('lines', linewidth=4, color='b') # 等同于: # mpl.rcParam['line.linewidth'] = 4 # mpl.rcParam['line.linecolor'] = 'b'
mpl.rcdefaults() # 重置动态修改后的配置参数,将配置重置为标准设置
import matplotlib as mpl mpl.use('Agg') # to render to file, or to not use a graphical display mpl.use('GTKAgg') # to render to a GTK UI window
fig = plt.figure(figsize=(8,4), dpi=100)
语法: table(cellText=None, cellColours=None, cellLoc='right',colWidths=None, rowLabels=None,rowColours=None,rowLoc='left', colLables=None,colColours=None,colLoc='center', loc='bottom',bbox=None)
t = plt.xlabel('some text', fontsize=16, color='green')
t = plt.xlabel('some text') plt.setp(t, fontsize=16, color='green')
t = plt.xlabel('some text') t.set_fontsize(16) t.set_color('green')
ax.get_xticklabels() # 获取对象 for t in ax.get_xticklabels(): # 获取对象与API方法合用 t.set_fontsize(5.) setp(ax.get_xticklabels(), fontsize=5.) # 获取对象与setp()合用
plt.rcParams['font.sans-serif'] = ['SimHei'] # 将默认字体指定为黑体,得以正常显示中文标签plt.rcParams['axes.unicode_minus'] = False # 解决保存图像时,符号'-'显示为方块的问题
# # RESTART THE NOTEBOOK: the matplotlib backend can only be selected before pylab is imported! # (e.g. Kernel > Restart) # import matplotlib matplotlib.use('svg') import matplotlib.pylab as plt import numpy from IPython.display import Image, SVG# # Now we are using the svg backend to produce SVG vector graphics # fig, ax = plt.subplots() t = numpy.linspace(0, 10, 100) ax.plot(t, numpy.cos(t)*numpy.sin(t)) plt.savefig("test.svg")# # Show the produced SVG file. # SVG(filename="test.svg")
%matplotlib inline %config InlineBackend.figure_format='svg' import matplotlib.pylab as plt import numpy# # Now we are using the SVG vector graphics displaced inline in the notebook # fig, ax = plt.subplots() t = numpy.linspace(0, 10, 100) ax.plot(t, numpy.cos(t)*numpy.sin(t)) plt.savefig("test.svg")
# # RESTART THE NOTEBOOK: the matplotlib backend can only be selected before pylab is imported! # (e.g. Kernel > Restart) # import matplotlib matplotlib.use('Qt4Agg') # or for example MacOSX import matplotlib.pylab as plt import numpy as np# Now, open an interactive plot window with the Qt4Agg backend fig, ax = plt.subplots() t = np.linspace(0, 10, 100) ax.plot(t, np.cos(t) * np.sin(t)) plt.show()