File size: 7,423 Bytes
86a4b1d 0092ed4 c6b6b22 aee291e cafd83e 0d8cf84 cb5fa73 38b3a58 98072ca 7ea9db9 1131b7f 82ab6a1 bf3cf13 8ceb05e 1131b7f a88c76d de25161 aee291e b532513 a7ab215 1521735 8df080b fbee28c dcaa8fb eb73ff6 32cefb4 5656e90 0dc478a 20986ae 6e5b8e0 53d2b62 71f7697 cafd83e c96d6fa 38b3a58 7ec74db 7ea9db9 14433b5 1131b7f 05e3282 551bc6e 1131b7f cb5fa73 b0b8734 c7a82a8 e07283d a88c76d fd8479d ca06b5f 72d493e 4cca739 4ddeff1 0d8cf84 7377fc4 723598f 0092ed4 322b4e5 0092ed4 9d6f321 ff68a5b 6b8906c 86a4b1d 1ff7e61 a0f2053 da678cf a0f2053 da678cf 6e5b8e0 da678cf 71f7697 38b3a58 da678cf 7ec74db c6b6b22 da678cf 1131b7f 331b6e8 1131b7f cb5fa73 e4d870d cb5fa73 e07283d a88c76d 84f9f8a fd8479d c1d7aef fd8479d 0d8cf84 723598f 0d8cf84 0092ed4 a0927ea 0092ed4 a0f2053 1ff7e61 e67a19f 4877abc 35ee90f 1ff7e61 86a4b1d 1ff7e61 3577ae5 0c606cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import gradio as gr
import pygal
import requests
import pandas as pd
import pandas_bokeh
import matplotlib.pyplot as plt,mpld3
import seaborn as sns
import bokeh.plotting as bk
from bokeh.embed import json_item
from highcharts import Highchart
import pyecharts
from pyecharts.charts import Bar
import cufflinks as cf
import plotly.offline
cf.go_offline()
cf.set_config_file(offline=True, world_readable=True)
#from pyg2plot import Plot
#plt.rcParams['font.sans-serif']=['SimHei']
#plt.rcParams['axes.unicode_minus']=False
def pd_bar():
pd.options.plotting.backend='matplotlib'
plt.close('all')
df = pd.DataFrame({'课程':['语文', '数学', '英语'], '成绩':[66, 98, 78]})
ax = df.plot(x='课程',y='成绩',kind='bar',title='chinese word error!', color=["red","green","purple"])
ax.table(cellText=df.values,colLabels=df.columns,loc='bottom',colLoc='center')
return ax.get_figure() #plt
def plt_line():
plt.close('all')
df = pd.DataFrame({'课程':['yw', 'sx', 'yy'], '成绩':[66, 98, 100]})
plt.plot(df['成绩'],color='green',linestyle='dashed',marker='o',markerfacecolor='blue',markersize=20)
return plt.gcf()
def pd_plotly():
pd.options.plotting.backend='plotly'
df = pd.DataFrame({'课程':['语文', '数学', '英语'], '成绩':[66, 98, 78]})
ax = df.plot.bar(x='课程',y='成绩')
return ax #plt
def pd_bokeh():
pd.set_option('plotting.backend','pandas_bokeh')
pandas_bokeh.output_file("test.html")
df=pd.DataFrame({'班级':['天','地','玄','黄'],'姓名':['诸葛清明','公孙轩辕','李耳','石破天'],'成绩':[66,78,99,85]})
tt=df.plot_bokeh.bar(x='姓名',y='成绩',ylabel="群员吹水战绩",title="吹牛逼你们是认真的",alpha=0.6,figsize=(600,300),show_figure=False,legend="bottom_right")
pandas_bokeh.save(tt)
p = bk.figure(plot_width=400,plot_height=400)
p.patch([1,2,3,4,5],[6,7,8,7,3],alpha=0.5,line_width=2)
item_text = json_item(p, "plotDiv")
return 'test.html',item_text
def pyecharts1():
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.render('test.html')
with open('test.html',"r",encoding="utf-8") as aa:
bb=aa.read()
return 'test.html',bb
def cff():
df=pd.DataFrame([[0.2,0.4,0.6],[0.4,0.3,0.4],[0.4,0.5,0.2],[0.5,0.4,0.4]],columns=['a','b','c'])
cfft=df.iplot(kind='bubble',x='a',y='b',size='c')
return cfft
def seab():
df = pd.DataFrame({'a': range(10), 'b': [1,3,1,4,5,2,0,5,8,5]})
g=sns.relplot(x="a", y="b", kind="line", data=df)
return plt
def pyg2():
return ''
#def pyg2plot_chart():
# bar = Plot("Column")
# bar.set_options({"height": 600,"width":800,"data": [{ "年": "2001", "销量": 3 },{ "年": "2002", "销量": 4 },{ "年": "2003", "销量": 5.5 },{"年": "2004", "销量": 7 },{ "年": "2005", "销量": 8.9 },{ "年": "2006", "销量": 10}],"xField": "年","yField": "销量","label": {}})
# return bar.render_html()
def pyhighchats():
H = Highchart()
data = list(range(1,20))
H.add_data_set(data,'line')
H.save_file('test.html')
return 'test.html'
def d3_js():
fig=plt.figure()
obj,=plt.plot([3,1,4,1,5])
rr=mpld3.fig_to_html(fig)
return rr
def pygalc():
aa=pygal.Bar()(1,3,3,7)(1,6,6,4)
bb=aa.render(is_unicode=True)
cc=aa.render_data_uri()
aa.render_to_file("test.svg")
return 'test.svg',bb,cc,bb
demo=gr.Blocks()
with demo:
gr.Markdown("# <center>图表渲染测试")
with gr.Tabs():
with gr.TabItem("pd_bar"):
pd_button=gr.Button("pd_bar>>")
pd_output=gr.Plot(label="pd_chart1")
pd_button.click(pd_bar, inputs=[], outputs=pd_output,api_name='pd_bar')
with gr.TabItem("plt_line"):
plt_button=gr.Button("plt_line>>")
plt_output=gr.Plot(label="plt_chart")
plt_button.click(plt_line, inputs=[], outputs=plt_output,api_name='plt_line')
with gr.TabItem("pd_plotly"):
plotly_button=gr.Button("pd_plotly>>")
plotly_output=gr.Plot(label="plotly_chart")
plotly_button.click(pd_plotly, inputs=[], outputs=plotly_output,api_name='pd_plotly')
with gr.TabItem("pd_bokeh"):
bokeh_button=gr.Button("pd_bokeh>>")
bokeh_output=[gr.File(label="bokeh_chart"),gr.Plot(label="bokeh_chart2")]
bokeh_button.click(pd_bokeh, inputs=[], outputs=bokeh_output,api_name='pd_bokeh')
with gr.TabItem("pyecharts"):
ehc_button=gr.Button("pyecharts>>")
ehc_output=[gr.File(label="pyecharts_chart"),gr.Textbox(label="pyecharts_chart2",lines=18)]
ehc_button.click(pyecharts1, inputs=[], outputs=ehc_output,api_name='pyecharts')
with gr.TabItem("cufflinks"):
cff_button=gr.Button("cufflinks>>")
cff_output=gr.Plot(label="cufflinks_chart")
cff_button.click(cff, inputs=[], outputs=cff_output,api_name='cuffl')
with gr.TabItem("seaborn"):
sb_button=gr.Button("seaborn>>")
sb_output=gr.Plot(label="seaborn_chart")
sb_button.click(seab, inputs=[], outputs=sb_output,api_name='seaborn')
with gr.TabItem("pyg2plot"):
g2plot_button=gr.Button("pyg2plot>>")
g2plot_output=gr.Plot(label="pyg2plot_chart")
g2plot_button.click(pyg2, inputs=[], outputs=g2plot_output,api_name='pyg2plot')
# with gr.TabItem("pyg2plot"):
# pyg2_button=gr.Button("pyg2plot>>")
# pyg2_output=gr.Plot(label="pyg2plot_chart")
# pyg2_button.click(pyg2plot_chart, inputs=[], outputs=pyg2_output)
with gr.TabItem("pyhighcharts"):
pyhc_button=gr.Button("pyhc>>")
pyhc_output=gr.File(label="pyhc_chart")
pyhc_button.click(pyhighchats, inputs=[], outputs=pyhc_output,api_name='pyhcharts')
with gr.TabItem("d3JS"):
d3_button=gr.Button("D3JS>>")
d3_output=gr.Textbox(label="D3_chart1",lines=18)
d3_button.click(d3_js, inputs=[], outputs=d3_output,api_name='d3_js')
with gr.TabItem("pygal"):
pyga_button=gr.Button("pygal>>")
pyga_output=[gr.File(label="pygal_chart"),gr.Textbox(label="pygal_chart1",lines=10),gr.Textbox(label="pygal_chart2",lines=3),gr.HTML(label="html渲染")]
pyga_button.click(pygalc, inputs=[], outputs=pyga_output,api_name='pygal')
with gr.TabItem("echarts"):
with gr.Accordion("可伸缩扩展布局容器",open=False):
gr.Markdown("测试伸缩布局")
# gr.Carousel()新组件待测试,,,
htmls='''
<html>
<head><style type="text/css">body {margin: 0;}#main2 {height: 100%;}</style></head>
<body><div id="main2"></div>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.0.2/dist/echarts.min.js"></script>
<script>var myChart = echarts.init(document.getElementById("main2"));
option = {tooltip:{formatter:"{a} <br/>{b} : {c}%"},series:[{name:'业务指标',type:'gauge',detail:{formatter:'{value}%'},axisLine:{lineStyle:{width:20}},splitLine:{length:20},data:[{value:50,name:'完成率'}]}]};
myChart.setOption(option);
</script></body>
</html>
'''
gr.HTML(htmls)
gr.HTML("https://hf.space/static/changxin/highcharts/index.html")
demo.launch() |