pyecharts x轴字体大小调整_pyecharts 柱状图基础篇#学习笔记#
2020年初,很久沒(méi)有用過(guò)pyecharts的我由于工作原因,安裝了新版pyecharts之后,以前的pyecharts代碼報(bào)錯(cuò)了。搜索之后才發(fā)現(xiàn),我安裝的是不兼容舊版本的新版。
空閑的時(shí)間,把新版echart當(dāng)作新的模塊梳理了一下,整理為便于理解的形式。
參考網(wǎng)址:A Python Echarts Plotting Library
這里描述了設(shè)置柱狀圖6大元素、柱狀圖轉(zhuǎn)置XY軸、堆疊柱狀圖的方法。
了解清楚了柱狀圖,其他圖形應(yīng)該就無(wú)師自通啦,框架結(jié)構(gòu)都是一樣的,參數(shù)不同而已,看一下文檔就行啦~
一、設(shè)置柱狀圖6大元素的方式
6大元素為:標(biāo)題、圖例、X軸、Y軸、Y軸區(qū)域分隔線、數(shù)據(jù)標(biāo)簽
首先導(dǎo)入需要的模塊
from由于新版參數(shù)設(shè)置均已經(jīng)選項(xiàng)配置化,需要配置的選項(xiàng)都放在了配置項(xiàng)(options)里面,因此需要導(dǎo)入options及使用到的圖表。
然后,以下是基礎(chǔ)代碼框架
bar1=(Bar() ##定義為柱狀圖.add_xaxis([1,2,3]) ##X軸的值.add_yaxis('y值',[4,5,6],其他參數(shù)) ##y的值和y的一些數(shù)據(jù)配置項(xiàng).set_global_opts(這里面配置一些 全局參數(shù)).set_series_opts(這里面配一些 系列參數(shù)) )bar1.render('bar1.html') ###輸出html文件新版pyecharts可以采用以上的鏈?zhǔn)秸{(diào)用方式,也可以采用bar1.add_xaxis()分開(kāi)調(diào)用的方式。為了代碼清晰并且看上去高大上一點(diǎn)(哈哈~~),建議采用鏈?zhǔn)秸{(diào)用。
這6大元素參數(shù)在代碼中,分別進(jìn)行配置的地方如下:
標(biāo)題、圖例、X軸、Y軸、Y軸區(qū)域分隔線為全局參數(shù) ,在set_global_opts后面設(shè)置。
數(shù)據(jù)標(biāo)簽為系列參數(shù),在set_series_opts后面設(shè)置。
具體的簡(jiǎn)單示例為:
1.設(shè)置標(biāo)題
格式為:.set_global_opts(title_opts=opts.TitleOpts(具體參數(shù)))
具體參數(shù)細(xì)則參考TitleOpts的參數(shù)詳情,以下代碼沒(méi)有配置完所有參數(shù),網(wǎng)址直達(dá):A Python Echarts Plotting Library
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值).set_global_opts(title_opts=opts.TitleOpts(title='我是大標(biāo)題',subtitle='我是副標(biāo)題',pos_left='20%' #標(biāo)題的位置 距離左邊20%距離。,item_gap=10#主副標(biāo)題之間的距離,title_textstyle_opts=opts.TextStyleOpts(color='red'#,font_size=12,font_weight='bold' ) #大標(biāo)題文字的格式配置,,subtitle_textstyle_opts=opts.TextStyleOpts(color='blue',font_style='normal'## 可選:'normal','italic','oblique',font_weight='normal' #粗細(xì) 'normal','bold','bolder','lighter' ,font_family= 'monospace'# 還可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...,font_size=12,background_color='grey'#文字背景顏色,border_color='black' #文字塊邊框顏色)###小標(biāo)題文字的格式配置)))bar1.render('bar1.html') ###輸出html文件輸出的格式為:
2.設(shè)置圖例
格式為:.set_global_opts(legend_opts=opts.LegendOpts(具體參數(shù)))
具體參數(shù)細(xì)則參考LegendOpts的參數(shù)詳情,以下代碼沒(méi)有配置完所有參數(shù),網(wǎng)址直達(dá):A Python Echarts Plotting Library
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值).set_global_opts(legend_opts=opts.LegendOpts(type_=None # 'plain':普通圖例。缺省就是普通圖例。 # 'scroll':可滾動(dòng)翻頁(yè)的圖例。當(dāng)圖例數(shù)量較多時(shí)可以使用。 ,pos_left='right' #圖例橫向的位置,right表示在右側(cè),也可以為百分比,pos_top='middle'#圖例縱向的位置,middle表示中間,也可以為百分比,orient='vertical'#horizontal #圖例方式的方式)))bar1.render('bar1.html') ###輸出html文件輸出的格式為:
3.X軸設(shè)置
格式為:.set_global_opts(xaxis_opts=opts.AxisOpts(具體參數(shù)))
具體參數(shù)細(xì)則參考AxisOpts的參數(shù)詳情,以下代碼沒(méi)有配置完所有參數(shù),網(wǎng)址直達(dá):
A Python Echarts Plotting Library
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值).set_global_opts(xaxis_opts=opts.AxisOpts(name='X軸名稱(chēng)',name_location='middle' #坐標(biāo)軸名字所在的位置,name_gap=25#坐標(biāo)軸名字與坐標(biāo)軸之間的距離,name_rotate=15 #坐標(biāo)軸名字旋轉(zhuǎn)角度,offset=5 #坐標(biāo)軸X的值距離X軸的距離,name_textstyle_opts=opts.TextStyleOpts(color='black',font_style='italic'## 可選:'normal','italic','oblique',font_weight='bolder' #粗細(xì) 'normal','bold','bolder','lighter' ,font_family= 'monospace'# 還可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...,font_size=14,background_color='grey'#文字背景顏色,border_color='black' #文字塊邊框顏色)##X軸名稱(chēng)的格式配置,axistick_opts=opts.AxisTickOpts(is_inside=True #刻度線是否在內(nèi)側(cè)) #坐標(biāo)軸刻度配置項(xiàng),axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(width=3 ##設(shè)置寬度,opacity=0 #設(shè)置透明度,type_='dashed' # 'solid', 'dashed', 'dotted',color='black') )#坐標(biāo)軸線的配置項(xiàng),axislabel_opts=opts.LabelOpts(font_size=13#字的大小,rotate=15 #字旋轉(zhuǎn)的角度)##坐標(biāo)軸標(biāo)簽的格式配置)))bar1.render('bar1.html') ###輸出html文件輸出格式為:
4.Y軸、Y軸分割線的設(shè)置
y軸配置和X軸的類(lèi)似。
格式為:.set_global_opts(yaxis_opts=opts.AxisOpts(具體參數(shù)))
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值).set_global_opts(yaxis_opts=opts.AxisOpts(name='Y軸名稱(chēng)',name_location='middle' #坐標(biāo)軸名字所在的位置,name_gap=25#坐標(biāo)軸名字與坐標(biāo)軸之間的距離,name_rotate=60 #坐標(biāo)軸名字旋轉(zhuǎn)角度,name_textstyle_opts=opts.TextStyleOpts(color='black',font_style='italic'## 可選:'normal','italic','oblique',font_weight='bolder' #粗細(xì) 'normal','bold','bolder','lighter' ,font_family= 'monospace'# 還可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...,font_size=14,background_color='grey'#文字背景顏色,border_color='black' #文字塊邊框顏色),offset=5 #y軸相對(duì)于默認(rèn)位置偏移的距離,相同位置有多個(gè)y軸時(shí)可以用到,split_number=5 #坐標(biāo)軸分割的段數(shù),默認(rèn)為5,只是預(yù)估,不是實(shí)際展示的段數(shù),min_=0 #最小時(shí)候,max_=20000 #最大值,splitline_opts=opts.SplitLineOpts(is_show=True # 是否展示Y軸分割線,linestyle_opts=opts.LineStyleOpts(width=1 ##設(shè)置寬度,opacity=0.5 #設(shè)置透明度,type_='dotted' # 'solid', 'dashed', 'dotted',color='grey' ) )# y軸分割線顯示的相關(guān)設(shè)置,X軸和y軸都有, axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(width=3 ##設(shè)置寬度# ,opacity=0 #設(shè)置透明度,type_='dashed' # 'solid', 'dashed', 'dotted',color='red' ) )###坐標(biāo)軸線的配置項(xiàng)# ,splitarea_opts ##分割區(qū)域配置項(xiàng))) )bar1.render('bar1.html') ###輸出html文件輸出格式為:
5.數(shù)據(jù)標(biāo)簽
格式為:.set_series_opts(label_opts=opts.LabelOpts(具體參數(shù)))
具體參數(shù)細(xì)則參考LabelOpts的參數(shù)詳情,以下代碼沒(méi)有配置完所有參數(shù),網(wǎng)址直達(dá):A Python Echarts Plotting Library
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值).set_series_opts(label_opts=opts.LabelOpts(position='insideTop' #設(shè)置數(shù)據(jù)標(biāo)簽所在的位置 'top','left','right','bottom','inside','insideLeft','insideRight'# 'insideTop','insideBottom', 'insideTopLeft','insideBottomLeft'# 'insideTopRight','insideBottomRight',color='white'#數(shù)據(jù)標(biāo)簽的顏色,font_size=12 # ,formatter #數(shù)據(jù)標(biāo)簽顯示格式)##設(shè)置數(shù)據(jù)標(biāo)簽的格式s) )bar1.render('bar1.html') ###輸出html文件輸出格式為:
opts.LabelOpts()在代碼中都有重復(fù)出現(xiàn),這個(gè)配置項(xiàng)可以使用來(lái)設(shè)置標(biāo)簽格式的,包括X軸的標(biāo)簽、Y軸的標(biāo)簽、數(shù)據(jù)標(biāo)簽。配置邏輯有差異,如下:
x和y軸配置標(biāo)簽的邏輯為:設(shè)置全局配置項(xiàng)→設(shè)置x軸/y軸的配置項(xiàng)→設(shè)置軸標(biāo)簽的配置項(xiàng)→使用opts.LabelOpts()進(jìn)行配置。
數(shù)據(jù)標(biāo)簽配置標(biāo)簽的邏輯為:設(shè)置系列配置項(xiàng)→設(shè)置標(biāo)簽配置項(xiàng)→使用opts.LabelOpts()進(jìn)行配置
總結(jié):將以上的代碼匯總在一起,得到以下的圖形:
代碼奉上:
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值).set_global_opts(title_opts=opts.TitleOpts(title='我是大標(biāo)題',subtitle='我是副標(biāo)題',pos_left='45%' #標(biāo)題的位置 距離左邊20%距離。,item_gap=15#主副標(biāo)題之間的距離,title_textstyle_opts=opts.TextStyleOpts(color='red',font_size=17,font_weight='bold' ) #大標(biāo)題的格式配置,,subtitle_textstyle_opts=opts.TextStyleOpts(color='blue',font_style='normal'## 可選:'normal','italic','oblique',font_weight='normal' #粗細(xì) 'normal','bold','bolder','lighter' ,font_family= 'monospace'# 還可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...,font_size=12,background_color='grey'#文字背景顏色,border_color='black' #文字塊邊框顏色)),legend_opts=opts.LegendOpts(type_=None # 'plain':普通圖例。缺省就是普通圖例。 # 'scroll':可滾動(dòng)翻頁(yè)的圖例。當(dāng)圖例數(shù)量較多時(shí)可以使用。 ,pos_left='right' #圖例橫向的位置,pos_top='middle'#圖例縱向的位置,orient='vertical'#horizontal #圖例方式的方式), xaxis_opts=opts.AxisOpts(name='X軸名稱(chēng)',name_location='middle' #坐標(biāo)軸名字所在的位置,name_gap=25#坐標(biāo)軸名字與坐標(biāo)軸之間的距離,name_rotate=15 #坐標(biāo)軸名字旋轉(zhuǎn)角度,offset=5 #坐標(biāo)軸X的值距離X軸的距離,name_textstyle_opts=opts.TextStyleOpts(color='black',font_style='italic'## 可選:'normal','italic','oblique',font_weight='bolder' #粗細(xì) 'normal','bold','bolder','lighter' ,font_family= 'monospace'# 還可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...,font_size=14,background_color='grey'#文字背景顏色,border_color='black' #文字塊邊框顏色),axistick_opts=opts.AxisTickOpts(is_inside=True #刻度線是否在內(nèi)側(cè)) #坐標(biāo)軸刻度配置項(xiàng),axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(width=3 ##設(shè)置寬度#,opacity=0 #設(shè)置透明度,type_='dashed' # 'solid', 'dashed', 'dotted',color='black' ) )###坐標(biāo)軸線的配置項(xiàng),axislabel_opts=opts.LabelOpts(font_size=13,rotate=15)##坐標(biāo)軸標(biāo)簽的配置項(xiàng)),yaxis_opts=opts.AxisOpts(name='Y軸名稱(chēng)',name_location='middle' #坐標(biāo)軸名字所在的位置,name_gap=25#坐標(biāo)軸名字與坐標(biāo)軸之間的距離,name_rotate=60 #坐標(biāo)軸名字旋轉(zhuǎn)角度,name_textstyle_opts=opts.TextStyleOpts(color='black',font_style='italic'## 可選:'normal','italic','oblique',font_weight='bolder' #粗細(xì) 'normal','bold','bolder','lighter' ,font_family= 'monospace'# 還可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...,font_size=14,background_color='grey'#文字背景顏色,border_color='black' #文字塊邊框顏色),offset=5 #y軸相對(duì)于默認(rèn)位置偏移的距離,相同位置有多個(gè)y軸時(shí)可以用到,split_number=5 #坐標(biāo)軸分割的段數(shù),默認(rèn)為5,只是預(yù)估,不是實(shí)際展示的段數(shù),min_=0 #最小時(shí)候,max_=20000 #最大值,splitline_opts=opts.SplitLineOpts(is_show=True # 是否展示Y軸分割線,linestyle_opts=opts.LineStyleOpts(width=1 ##設(shè)置寬度,opacity=0.5 #設(shè)置透明度,type_='dotted' # 'solid', 'dashed', 'dotted',color='grey' ) )# y軸分割線顯示的相關(guān)設(shè)置,X軸和y軸都有, axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(width=3 ##設(shè)置寬度# ,opacity=0 #設(shè)置透明度,type_='dashed' # 'solid', 'dashed', 'dotted',color='red' ) )###坐標(biāo)軸線的配置項(xiàng)# ,splitarea_opts ##分割區(qū)域配置項(xiàng))).set_series_opts(label_opts=opts.LabelOpts(position='insideTop' #設(shè)置數(shù)據(jù)標(biāo)簽所在的位置 'top','left','right','bottom','inside','insideLeft','insideRight'# 'insideTop','insideBottom', 'insideTopLeft','insideBottomLeft'# 'insideTopRight','insideBottomRight',color='white'#數(shù)據(jù)標(biāo)簽的顏色,font_size=12 # ,formatter #數(shù)據(jù)標(biāo)簽顯示格式)##設(shè)置數(shù)據(jù)標(biāo)簽的格式s))bar1.render('bar1.html') ###輸出html文件二、轉(zhuǎn)置XY軸
調(diào)用方式很簡(jiǎn)單,為: .reversal_axis()
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值).set_series_opts(label_opts=opts.LabelOpts(position='right' #設(shè)置數(shù)據(jù)標(biāo)簽所在的位置'top','left','right','bottom','inside','insideLeft','insideRight'# 'insideTop','insideBottom', 'insideTopLeft','insideBottomLeft'# 'insideTopRight','insideBottomRight',font_size=12 # ,formatter #數(shù)據(jù)標(biāo)簽顯示格式)##設(shè)置數(shù)據(jù)標(biāo)簽的格式s).reversal_axis())bar1.render('bar1.html') ###輸出html文件輸出格式為:
三、堆疊柱狀圖
格式為:.add_yaxis(具體參數(shù)),具體參數(shù)中,stack參數(shù)設(shè)置一個(gè)字符串。多個(gè).add_yaxis()時(shí),stack的值為一樣的y值就會(huì)疊加在一起。
代碼示例如下:兩個(gè).add_yaxis()的stack值都為“stack1”,所以就堆疊在一起了。
bar1=(Bar( ).add_xaxis(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']).add_yaxis('甲的數(shù)量'##系列的名稱(chēng),[1000,2000,3000,5000,8000,3700,5800,7900,12000,10000,9000,8000] ##系列的數(shù)值,stack='stack1').add_yaxis('已的數(shù)量'##系列的名稱(chēng),[500,1000,588,5788,6000,5677,3467,4444,3333,6666,7777,8888] ##系列的數(shù)值,stack='stack1').set_global_opts(title_opts=opts.TitleOpts(title='堆疊的柱狀圖',pos_left='45%'),legend_opts=opts.LegendOpts(type_=None # 'plain':普通圖例。缺省就是普通圖例。 # 'scroll':可滾動(dòng)翻頁(yè)的圖例。當(dāng)圖例數(shù)量較多時(shí)可以使用。 ,pos_left='right' #圖例橫向的位置,pos_top='middle'#圖例縱向的位置,orient='vertical'#horizontal #圖例方式的方式)).set_series_opts(label_opts=opts.LabelOpts(is_show=False ##不顯示數(shù)據(jù)標(biāo)簽)) )bar1.render('bar1.html') ###輸出html文件輸出格式為:
總結(jié)
以上是生活随笔為你收集整理的pyecharts x轴字体大小调整_pyecharts 柱状图基础篇#学习笔记#的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php 序列化 java_PHP--序列
- 下一篇: 使用 做签名的post_基础实操|使用j