gpt4 book ai didi

python - Matplotlib 设置,用于保存用于发布的图形和来自交互式绘图的报告

转载 作者:行者123 更新时间:2023-11-28 16:48:26 26 4
gpt4 key购买 nike

我写了一个 python 函数来获取任何当前的交互式图形并修改标签和刻度标签的文本大小。然后删除顶部和右侧的轴以获得更优选的图形输出格式。然后它输出数字。我遇到的问题是设置轴的大小,以便 xlabel 保留在图形的 Canvas 上,因此在输出时显示出来。我知道在创建轴时可以手动执行此操作:

ax = plt.axes([0.125,0.2,0.95-0.125,0.95-0.2])

在绘制图形后是否可以设置这些参数?如果是这样,我如何获得访问权限或着手修改它? (也可以在matplotlibrc文件中设置吗?)。

另一个问题是关于图形是否有图例:

我通常使用这组图例参数,但事后如何设置其他规范?我只弄清楚了下面的那些:

legArgs = dict(bbox_to_anchor=[.44,1.18], borderpad=0.1, labelspacing=0,
handlelength=1.8, handletextpad=0.05, frameon=False, ncol=5,
columnspacing=0.02)
#ncol,numpoints,columnspacing,title,bbox_transform,prop
leg = ax.legend(tuple(legendLabels),tuple(modFreq),'upper center',**legArgs)
leg.get_title().set_fontsize(tick_size)
leg.set_frame_on(False)
leg.set_bbox_to_anchor([1.1,1.05])
for i in leg.get_texts():
i.set_fontsize(8)

具有辅助功能的完整绘图功能:

def plot_for_publication_output(fig, ax, filename, dir_addition='', 
fulldir=None, column_width=1, dpi=600,
out_formats=['.eps','.pdf','.png']):
"""
column_width assumes a standard 2 column width format (default 1 column,
2 meaning spreads across both columns)
"""
if fulldir == None:
store_dir = os.path.expanduser('~/'+'Dropbox/Research/Results/' + dir_addition)
else:
store_dir = fulldir

spineLineWidth = 0.5
tick_size = 9
fontlabel_size = 10.5
mydpi = dpi
outExt = out_formats
dashs = ['-.', '-', '--', ':']
dashes = [(1.5, 0), (3.5, 1.5), (1.5, 1.5, 3,1.5), (1, 1)]
plot_color = "bgrkcykw"
fig_width_pt = 246.0 * column_width # Get this from LaTeX using
# \showthe\columnwidth
inches_per_pt = 1.0 / 72.27 # Convert pt to inches
golden_mean = (np.sqrt(5) - 1.0) / 2.0 # Aesthetic ratio
fig_width = fig_width_pt * inches_per_pt # width in inches
fig_height = fig_width * golden_mean # height in inches
fig.set_size_inches(fig_width, fig_height)
plotSetAxisTickLabels(ax, tick_size)
plotSetAxisLabels(ax, fontlabel_size)
#params = {'axes.labelsize': fontlabel_size, 'text.fontsize': fontlabel_size,
# 'legend.fontsize': fontlabel_size, 'xtick.labelsize': tick_size,
# 'ytick.labelsize': tick_size, 'text.usetex': True,
# 'figure.figsize': fig_size}
#plt.rcParams.update(params)
#ax = plt.axes([0.125, 0.2, 0.95 - 0.125, 0.95 - 0.2])
set_spineLineWidth(ax,spineLineWidth)
clear_spines(ax)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
for i in outExt:
plt.savefig(os.path.join(store_dir, filename) + i, dpi = mydpi)

def clear_spines(ax):
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
def set_spineLineWidth(ax, lineWidth):
for i in ax.spines.keys():
ax.spines[i].set_linewidth(lineWidth)
def showOnlySomeTicks(x, pos):
s = str(int(x))
if x == 5000:
return '5e3'#'%.0e' % x
return ''
def plotSetAxisTickLabels(ax, size=16):
for i in ax.xaxis.get_ticklabels():
i.set_fontsize(size)
for i in ax.yaxis.get_ticklabels():
i.set_fontsize(size)

def plotSetAxisLabels(ax, size=16):
ax.xaxis.get_label().set_fontsize(size)
ax.yaxis.get_label().set_fontsize(size)

最佳答案

您可以使用 set_position 函数更改轴位置:

# save the instance 
ax = plt.axes([0.125,0.2,0.95-0.125,0.95-0.2])

# change the axes position
ax.set_position([0.52, 0.85, 0.16, 0.075])

上述命令的文档可在 matplotlib axes documentation 中找到.

您还可以在 legend in the API 中找到图例实例的函数,您可能已经找到,或者在子对象的文档中四处挖掘,您也可以按照与此行相同的方式调用子函数:

leg.get_title().set_fontsize(tick_size)

关于python - Matplotlib 设置,用于保存用于发布的图形和来自交互式绘图的报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11201987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com