Home > OS >  How to return a template empty chart with a function with matplotlib & seaborn
How to return a template empty chart with a function with matplotlib & seaborn

Time:06-04

I am creating several charts where only the actual plot changes. The other information does not changes (the blue boxes & all the text shown in the pic below).

I have the below code to generate the blank chart as of now

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)

Then the data is plotted & info added with the lines below

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)

I want to create a function to create the blank chart so that I do not have to repeat that much code again and again. But I do no know what to return in that function so that I have access to all the axes (especially plotAxis) with which I can add plots, lines, annotation etc.

I tried returning & using as below but it did not work

    emptyFigure = [fig,ax]
    return emptyFigure

blankFigure,allAxes = createBlankChart()

graph= sns.lineplot(data=dataframe, x=columnDictionary['C'], y=columnDictionary['P'],ax=allAxes .plotAxis,label="HYPE")

Can anyone guide?

enter image description here

CodePudding user response:

Right now your plotAxis is not part of ax which you return.

So you can do return [fig, ax, plotaxis] and then blankFigure, allAxes, plotAxis = createBlankChart() to use is as ax=plotAxis in sns.lineplot().

  • Related