In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use pyecharts to achieve data visualization in Python. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
Map
First import the required package
From pyecharts.charts import Pie, Grid,Bar,Linefrom pyecharts.faker import Faker # packet from pyecharts.charts import Map,Geofrom pyecharts import options as optsfrom pyecharts.globals import ThemeType
OK, I now have a set of data from a province, which looks something like this.
Locate = ['Hefei City', 'Fuyang City', 'Bozhou City', 'Anqing City','Ma'anshan City', 'Tongling City', 'Liu'an City', 'Chuzhou City', 'Chizhou City', 'Bengbu City', 'Wuhu City', 'Suzhou City', 'Xuancheng City', 'Huaibei City', 'Huainan City', 'Huangshan City'] `Anqing City,'Ma'anshan City', 'Tongling City', 'Liu'an City', 'Chuzhou City', 'Chizhou City', 'Bengbu City', 'Wuhu City', 'Suzhou City', 'Xuancheng City', 'Huaibei City', 'Huainan City', 'Huangshan City'] `data = ['115', 105','72', 66', 30' '22', 41', 11, 11, 88, 27, 27, 5, 22, 14, 9]
This is also the data format that needs to draw a map, two list, one is the place name, one is the corresponding data of each city, now execute the following code to get the epidemic map of Anhui Province.
List1 = [[locate [I]] for i in range (len (locate))] # first create the data map_1 = Map (init_opts=opts.InitOpts (width= "400px", height= "460px")) # create a map, where you can resize or modify the theme color in parentheses. Map_1.add ("Anhui epidemic", list1, maptype= "Anhui") # add Anhui map map_1.set_global_opts (# set global configuration item # title_opts=opts.TitleOpts (title= "Anhui epidemic"), add title visualmap_opts=opts.VisualMapOpts (max_=120, is_piecewise=True), # most big data scope and use segmented legend_opts=opts.LegendOpts (is_show=False) # whether to display the legend) map_1.render_notebook () # display directly in notebook # map_1.render ('map1.html') Save the map as html in the working directory
Of course, there are many configuration items that can be customized in the map. Just select the configuration items you need and add them to the corresponding function.
# data item (coordinate point name, coordinate point value) data_pair: Sequence, # map type, refer to pyecharts.datasets.map_filenames.json file maptype: str = "china", # whether to select legend is_selected: bool = True, # whether to enable mouse zooming and roaming. Is_roam: bool = True, # the center of the current view, expressed in latitude and longitude center: Optional [Sequence] = None, # the zoom ratio of the current view. Zoom: Optional [Numeric] = 1, # name mapping for custom regions name_map: Optional [dict] = None, # tag graphics shape symbol: Optional [str] = None, # whether to display tag graphics is_map_symbol_show: bool = True, # tag configuration item Refer to `series_ options.LabelOpts` label_opts: Union [opts.LabelOpts, dict] = opts.LabelOpts (), # prompt box component configuration item, refer to `series_ options.TooltipOpts` tooltip_opts: Union [opts.TooltipOpts, dict, None] = None, # element style configuration item, refer to `series_ options.ItemStyleOpts` itemstyle_opts: Union [opts.ItemStyleOpts, dict, None] = None, # highlight label configuration item Refer to `series_ options.LabelOpts` emphasis_label_opts: Union [opts.LabelOpts, dict, None] = None, # highlight element style configuration item, reference `series_ options.ItemStyleOpts` emphasis_itemstyle_opts: Union [opts.ItemStyleOpts, dict, None] = None, pie chart
Continue to use the data in the map to draw the pie chart. Now if you want to see the distribution ratio of the epidemic in various regions of Anhui, you can consider using the pie chart (rose chart). Detailed code
Map_2 = (Pie (init_opts=opts.InitOpts (width= "600px", height= "500px")) create a pie chart .add ("", # name [[locate [I], data [I]] for i in range (len (locate))], # add data radius= ["40%", "75%"] # adjust radius) .set _ global_opts (legend_opts=opts.LegendOpts (orient= "vertical", pos_top= "10%", pos_left= "88%" # legend settings) ) .set _ series_opts (label_opts=opts.LabelOpts (formatter= "{b}: {c}") # set label) map_2.render_notebook () # display # map_2.render ('map2.html') # directly in notebook and save to the local bar chart
Demo
C = (Bar () .add _ xaxis (Faker.choose ()) .add _ yaxis ("merchant A", Faker.values ()) # data configuration .add _ yaxis ("merchant B", Faker.values ()) # data configuration .set _ global_opts (title_opts=opts.TitleOpts (title= "Bar- basic example") Subtitle= "I am the subtitle") # Global configuration title) c.render_notebook () you can adjust the title, legend, weight, position, background image, etc., by adding configuration items.
# Series data yaxis_data: Sequence [Numeric, opts.BarItem, dict], # whether to select the legend is_selected: bool = True, # index of the x-axis used is useful when there are multiple x-axes in a single chart instance. Xaxis_index: Optional [Numeric] = None, the index of the y-axis used by # is useful when there are multiple y-axes in a single chart instance. Yaxis_index: Optional [Numeric] = None, # series label color color: Optional [str] = None, # data stack, stack values with the same series configuration on the same category axis can be stacked. Stack: Optional [str] = None, # the distance between columns of the same series defaults to 20% of the category spacing, you can set a fixed value category_gap: Union [Numeric, str] = "20%", and # the distance between columns of different series is a percentage (for example,'30% distance, which represents 30% of the column width). # if you want the columns of two series to overlap, you can set gap to'- 100%'. This is useful when using pillars as backgrounds. Gap: Optional [str] = None, # tag configuration item, reference `series_ options.LabelOpts` label_opts: Union [opts.LabelOpts, dict] = opts.LabelOpts (), # marked point configuration item, reference `series_ options.MarkPointOpts` markpoint_opts: Union [opts.MarkPointOpts, dict, None] = None, # tagged line configuration item, reference `series_ options.MarkLineOpts` markline_opts: Union [opts.MarkLineOpts, dict, None] = None, # prompt box component configuration item Refer to `series_ options.TooltipOpts` tooltip_opts: Union [opts.TooltipOpts, dict, None] = None, # entity style configuration item, refer to `series_ options.ItemStyleOpts` itemstyle_opts: Union [opts.ItemStyleOpts, dict, None] = None, draw multiple maps on the same layer
If you want to overlay and draw graphics at the same time, you can refer to the following methods
Def two_pic ()-> Bar: X = Faker.choose () # Select data bar = (# draw bar Bar () .add _ xaxis (x) .add _ yaxis ("merchant A", Faker.values ()) .add _ yaxis ("merchant B") Faker.values () .set _ global_opts (title_opts=opts.TitleOpts (title= "Overlap-line+scatter")) line = (# add line Line () .add _ xaxis (x) .add _ yaxis ("merchant A", Faker.values ()) .add _ yaxis ("merchant B", Faker.values ()) bar.overlap (line) return bartwo_pic () .render_notebook () summary
The above is how to use pyecharts to achieve data visualization in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.