Created
July 20, 2019 07:49
-
-
Save fellesy/4d21d9489113178c504133838555efb6 to your computer and use it in GitHub Desktop.
#python-pptx #ppt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pptx import Presentation | |
from pptx.chart.data import CategoryChartData,XyChartData | |
from pptx.enum.chart import XL_CHART_TYPE,XL_LEGEND_POSITION,XL_CATEGORY_TYPE | |
from pptx.util import Inches,Pt | |
import numpy as numpy | |
import pandas as pd | |
from WindPy import * | |
def draw_linechart(data,left=0.3,top=4,width=5,height=3): | |
prsindex = data.index | |
seriesname = data.columns | |
chart_data = CategoryChartData() | |
chart_data.categories = prsindex | |
for i in range(len(seriesname)): | |
chart_data.add_series(seriesname[i],data[[seriesname[i]]].values[:,0]) | |
#print(data[[seriesname[i]]].head()) | |
chart = slide.shapes.add_chart( | |
XL_CHART_TYPE.LINE, Inches(left), Inches(top), Inches(width), Inches(height), chart_data | |
).chart | |
for i in range(len(chart.series)): | |
chart.series[i].smooth = True | |
chart.has_legend = True | |
chart.legend.include_in_layout = True | |
# 设定图标元素 | |
#chart.series[0].smooth = True | |
chart.legend.position = XL_LEGEND_POSITION.TOP | |
chart.legend.font.size=Pt(9) | |
chart.category_axis.tick_labels.number_format = 'yyyy-mm' | |
chart.category_axis.tick_labels.font.size = Pt(9) | |
chart.value_axis.has_major_gridlines = False | |
chart.value_axis.has_minor_gridlines = False | |
chart.value_axis.tick_labels.font.size = Pt(9) | |
#chart.chart_title.text_frame.text = title | |
def draw_barchart(data,left=0.3,top=4,width=5,height=3): | |
prsindex = data.index | |
seriesname = data.columns | |
chart_data = CategoryChartData() | |
chart_data.categories = prsindex | |
for i in range(len(seriesname)): | |
chart_data.add_series(seriesname[i],data[[seriesname[i]]].values[:,0]) | |
#print(data[[seriesname[i]]].head()) | |
chart = slide.shapes.add_chart( | |
XL_CHART_TYPE.COLUMN_CLUSTERED, Inches(left), Inches(top), Inches(width), Inches(height), chart_data | |
).chart | |
# 设定图标元素 | |
chart.has_legend = True | |
chart.legend.include_in_layout = True | |
#chart.series[0].smooth = True | |
chart.category_axis.category_type == XL_CATEGORY_TYPE.AUTOMATIC_SCALE | |
chart.legend.position = XL_LEGEND_POSITION.TOP | |
chart.legend.font.size=Pt(9) | |
#chart.category_axis.tick_labels.number_format = 'yyyy-mm' | |
chart.category_axis.tick_labels.font.size = Pt(9) | |
chart.value_axis.has_major_gridlines = False | |
chart.value_axis.has_minor_gridlines = False | |
chart.value_axis.tick_labels.font.size = Pt(9) | |
# 设置横轴类型:文字坐标。其他选项:AUTOMATIC_SCALE,TIME_SCALE | |
#assert chart.category_axis.category_type == XL_CATEGORY_TYPE.CATEGORY_SCALE | |
def draw_scatterchart(data_x,data_y,left=0.3,top=4,width=4.3,height=3): | |
chart_data = XyChartData() | |
if len(data_x)!=len(data_y): | |
print("#####the size donesn't match#####") | |
return False | |
series_1_title = str(data_x.name)+"VS"+str(data_y.name) | |
series_1 = chart_data.add_series(series_1_title) | |
for i in range(len(data_x)): | |
series_1.add_data_point(data_x[i], data_y[i]) | |
chart = slide.shapes.add_chart( | |
XL_CHART_TYPE.XY_SCATTER,Inches(left), Inches(top), Inches(width), Inches(height), chart_data | |
).chart | |
# 设定图标元素 | |
chart.has_legend = True | |
chart.legend.include_in_layout = True | |
chart.legend.position = XL_LEGEND_POSITION.TOP | |
chart.legend.font.size=Pt(9) | |
chart.category_axis.has_major_gridlines = False | |
chart.category_axis.has_minor_gridlines = False | |
chart.category_axis.tick_labels.font.size = Pt(9) | |
chart.value_axis.has_major_gridlines = False | |
chart.value_axis.has_minor_gridlines = False | |
chart.value_axis.tick_labels.font.size = Pt(9) | |
#从wind取数据 | |
def get_wind_edb(id,start='2018-01-01',end='2019-07-01'): | |
x = w.edb(id,start,end) | |
xd = numpy.array(x.Data).T | |
data = pd.DataFrame(xd,index=x.Times,columns=id.split(',')).dropna() | |
print(data.head()) | |
return data | |
#把index的类型从datetimeindex改为string | |
def index_to_str(data,type=1): | |
if type==1: | |
lenth=7 #月度 | |
else: | |
lenth=4 #年度 | |
index = data.index.to_pydatetime() | |
date_index = index | |
str_index = numpy.vectorize(lambda s:s.strftime("%Y-%m-%d"))(date_index) | |
pd_index = pd.Series(str_index) | |
data.index=pd_index.str[:lenth] | |
return data | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment