python openpyxl + VBA控制Excel批量绘制两两散点图

  尽管R语言是进行生态环境数据探索的首选,但Excel无疑在我们搜集、整理以及初步数据探索中依旧有着十分重要的作用。绘制环境因子间的两两散点图是探索因子关系的常见步骤,当环境因子较多时scatterplotMatrix()显得力不从心,当因子间的关系不是简单的线性关系时,corrplot()也不能完全展示数据间的关系。这里我们用python的openpyxl包控制excel绘制11个变量间的两两散点图。

  效果如下:
python openpyxl + VBA控制Excel批量绘制两两散点图_第1张图片

主要步骤:

  1. python计算两两相关性P值
  2. python的openpyxl包控制excel绘制11个变量间的两两散点图,并将P值写入excel表格;
  3. VBA将P值添入散点图,并修改为默认格式;

步骤代码

  1. 步骤1的python代码
xie = pd.read_excel("./Desktop/xie.xlsx")
p = np.zeros(55)
l=0
for i in range(0,10):
    for j in range(i+1,11):
        x=np.array(xie[xie.columns[i]])
        y=np.array(xie[xie.columns[j]])
        fit = stats.linregress(x,y)
        p[l] = fit.pvalue
        print([l,p[l]])
        l=l+1
  1. 步骤2的python代码:
from openpyxl import load_workbook
from openpyxl.chart import (
    ScatterChart,
    Reference,
    Series,
    legend,
)
from openpyxl.chart.text import (RichText,Text)
from openpyxl.drawing.text import (
    Paragraph, 
    ParagraphProperties, 
    CharacterProperties, 
    Font,
    TextField
)
from openpyxl.styles import Border
from openpyxl.chart.trendline import (Trendline,TrendlineLabel)
from openpyxl.chart.title import title_maker

wb = load_workbook('./Desktop/xie.xlsx') 
ws = wb.active
k=0
for i in range(1,11):
    for j in range(i+1,12):       
        chart = ScatterChart()
        chart.height = 6.42
        chart.width = 9.57
        chart.legend = None
        xv = Reference(ws, min_col=i, min_row=2, max_row=80)
        #print(i)
        yv = Reference(ws, min_col=j, min_row=2, max_row=80)
        series = Series(values = yv,xvalues = xv,title_from_data=False)
        chart.series.append(series)
        chart.title = ws.cell(1,i).value+" vs "+ws.cell(1,j).value
        title_font = Font(typeface='Calibri')
        cp = CharacterProperties(latin=title_font, sz=900)
        cp2 = CharacterProperties(latin=title_font, sz=1400,b=False)
        pp = ParagraphProperties(defRPr=cp)
        pp2 = ParagraphProperties(defRPr=cp2)
        rtp = RichText(p=[Paragraph(pPr=pp, endParaRPr=cp)])
        chart.x_axis.txPr = rtp
        chart.y_axis.txPr = rtp
        chart.title.text.rich.paragraphs[0].pPr = pp2
        s1 = chart.series[0]
        s1.marker.symbol = "circle"
        #s1.marker.graphicalProperties.solidFill = "FF0000" # Marker filling
        #s1.marker.graphicalProperties.line.solidFill = "FF0000" # Marker outline
        s1.graphicalProperties.line.noFill = True  # hide lines
        s1.trendline=Trendline(trendlineType = 'linear',dispEq=True,dispRSqr=True)
        chartPosition = "N"+str(7*k+1)
        mycell = ws.cell(row=k+1, column=25)
        mycell.value = p[k]
        k=k+1
        ws.add_chart(chart, chartPosition)
wb.save("./Desktop/xie2.xlsx")
  1. 步骤3的VBA代码:
Sub addPvalue()
    For i = 1 To 55
    ActiveSheet.ChartObjects(Index:=i).Activate
    a = ActiveChart.FullSeriesCollection(1).Trendlines(1).DataLabel.Text
    ActiveChart.FullSeriesCollection(1).Trendlines(1).DataLabel.Text = (a & ";" & Cells(i + 1, 22))
    ActiveChart.ChartStyle = 240
    Next i
End Sub

测试数据和可参考如下链接
python openpyxl + excel VBA批量绘制两两散点图

你可能感兴趣的:(编程让____更美好,python,excel,开发语言)