<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
折線圖和柱狀圖一樣是我們日常視覺化最多的一個圖例,當然它的優勢和適用場景相信大家肯定不陌生,要想快速的得出趨勢,抓住趨勢二字,就會很快的想到要用折線圖來表示了。折線圖是通過直線將這些點按照某種順序連線起來形成的圖,適用於資料在一個有序的因變數上的變化,它的特點是反應事物隨類別而變化的趨勢,可以清晰展現資料的增減趨勢、增減的速率、增減的規律、峰值等特徵。
優點:
缺點:每張圖上不適合展示太多折線
雙摺線圖在一張圖裡面顯示,肯定有一個相同的維度,然後有兩個不同的資料集。比如一天的溫度有最高的和最低的溫度,我們就可以用這個來作為展示了。
import pyecharts.options as opts from pyecharts.charts import Line week_name_list = ["週一", "週二", "週三", "週四", "週五", "週六", "週日"] high_temperature = [11, 11, 15, 13, 12, 13, 10] low_temperature = [1, -2, 2, 5, 3, 2, 0] ( Line(init_opts=opts.InitOpts(width="1000px", height="600px")) .add_xaxis(xaxis_data=week_name_list) .add_yaxis( series_name="最高氣溫", y_axis=high_temperature, # 顯示最大值和最小值 # markpoint_opts=opts.MarkPointOpts( # data=[ # opts.MarkPointItem(type_="max", name="最大值"), # opts.MarkPointItem(type_="min", name="最小值"), # ] # ), # 顯示平均值 # markline_opts=opts.MarkLineOpts( # data=[opts.MarkLineItem(type_="average", name="平均值")] # ), ) .add_yaxis( series_name="最低氣溫", y_axis=low_temperature, # 設定刻度標籤 # markpoint_opts=opts.MarkPointOpts( # data=[opts.MarkPointItem(value=-2, name="周最低", x=1, y=-1.5)] # ), # markline_opts=opts.MarkLineOpts( # data=[ # opts.MarkLineItem(type_="average", name="平均值"), # opts.MarkLineItem(symbol="none", x="90%", y="max"), # opts.MarkLineItem(symbol="circle", type_="max", name="最高點"), # ] # ), ) .set_global_opts( title_opts=opts.TitleOpts(title="未來一週氣溫變化", subtitle="副標題"), # tooltip_opts=opts.TooltipOpts(trigger="axis"), # toolbox_opts=opts.ToolboxOpts(is_show=True), xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False), ) .render("最低最高溫度折線圖.html") ) print("圖表已生成!請查收!")
還記得二重積分嗎,面積代表什麼?有時候我們就想要看誰圍出來的面積大,這個在物理的實際運用中比較常見,下面來看看效果吧。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker from pyecharts.globals import ThemeType c = ( Line({"theme": ThemeType.MACARONS}) .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values(), is_smooth=True) .add_yaxis("商家B", Faker.values(), is_smooth=True) .set_series_opts( areastyle_opts=opts.AreaStyleOpts(opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts( title_opts=opts.TitleOpts(title="標題"), xaxis_opts=opts.AxisOpts( axistick_opts=opts.AxisTickOpts(is_align_with_label=True), is_scale=False, boundary_gap=False, name='類別', name_location='middle', name_gap=30, # 標籤與軸線之間的距離,預設為20,最好不要設定20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標籤字型大小 )), yaxis_opts=opts.AxisOpts( name='數量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項 ) .render("面積折線圖-緊貼Y軸.html") ) print("請查收!")
此模板和Excel裡面的視覺化差不多,沒有一點功能元素,雖然它是最簡潔的,但是我們可以通過這個進行改動,在上面創作的畫作。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.globals import ThemeType x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] y_data = [820, 932, 901, 934, 1290, 1330, 1320] ( Line({"theme": ThemeType.MACARONS}) .set_global_opts( tooltip_opts=opts.TooltipOpts(is_show=False), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標籤與軸線之間的距離,預設為20,最好不要設定20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標籤字型大小 )), yaxis_opts=opts.AxisOpts( type_="value", axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), name='數量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), ) .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="", y_axis=y_data, symbol="emptyCircle", is_symbol_show=True, label_opts=opts.LabelOpts(is_show=False), ) .render("簡單折線圖.html") )
有時候我們在處理資料的時候,發現有些類別的資料缺失了,這個時候我們想要它可以自動連線起來,那麼這個模板就可以用到了。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker from pyecharts.globals import ThemeType y = Faker.values() y[3], y[5] = None, None c = ( Line({"theme": ThemeType.WONDERLAND}) .add_xaxis(Faker.choose()) .add_yaxis("商家A", y, is_connect_nones=True) .set_global_opts(title_opts=opts.TitleOpts(title="標題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標籤與軸線之間的距離,預設為20,最好不要設定20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標籤字型大小 )), yaxis_opts=opts.AxisOpts( name='數量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), ) # toolbox_opts=opts.ToolboxOpts() # 工具選項) .render("資料缺失折線圖.html") )
此圖例未必用的上,當然也可以作為一個模板分享於此。
import pyecharts.options as opts from pyecharts.charts import Line x_data = ["一", "二", "三", "四", "五", "六", "七", "八", "九"] y_data_3 = [1, 3, 9, 27, 81, 247, 741, 2223, 6669] y_data_2 = [1, 2, 4, 8, 16, 32, 64, 128, 256] y_data_05 = [1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, 1 / 64, 1 / 128, 1 / 256, 1 / 512] ( Line(init_opts=opts.InitOpts(width="1200px", height="600px")) .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="1/2的指數", y_axis=y_data_05, linestyle_opts=opts.LineStyleOpts(width=2), ) .add_yaxis( series_name="2的指數", y_axis=y_data_2, linestyle_opts=opts.LineStyleOpts(width=2) ) .add_yaxis( series_name="3的指數", y_axis=y_data_3, linestyle_opts=opts.LineStyleOpts(width=2) ) .set_global_opts( title_opts=opts.TitleOpts(title="對數軸範例", pos_left="center"), tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}"), legend_opts=opts.LegendOpts(pos_left="left"), xaxis_opts=opts.AxisOpts(type_="category", name="x"), yaxis_opts=opts.AxisOpts( type_="log", name="y", splitline_opts=opts.SplitLineOpts(is_show=True), is_scale=True, ), ) .render("對數軸折線圖.html") )
多個折線圖展示要注意的是,資料量不能過於的接近,不然密密麻麻的折線,反而讓人看起來不舒服。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.globals import ThemeType x_data = ["週一", "週二", "週三", "週四", "週五", "週六", "週日"] y_data = [820, 932, 901, 934, 1290, 1330, 1320] ( Line({"theme": ThemeType.MACARONS}) .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="郵件行銷", stack="總量", y_axis=[120, 132, 101, 134, 90, 230, 210], label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="聯盟廣告", stack="總量", y_axis=[220, 182, 191, 234, 290, 330, 310], label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="視訊廣告", stack="總量", y_axis=[150, 232, 201, 154, 190, 330, 410], label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="直接存取", stack="總量", y_axis=[320, 332, 301, 334, 390, 330, 320], label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="搜尋引擎", stack="總量", y_axis=[820, 932, 901, 934, 1290, 1330, 1320], label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts( title_opts=opts.TitleOpts(title="折線圖堆疊"), tooltip_opts=opts.TooltipOpts(trigger="axis"), yaxis_opts=opts.AxisOpts( type_="value", axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), name='數量', name_location='middle', name_gap=40, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name='類別', name_location='middle', name_gap=30, # 標籤與軸線之間的距離,預設為20,最好不要設定20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標籤字型大小 )), ) .render("折線圖堆疊.html") )
有時候需要在一個圖裡面進行對比,那麼我們應該如何呈現一個絲滑般的曲線折線圖呢?看看這個
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker c = ( Line() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values(), is_smooth=True) # 如果不想變成曲線就刪除即可 .add_yaxis("商家B", Faker.values(), is_smooth=True) .set_global_opts(title_opts=opts.TitleOpts(title="標題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標籤與軸線之間的距離,預設為20,最好不要設定20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標籤字型大小 )), yaxis_opts=opts.AxisOpts( name='數量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項 ) .render("二維折線圖.html") )
次模板的最大的好處就是可以移動滑鼠智慧顯示資料
import pyecharts.options as opts from pyecharts.charts import Line # 將在 v1.1.0 中更改 from pyecharts.commons.utils import JsCode js_formatter = """function (params) { console.log(params); return '降水量 ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : ''); }""" ( Line(init_opts=opts.InitOpts(width="1200px", height="600px")) .add_xaxis( xaxis_data=[ "2016-1", "2016-2", "2016-3", "2016-4", "2016-5", "2016-6", "2016-7", "2016-8", "2016-9", "2016-10", "2016-11", "2016-12", ] ) .extend_axis( xaxis_data=[ "2015-1", "2015-2", "2015-3", "2015-4", "2015-5", "2015-6", "2015-7", "2015-8", "2015-9", "2015-10", "2015-11", "2015-12", ], xaxis=opts.AxisOpts( type_="category", axistick_opts=opts.AxisTickOpts(is_align_with_label=True), axisline_opts=opts.AxisLineOpts( is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#6e9ef1") ), axispointer_opts=opts.AxisPointerOpts( is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter)) ), ), ) .add_yaxis( series_name="2015 降水量", is_smooth=True, symbol="emptyCircle", is_symbol_show=False, # xaxis_index=1, color="#d14a61", y_axis=[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3], label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ) .add_yaxis( series_name="2016 降水量", is_smooth=True, symbol="emptyCircle", is_symbol_show=False, color="#6e9ef1", y_axis=[3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3, 0.7], label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), ) .set_global_opts( legend_opts=opts.LegendOpts(), tooltip_opts=opts.TooltipOpts(trigger="none", axis_pointer_type="cross"), xaxis_opts=opts.AxisOpts( type_="category", axistick_opts=opts.AxisTickOpts(is_align_with_label=True), axisline_opts=opts.AxisLineOpts( is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61") ), axispointer_opts=opts.AxisPointerOpts( is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter)) ), ), yaxis_opts=opts.AxisOpts( type_="value", splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1) ), ), ) .render("多維顏色多維折線圖.html") )
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker from pyecharts.globals import ThemeType c = ( Line({"theme": ThemeType.MACARONS}) .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values(), is_step=True) .set_global_opts(title_opts=opts.TitleOpts(title="標題"), xaxis_opts=opts.AxisOpts( name='類別', name_location='middle', name_gap=30, # 標籤與軸線之間的距離,預設為20,最好不要設定20 name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # 標籤字型大小 )), yaxis_opts=opts.AxisOpts( name='數量', name_location='middle', name_gap=30, name_textstyle_opts=opts.TextStyleOpts( font_family='Times New Roman', font_size=16 # font_weight='bolder', )), # toolbox_opts=opts.ToolboxOpts() # 工具選項 ) .render("階梯折線圖.html") )
裡面的渲染效果相當好看,可以適用於炫酷的展示,資料集可以展示也可以不展示,在相應的位置更改引數即可。
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.commons.utils import JsCode x_data = ["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"] y_data = [393, 438, 485, 631, 689, 824, 987, 1000, 1100, 1200,1500,1000,1700,1900,2000,500,1200,1300,1500,1800,1500,1900,1700,1000,1900,1800,2100,1600,2200,2300] background_color_js = ( "new echarts.graphic.LinearGradient(0, 0, 0, 1, " "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)" ) area_color_js = ( "new echarts.graphic.LinearGradient(0, 0, 0, 1, " "[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)" ) c = ( Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js))) .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="註冊總量", y_axis=y_data, is_smooth=True, is_symbol_show=True, symbol="circle", symbol_size=6, linestyle_opts=opts.LineStyleOpts(color="#fff"), label_opts=opts.LabelOpts(is_show=True, position="top", color="white"), itemstyle_opts=opts.ItemStyleOpts( color="red", border_color="#fff", border_width=3 ), tooltip_opts=opts.TooltipOpts(is_show=False), areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1), ) .set_global_opts( title_opts=opts.TitleOpts( title="OCTOBER 2015", pos_bottom="5%", pos_left="center", title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16), ), xaxis_opts=opts.AxisOpts( type_="category", boundary_gap=False, axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"), axisline_opts=opts.AxisLineOpts(is_show=False), axistick_opts=opts.AxisTickOpts( is_show=True, length=25, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"), ), splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f") ), ), yaxis_opts=opts.AxisOpts( type_="value", position="right", axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"), axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts(width=2, color="#fff") ), axistick_opts=opts.AxisTickOpts( is_show=True, length=15, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"), ), splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f") ), ), legend_opts=opts.LegendOpts(is_show=False), ) .render("高渲染.html") )
所有圖表均可設定,無論是字型的大小,還是顏色,還是背景都可以自己設定喲!下期文章我們繼續探索折線圖的魅力喲!
到此這篇關於Python視覺化神器pyecharts繪製折線圖詳情的文章就介紹到這了,更多相關python繪製折線圖內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45