如何 return 带有 matplotlib 和 seaborn 函数的模板空图表
How to return a template empty chart with a function with matplotlib & seaborn
我正在创建几个图表,其中只有实际 plot
发生变化。其他信息没有变化(蓝色框和下图中显示的所有文本)。
我有下面的代码来生成blank chart
截至目前
sns.set(rc={'axes.facecolor':'white','grid.color': '#b0b0b0','axes.edgecolor': 'black','figure.edgecolor':'black'})
fig, ax = plt.subplots(figsize=(15, 10))
gridShape=(25,6)
primaryFontSize = 15
secondaryFontSize = 12
smallFontSize = 10
topHeaderRowSpan=2
infoRowSpan=1
mainChartRowspan = 17
footerStartRow = mainChartRowspan+4
ax1 = plt.subplot2grid(gridShape, (0, 0), colspan=1, rowspan=topHeaderRowSpan)
ax1.text(0.5, 0.5, 'P=f(i)[bar]', va="center", ha="center" , fontsize=primaryFontSize)
ax2 = plt.subplot2grid(gridShape, (0,1), colspan=2, rowspan=topHeaderRowSpan)
ax2.text(0.5, 0.5, pcvType, va="center", ha="center" , fontsize=primaryFontSize)
ax3 = plt.subplot2grid(gridShape, (0,3), colspan=1, rowspan=infoRowSpan)
ax3.text(0.5, 0.5, 'Part Nr:', va="center", ha="center" , fontsize=secondaryFontSize)
ax4 = plt.subplot2grid(gridShape, (1,3), colspan=1, rowspan=infoRowSpan)
ax4.text(0.5, 0.5, partNumber , va="center", ha="center" , fontsize=secondaryFontSize)
ax5 = plt.subplot2grid(gridShape, (0,4), colspan=1, rowspan=infoRowSpan)
ax5.text(0.5, 0.5, 'Serial Nr:', va="center", ha="center" , fontsize=secondaryFontSize)
ax6 = plt.subplot2grid(gridShape, (1,4), colspan=1, rowspan=infoRowSpan)
ax6.text(0.5, 0.5, serailNumber , va="center", ha="center" , fontsize=secondaryFontSize)
ax7 = plt.subplot2grid(gridShape, (0,5), colspan=1, rowspan=infoRowSpan)
ax7.text(0.5, 0.5, 'Manufacturing Date:', va="center", ha="center" , fontsize=secondaryFontSize)
ax8 = plt.subplot2grid(gridShape, (1,5), colspan=1, rowspan=infoRowSpan)
ax8.text(0.5, 0.5, mfgDate , va="center", ha="center" , fontsize=secondaryFontSize)
plotAxis = plt.subplot2grid(gridShape, (3, 0), colspan=6, rowspan=mainChartRowspan)
ax9 = plt.subplot2grid(gridShape, (footerStartRow+1,0), colspan=1, rowspan=3)
ax9.imshow(im, aspect='auto', extent=(0.4, 0.6, .5, .7))
ax10 = plt.subplot2grid(gridShape, (footerStartRow+1,1), colspan=1, rowspan=1)
ax10.text(0.5, 0.5, 'Test Bench:', va="center", ha="center" , fontsize=secondaryFontSize)
ax11 = plt.subplot2grid(gridShape, (footerStartRow+2,1), colspan=1, rowspan=1)
ax11.text(0.5, 0.5, 'Operator:', va="center", ha="center" , fontsize=secondaryFontSize)
ax12 = plt.subplot2grid(gridShape, (footerStartRow+3,1), colspan=1, rowspan=1)
ax12.text(0.5, 0.5, 'Date:', va="center", ha="center" , fontsize=secondaryFontSize)
ax13 = plt.subplot2grid(gridShape, (footerStartRow+1,2), colspan=1, rowspan=1)
ax13.text(0.5, 0.5, testBench , va="center", ha="center" , fontsize=secondaryFontSize)
ax14 = plt.subplot2grid(gridShape, (footerStartRow+2,2), colspan=1, rowspan=1)
ax14.text(0.5, 0.5, operator , va="center", ha="center" , fontsize=secondaryFontSize)
ax15 = plt.subplot2grid(gridShape, (footerStartRow+3,2), colspan=1, rowspan=1)
ax15.text(0.5, 0.5, testDate , va="center", ha="center" , fontsize=secondaryFontSize)
ax16 = plt.subplot2grid(gridShape, (footerStartRow+1,3), colspan=2, rowspan=1)
ax16.text(0.5, 0.5, 'Input Data' , va="center", ha="center" , fontsize=secondaryFontSize)
ax17 = plt.subplot2grid(gridShape, (footerStartRow+2,3), colspan=2, rowspan=2)
ax17txt = ax17.text(0.5, 0.5, filePath , va="center", ha="center",wrap=True, fontsize=smallFontSize)
ax17txt._get_wrap_line_width = lambda : 230 # wrap to 600 screen pixels
ax18 = plt.subplot2grid(gridShape, (footerStartRow+1,5), colspan=1, rowspan=1)
ax18.text(0.5, 0.5, 'Test Order Nr:', va="center", ha="center" , fontsize=secondaryFontSize)
ax19 = plt.subplot2grid(gridShape, (footerStartRow+2,5), colspan=2, rowspan=2)
ax19.text(0.5, 0.5, serialNumber , va="center", ha="center" , fontsize=secondaryFontSize)
axesList = [ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9,ax10,ax11,ax12,ax13,ax14,ax15,ax16,ax17,ax18,ax19]
for a in axesList:
a.set_xticklabels([])
a.set_yticklabels([])
a.xaxis.set_visible(False)
a.yaxis.set_visible(False)
plt.subplots_adjust(wspace =0, hspace =0)
然后绘制数据并添加以下行的信息
graph= sns.lineplot(data=dataframe, x=columnDictionary['C'], y=columnDictionary['P'],ax=plotAxis,label="HYPE")
graph.set_title("A-B");
plotAxis.set_xlabel('A', fontsize=16)
plotAxis.set_ylabel('B', fontsize=16)
我想创建一个函数来创建空白图表,这样我就不必一次又一次地重复那么多代码。但是我不知道在那个函数中 return
是什么,这样我就可以访问所有的轴(尤其是 plotAxis
),我可以用它来添加绘图、线条、注释等
我试过返回并按如下方式使用,但没有成功
emptyFigure = [fig,ax]
return emptyFigure
blankFigure,allAxes = createBlankChart()
graph= sns.lineplot(data=dataframe, x=columnDictionary['C'], y=columnDictionary['P'],ax=allAxes .plotAxis,label="HYPE")
谁能指导一下?
目前您的 plotAxis
不属于您 return 的 ax
。
所以你可以做 return [fig, ax, plotaxis]
然后 blankFigure, allAxes, plotAxis = createBlankChart()
就像 sns.lineplot()
中的 ax=plotAxis
一样使用
我正在创建几个图表,其中只有实际 plot
发生变化。其他信息没有变化(蓝色框和下图中显示的所有文本)。
我有下面的代码来生成blank chart
截至目前
sns.set(rc={'axes.facecolor':'white','grid.color': '#b0b0b0','axes.edgecolor': 'black','figure.edgecolor':'black'})
fig, ax = plt.subplots(figsize=(15, 10))
gridShape=(25,6)
primaryFontSize = 15
secondaryFontSize = 12
smallFontSize = 10
topHeaderRowSpan=2
infoRowSpan=1
mainChartRowspan = 17
footerStartRow = mainChartRowspan+4
ax1 = plt.subplot2grid(gridShape, (0, 0), colspan=1, rowspan=topHeaderRowSpan)
ax1.text(0.5, 0.5, 'P=f(i)[bar]', va="center", ha="center" , fontsize=primaryFontSize)
ax2 = plt.subplot2grid(gridShape, (0,1), colspan=2, rowspan=topHeaderRowSpan)
ax2.text(0.5, 0.5, pcvType, va="center", ha="center" , fontsize=primaryFontSize)
ax3 = plt.subplot2grid(gridShape, (0,3), colspan=1, rowspan=infoRowSpan)
ax3.text(0.5, 0.5, 'Part Nr:', va="center", ha="center" , fontsize=secondaryFontSize)
ax4 = plt.subplot2grid(gridShape, (1,3), colspan=1, rowspan=infoRowSpan)
ax4.text(0.5, 0.5, partNumber , va="center", ha="center" , fontsize=secondaryFontSize)
ax5 = plt.subplot2grid(gridShape, (0,4), colspan=1, rowspan=infoRowSpan)
ax5.text(0.5, 0.5, 'Serial Nr:', va="center", ha="center" , fontsize=secondaryFontSize)
ax6 = plt.subplot2grid(gridShape, (1,4), colspan=1, rowspan=infoRowSpan)
ax6.text(0.5, 0.5, serailNumber , va="center", ha="center" , fontsize=secondaryFontSize)
ax7 = plt.subplot2grid(gridShape, (0,5), colspan=1, rowspan=infoRowSpan)
ax7.text(0.5, 0.5, 'Manufacturing Date:', va="center", ha="center" , fontsize=secondaryFontSize)
ax8 = plt.subplot2grid(gridShape, (1,5), colspan=1, rowspan=infoRowSpan)
ax8.text(0.5, 0.5, mfgDate , va="center", ha="center" , fontsize=secondaryFontSize)
plotAxis = plt.subplot2grid(gridShape, (3, 0), colspan=6, rowspan=mainChartRowspan)
ax9 = plt.subplot2grid(gridShape, (footerStartRow+1,0), colspan=1, rowspan=3)
ax9.imshow(im, aspect='auto', extent=(0.4, 0.6, .5, .7))
ax10 = plt.subplot2grid(gridShape, (footerStartRow+1,1), colspan=1, rowspan=1)
ax10.text(0.5, 0.5, 'Test Bench:', va="center", ha="center" , fontsize=secondaryFontSize)
ax11 = plt.subplot2grid(gridShape, (footerStartRow+2,1), colspan=1, rowspan=1)
ax11.text(0.5, 0.5, 'Operator:', va="center", ha="center" , fontsize=secondaryFontSize)
ax12 = plt.subplot2grid(gridShape, (footerStartRow+3,1), colspan=1, rowspan=1)
ax12.text(0.5, 0.5, 'Date:', va="center", ha="center" , fontsize=secondaryFontSize)
ax13 = plt.subplot2grid(gridShape, (footerStartRow+1,2), colspan=1, rowspan=1)
ax13.text(0.5, 0.5, testBench , va="center", ha="center" , fontsize=secondaryFontSize)
ax14 = plt.subplot2grid(gridShape, (footerStartRow+2,2), colspan=1, rowspan=1)
ax14.text(0.5, 0.5, operator , va="center", ha="center" , fontsize=secondaryFontSize)
ax15 = plt.subplot2grid(gridShape, (footerStartRow+3,2), colspan=1, rowspan=1)
ax15.text(0.5, 0.5, testDate , va="center", ha="center" , fontsize=secondaryFontSize)
ax16 = plt.subplot2grid(gridShape, (footerStartRow+1,3), colspan=2, rowspan=1)
ax16.text(0.5, 0.5, 'Input Data' , va="center", ha="center" , fontsize=secondaryFontSize)
ax17 = plt.subplot2grid(gridShape, (footerStartRow+2,3), colspan=2, rowspan=2)
ax17txt = ax17.text(0.5, 0.5, filePath , va="center", ha="center",wrap=True, fontsize=smallFontSize)
ax17txt._get_wrap_line_width = lambda : 230 # wrap to 600 screen pixels
ax18 = plt.subplot2grid(gridShape, (footerStartRow+1,5), colspan=1, rowspan=1)
ax18.text(0.5, 0.5, 'Test Order Nr:', va="center", ha="center" , fontsize=secondaryFontSize)
ax19 = plt.subplot2grid(gridShape, (footerStartRow+2,5), colspan=2, rowspan=2)
ax19.text(0.5, 0.5, serialNumber , va="center", ha="center" , fontsize=secondaryFontSize)
axesList = [ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9,ax10,ax11,ax12,ax13,ax14,ax15,ax16,ax17,ax18,ax19]
for a in axesList:
a.set_xticklabels([])
a.set_yticklabels([])
a.xaxis.set_visible(False)
a.yaxis.set_visible(False)
plt.subplots_adjust(wspace =0, hspace =0)
然后绘制数据并添加以下行的信息
graph= sns.lineplot(data=dataframe, x=columnDictionary['C'], y=columnDictionary['P'],ax=plotAxis,label="HYPE")
graph.set_title("A-B");
plotAxis.set_xlabel('A', fontsize=16)
plotAxis.set_ylabel('B', fontsize=16)
我想创建一个函数来创建空白图表,这样我就不必一次又一次地重复那么多代码。但是我不知道在那个函数中 return
是什么,这样我就可以访问所有的轴(尤其是 plotAxis
),我可以用它来添加绘图、线条、注释等
我试过返回并按如下方式使用,但没有成功
emptyFigure = [fig,ax]
return emptyFigure
blankFigure,allAxes = createBlankChart()
graph= sns.lineplot(data=dataframe, x=columnDictionary['C'], y=columnDictionary['P'],ax=allAxes .plotAxis,label="HYPE")
谁能指导一下?
目前您的 plotAxis
不属于您 return 的 ax
。
所以你可以做 return [fig, ax, plotaxis]
然后 blankFigure, allAxes, plotAxis = createBlankChart()
就像 sns.lineplot()
中的 ax=plotAxis
一样使用