In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to use Pyecharts for Python data Visualization", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python data Visualization Pyecharts how to use" article.
1. Install Pyechartspip install pyecharts2. Chart Foundation 2.1 theme style
The theme style is added using the InitOpts () method
The main parameters of this method are:
Parameters describe the width canvas width, requiring a string format, such as width= "500px" height canvas height, and a string format, such as width= "500px" chart_id chart ID, as the unique identifier of the chart. There are multiple charts when used to distinguish different chart page_title page title, string format theme chart theme. Bg_color chart background color, string format provided by ThemeType module
The styles you can choose from are:
2.2 Chart title
Adding a title to a chart requires the title_opts parameter of the set_global_options () method
The value of this parameter is generated by the TitleOpts () method of the opts module
And the syntax of the main parameters of TitleOpts () method is as follows:
2.3 Legend
Setting the legend requires the legend_opts parameter of the set_global_opts () method
The parameter value of this parameter refers to the LegendOpts () method of the options module.
The main parameters of the LegendOpts () method are as follows:
2.4 prompt box
The setting prompt box is mainly set through the tooltip_opts parameter in the set_global_opts () method.
The parameter value of this parameter refers to the TooltipOpts () method of the options module.
The main parameters of the TooltipOpts () method are as follows:
2.5 Visual mapping
Visual mapping is set by the visualmap_opts parameter in the set_global_opts () method
The value of this parameter refers to the VisualMapOpts () method of the options module.
The main parameters are as follows:
2.6 Toolbox
The toolbox is set through the toolbox_opts parameter in the set_global_opts () method
The value of this parameter refers to the ToolboxOpts () method of the options module.
The main parameters are as follows:
2.7 area Zoom
Region scaling is set by the datazoom_opts parameter in the set_global_opts () method
The value of this parameter refers to the DataZoomOpts () method of the options module.
The main parameters are as follows:
3. Bar chart Bar module
Drawing bar chart is realized by Bar module.
The main methods of this module are:
The main method describes add_xaxis () x-axis data add_yaxis () y-axis data reversal_axis () flips x-axis data add_dataset () raw data
The following shows a simple example without using too many complex styles:
Import numpy as npfrom pyecharts.charts import Barfrom pyecharts import options as optsfrom pyecharts.globals import ThemeType# generation data years = [2011, 2012, 2013, 2014, 2015] Y1 = [1, 3, 5, 7, 9] Y2 = [2, 4, 6, 4, 2] Y3 = [9, 7, 5, 3, 1] Y4 = list (np.random.randint (1, 10) 10) bar = Bar (init_opts=opts.InitOpts (theme=ThemeType.LIGHT)) # add x-axis and y-axis data bar.add_xaxis (years) bar.add_yaxis ('A', y1) bar.add_yaxis ('B', y2) bar.add_yaxis ('C', y3) bar.add_yaxis ('D', y4) # render the chart to the HTML file And save it in the current directory bar.render ("bar.html")
The effect of generating an image is as follows:
One inexplicable detail here is that you can see that the y4 data, that is, type D, is not shown in the image. After repeated attempts by Xiaoqiu, it is found that if the data generated by using random numbers is converted into a list, this part of random numbers will not be written to the html file:
Since you can't explain, avoid it.
4. Line chart / area chart Line module
The main methods of the Line module are add_xaxis () and add_yaxis (), which are used to add x-axis data and y-axis data, respectively.
The main parameters of add_yaxis () are as follows:
4.1 Line Chart
When drawing a line chart, the data on the x-axis must be a string before the line can be displayed properly.
From pyecharts.charts import Linefrom pyecharts import options as optsfrom pyecharts.globals import ThemeType# preparation data x = [2011, 2012, 2013, 2014, 2015] x_data = [str (I) for i in x] y1 = [1,3,2,5,8] y2 = [2,6,5,7] y3 = [5,7,4,3,1] line = Line (init_opts=opts.InitOpts (theme=ThemeType.ESSOS)) line.add_xaxis (xaxis_data=x_data) line.add_yaxis (series_name= "Class A") Y_axis=y1) line.add_yaxis (series_name= "Class B", y_axis=y2) line.add_yaxis (series_name= "Class C", y_axis=y3) line.render ("line.html")
The effect of generating an image is as follows:
4.2 area map
When drawing an area map, you need to specify the areastyle_opts parameter in the add_yaxis () method. Its value is provided by the AreaStyleOpts () method of the options module.
From pyecharts.charts import Linefrom pyecharts import options as optsfrom pyecharts.globals import ThemeTypex = [2011, 2012, 2013, 2014, 2015] x_data = [str (I) for i in x] y1 = [2,5,6,8,9] y2 = [1,4,4,7] y3 = [1,3,4,6,6] line = Line (init_opts=opts.InitOpts (theme=ThemeType.WONDERLAND)) line.add_xaxis (xaxis_data=x_data) line.add_yaxis (series_name= "Class A", y_axis=y1) Areastyle_opts=opts.AreaStyleOpts (opacity=1)) line.add_yaxis (series_name= "Class B", y_axis=y2, areastyle_opts=opts.AreaStyleOpts (opacity=1)) line.add_yaxis (series_name= "Class C", y_axis=y3, areastyle_opts=opts.AreaStyleOpts (opacity=1)) line.render ("line2.html")
The image effect is as follows:
5. Pie chart 5.1 pie chart
The pie chart is drawn using the Pie module, and the main method to be used in this module is the add () method.
The main parameters of this method are as follows:
The main parameter describes the name of the series_name series. Used for prompt text and legend labels. Data_pair data item in a format such as the color of [(key1,value1), (key2,value2)] color series labels. The radius of the radius pie chart. The default is in the form of percentage. The default is whether half of the rosetype relative to the height and width of the container is expanded into a Nightingale rose chart. You can take values such as radius goods area,radius to show the size of the data through the sector center corner, that is, the default sector chart. Area indicates that all sectors have the same angle at the center of the circle. Show the data size by radius from pyecharts.charts import Piefrom pyecharts import options as optsfrom pyecharts.globals import ThemeTypex_data = ['AAA',' BBB', 'CCC',' DDD', 'EEE',' FFF'] y_data = [200,200,100,400,500,600] # convert the data to the target format data = [list (z) for z in zip (x_data) Y_data)] # data sorting data.sort (key=lambda x: X [1]) pie = Pie (init_opts=opts.InitOpts (theme=ThemeType.MACARONS)) pie.add (series_name= "category", # sequence name data_pair=data, # data) pie.set_global_opts (# pie chart title title_opts=opts.TitleOpts (title= "quantitative analysis of categories" Pos_left= "center"), # does not show legend legend_opts=opts.LegendOpts (is_show=False),) pie.set_series_opts (# sequence label label_opts=opts.LabelOpts (),) pie.render ("pie.html")
The image effect is as follows:
5.2 Nightingale from pyecharts.charts import Piefrom pyecharts import options as optsfrom pyecharts.globals import ThemeTypex_data = ['AAA',' BBB', 'CCC',' DDD', 'EEE',' FFF', 'GGG',' HHH', 'III',' JJJ', 'KKK',' LLL', 'MMM',' NNN', 'OOO'] y_data = [200,100,400,50,600,300,500,700,800,900,1000 1100, 1200, 1300, 1500] # convert the data to the target format data = [list (z) for z in zip (x_data, y_data)] # data sorting data.sort (key=lambda x: X [1]) # create a pie chart and set the canvas size pie = Pie (init_opts=opts.InitOpts (theme=ThemeType.ROMANTIC, width='300px', height='400px')) # add data pie.add (series_name= "category", data_pair=data) to the pie chart Radius= ["8%", "160%"], # inner and outer radius center= ["65%", "65%"], # position rosetype='area', # rose chart The center angle is the same. Draw color='auto' # automatic color gradient by radius) pie.set_global_opts (# do not display legend legend_opts=opts.LegendOpts (is_show=False), # visual mapping visualmap_opts=opts.VisualMapOpts (is_show=False, min_=100, # color bar minimum max_=450000 # maximum color bar) pie.set_series_opts (# sequence label label_opts=opts.LabelOpts (position='inside', # tag location rotate=45, font_size=8) # font size) pie.render ("pie2.html")
The image effect is as follows:
6. Box diagram Boxplot module
The Boxplot class is used to draw a box chart.
Here is a detail: when preparing the y-axis data y_data, you need to put another layer of list outside the list, otherwise the graph lines will not be displayed.
Boxplot module is used to draw the box diagram.
The main methods are
Add_xaxis () and add_yaxis ()
From pyecharts.charts import Boxplotfrom pyecharts.globals import ThemeTypefrom pyecharts import options as optsy_data = [[5, 20, 22, 21, 23, 26, 25, 24, 28, 26, 29, 30, 50, 61]] boxplot = Boxplot (init_opts=opts.InitOpts (theme=ThemeType.INFOGRAPHIC)) boxplot.add_xaxis (["]) boxplot.add_yaxis (', y_axis=boxplot.prepare_data (y_data)) boxplot.render (" boxplot.html ")
The image effect is as follows:
7. Ripple special effect scatter plot EffectScatter module
The EffectScatter module is used to draw the ripple diagram, and the code example is as follows:
From pyecharts.charts import EffectScatterfrom pyecharts import options as optsfrom pyecharts.globals import ThemeTypex = [2011, 2012, 2013, 2014, 2015] x_data = [str (I) for i in x] y1 = [1,3,2,5,8] y2 = [2,6,5,7] y3 = [5,7,4,3,1] scatter = EffectScatter (init_opts=opts.InitOpts (theme=ThemeType.VINTAGE)) scatter.add_xaxis (x_data) scatter.add_yaxis (", y1) scatter.add_yaxis (") Y2) scatter.add_yaxis (", Y3) # render the chart to the HTML file Store scatter.render ("EffectScatter.html") in the directory where the program is located
The image effect is as follows:
8. Word cloud map WordCloud module
The word cloud map is drawn using the WordCloud module.
The main method is the add () method.
The main parameters of the add () method are as follows:
The main parameters of the add () method are
Prepare a txt file (001.txt) with the text content of "Orchid Pavilion Collection" as an example:
In the ninth year of Yonghe, he was in Gui Chou. At the beginning of spring, he would fix things at the Yin Orchid Pavilion in Huaiji Mountain. A group of sages is a collection of young men and women. Here there are high mountains, Mao Lin Xiuzhu, and there are clear currents and rapids, reflecting the left and right sides of the belt, which is regarded as the origin of curved water, followed by sitting. Although there is no silk bamboo orchestra, a song at the beginning is enough to tell the story of the secret love.
On that day, the sky was clear and the wind was smooth. Looking at the size of the universe, overlooking the prosperity of categories, so sightseeing Gao Huai, enough to be extremely audio-visual entertainment, believe in cola.
The companionship of his wife will always be admired. Or take it into your arms and understand the words in the first room, or go out of your way because of your trust. Although they are different in interest and restlessness, when they are happy with what they have encountered, they are temporarily satisfied with themselves, and they do not know that old age is coming; and their tiredness, feelings change with things, and feelings are tied to it. To the delight of it, between the pitching, has been a thing of the past, still can not be happy with it, the situation to repair the short, the end of the period! The ancients said, "death and life are also great." Doesn't it hurt!
The reason for the interest of people in the past, if they agree with each other, there is no mourning without prose, which cannot be described in the bosom. Although we know that death and life is an illusion, Qi Peng Sham is a delusion. After looking at the present, but also still look at the past, sad man! Therefore, the people of the time of narration recorded what they said, although the world was different, so they were interested in it. Those who visit later will also have a feeling of gentleness.
The code example is as follows:
Based on the TextRank algorithm, from pyecharts.charts import WordCloudfrom jieba import analyse# extracted the keywords textrank = analyse.textranktext = open ('001.txtwords,' ringing, encoding='UTF-8'). Read () keywords = textrank (text, topK=30) list1 = [] tup1 = () # keyword list for keyword, weight in textrank (text, topK=30, withWeight=True): # print ('% s% s'% (keyword, weight)) tup1 = (keyword) Weight) # keyword weight list1.append (tup1) # added to the list # drawing word mywordcloud = WordCloud () mywordcloud.add (', list1, word_size_range= [20,100]) mywordcloud.render ('wordclound.html')
The effect of the word cloud map is as follows:
9. Thermal map HeatMap module
The HeatMap module is used to draw the thermal map.
Taking the two-color ball case as an example, the data uses the generated random number to draw the thermal map:
Import pyecharts.options as optsfrom pyecharts.charts import HeatMapimport pandas as pdimport numpy as np# creates a DataFrame with 33 rows and 7 columns, and the data is generated using random numbers. Each data represents the number of occurrences of the number at that location S1 = np.random.randint (0200,33) S2 = np.random.randint (0200,33) S3 = np.random.randint (0200,33) S4 = np.random.randint (0200,33) S5 = np.random.randint (0200,33) S6 = np.random.randint (0200,33) S7 = np.random.randint (0200) 33) data = pd.DataFrame ({'position one': S1, 'position two': S2, 'position three': S3, 'position four': S4, 'position five': S5, 'position six': S6, 'position seven': S7}, index=range (1) (34)) # data conversion to list format supported by HeatMap value1 = [] for i in range (7): for j in range (33): value1.append ([I, j, int (data.iloc [j, I])]) # drawing heat map x = data.columnsheatmap=HeatMap (init_opts=opts.InitOpts (width='600px', height='650px')) heatmap.add_xaxis (x) heatmap.add_yaxis ("aa", list (data.index), value=value1 # y-axis data # y-axis label label_opts=opts.LabelOpts (is_show=True, color='white', position= "center") heatmap.set_global_opts (title_opts=opts.TitleOpts (title= "two-color ball winning number heat map", pos_left= "center"), legend_opts=opts.LegendOpts (is_show=False) # No legend # axis configuration item xaxis_opts=opts.AxisOpts (type_= "category", # category axis # separated area configuration item splitarea_opts=opts.SplitAreaOpts (is_show=True) # area fill pattern areastyle_opts=opts.AreaStyleOpts (opacity=1),), # axis configuration item yaxis_opts=opts.AxisOpts (type_= "category") # catalog axis # separated area configuration item splitarea_opts=opts.SplitAreaOpts (is_show=True, # area fill pattern areastyle_opts=opts.AreaStyleOpts (opacity=1)) ), # Visual Mapping configuration item visualmap_opts=opts.VisualMapOpts (is_piecewise=True, # segmented display min_=1, max_=170 # minimum, maximum orient='horizontal', # horizontal pos_left= "center") # Center) heatmap.render ("heatmap.html")
The effect of thermal map is as follows:
10. Water polo chart Liquid module
The Liquid module is used to draw the water polo map.
From pyecharts.charts import Liquidliquid = Liquid () liquid.add ('', [0.39]) liquid.render ("liquid.html")
The effect of the water polo map is as follows:
11. Calendar chart Calendar module
The calendar chart is drawn using the Calendar module
The main method used is the add () method
Import pandas as pdimport numpy as npfrom pyecharts import options as optsfrom pyecharts.charts import Calendardata = list (np.random.random (30)) # find the maximum and minimum mymax = round (max (data), 2) mymin = round (min (data), 2) # Generation date index = pd.date_range ('20220401,' 20220430') # merge list data_list = list (zip (index, data)) # generate calendar calendar = Calendar () calendar.add (",", data_list) Calendar_opts=opts.CalendarOpts (range_= ['2022-04-01,' 2022-04-30]) calendar.set_global_opts (title_opts=opts.TitleOpts (title= April 2022, pos_left='center'), visualmap_opts=opts.VisualMapOpts (max_=mymax, min_=mymin+0.1, orient= "horizontal", is_piecewise=True) Pos_top= "230px", pos_left= "70px",),) calendar.render ("calendar.html")
The effect of the calendar chart is as follows:
The above is about the content of this article on "how to use Pyecharts for Python data Visualization". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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.