[tools] Plot (matplotlib, seaborn)

让text in svg 可以被选中 直接用plt.savefig x.svg 里面的图片中的text无法被选中。采取下面的方案。 import matplotlib as mpl make svg render text to be selectable mpl.rcParams 'svg.fonttype' = 'none' display svg in

让text in svg 可以被选中

直接用plt.savefig(x.svg)里面的图片中的text无法被选中。采取下面的方案。

import matplotlib as mpl
# make svg render text to be selectable
mpl.rcParams['svg.fonttype'] = 'none'

display svg in jupyter

import seaborn as sns
import matplotlib.pyplot as plt

# Set the output format to SVG
%config InlineBackend.figure_format = 'svg'

if png, resolution setting

sns.set(rc={"figure.dpi":300, 'savefig.dpi':300})
sns.set_context('notebook')
sns.set_style("ticks")

savefig 放在plt.show()和plt.close()前

把show comment掉,可以不用显示出来而直接save

plt.plot(x, y)
plt.savefig('plot.png')  # You can specify the file format by the extension (e.g., png, pdf, svg)
plt.show()
plt.close()

save svg

plt.savefig('plot.svg',bbox_inches='tight') # bbox_inches='tight'可以确保文字在图片里

# Alternatively, you can specify the format explicitly:
# plt.savefig('plot.svg', format='svg',bbox_inches='tight')

savefig里面有bbox_inches='tight' 和pad_inches=0到0.几

tight 和pad inches=0可以让保存的图片没有边缘的白边。如果想要多一些白边,可以设置bbox_inches=tight然后pad_inches=0.3之类的调一下。

plot function里不用放plt.show()

以防在function外面还可以加一些plt.title之类的。

SVG settings

disable matplotlib warning GUI outside

import matplotlib
matplotlib.use('SVG')