Use pygal to plot the configuration of some basic parameters

preface

The process of drawing with pygal is to create an empty canvas, and then add data to the canvas.

1, Use pygal to plot the configuration of some basic parameters

Design idea of the sample code: use pygal to draw a histogram of 2-5 pages of information about Douban top250, and input the data into chart.add('',data_lst)_ LST is the data of a list nested dictionary. Each dictionary contains the following three information:

  • Douban top 250 movies per page
  • Description of links on each page of Douban Top250
  • Douban Top250 2-5 link address

The example code is as follows:

import pygal
# Specifies the scale of the x-axis
names = [f'The first{i}Page movie' for i in range(1,5)]
# Prepare data
# value is the number of movies per page
value = [
    25,
    25,
    25,
    25,
]
# xlink is the address link of Douban top250 page 2-5
xlink = [
    'https://movie.douban.com/top250?start=25&filter=',
    'https://movie.douban.com/top250?start=50&filter=',
    'https://movie.douban.com/top250?start=75&filter=',
    'https://movie.douban.com/top250?start=100&filter=',
]
# label is the description of the address link
label = [
    'Watercress top250 Page 2',
    'Watercress top250 Page 3',
    'Watercress top250 Page 4',
    'Watercress top250 Page 5',
]
data_lst = []
for index in range(len(value)):
    data_dict = {
    'value': value[index], 
    'label': label[index], 
    'xlink': xlink[index]
    			}
    data_lst.append(data_dict) # data_lst is a list nested dictionary object
print(data_lst)
from pygal.style import LightenStyle,LightColorizedStyle
# Change the default theme color to red
style = LightenStyle("#663333",base_style=LightColorizedStyle)
# to configure
config = pygal.Config()
# Rotates the text on the x axis 20 degrees
config.x_label_rotation = 20
# Hide the legend in the upper left corner
config.show_legend = False
# Configure title font size
config.title_font_size = 30
# Configure secondary labels, including x-axis and y-axis large font sizes
config.label_font_size = 20
# The main label is a multiple of a certain number on the y-axis, which is equivalent to a special scale to make the key data points more eye-catching
config.major_label_font_size = 24
# The number of characters is limited to 20, and the exceeding characters are displayed in
config.truncate_label = 20
# The y-axis reference dashed line is not displayed
config.show_y_guides = False
# Displays the x-axis reference dashed line
config.show_x_guides = True
# Configure chart width
config.width = 1000
# The first parameter is transferred to the configuration, and the second parameter is transferred to the theme color
chart = pygal.Bar(config,style=style)
# Set the title of the picture
chart.title = 'Watercress top250'
# Sets the scale of the x-axis
chart.x_labels = names
# Sets the title of the x axis
chart.x_title = 'the number of pages'
# Sets the title of the y axis
chart.y_title = 'Number of films'
# Add the data of y-axis without setting the legend to null
# Note that what is passed in here is the object of the list nested Dictionary (data_lst)
# The key -- value is the coordinate value of the y axis
chart.add('',data_lst)
chart.render_to_file('doubanTop250.svg')

The running results of the code are as follows:

Output data at the terminal_ Results of LST:

[
{'value': 25, 'label': 'Watercress top250 Page 2', 'xlink': 'https://movie.douban.com/top250?start=25&filter='}, 
{'value': 25, 'label': 'Watercress top250 Page 3', 'xlink': 'https://movie.douban.com/top250?start=50&filter='}, 
{'value': 25, 'label': 'Watercress top250 Page 4', 'xlink': 'https://movie.douban.com/top250?start=75&filter='}, 
{'value': 25, 'label': 'Watercress top250 Page 5', 'xlink': 'https://movie.douban.com/top250?start=100&filter='}
]

data_ Four data are stored in LST_ dictdata_ Dict = {number of movies per page of Douban Top250, description of links per page of Douban Top250, and link addresses of Douban Top250 2-5}
Note that the parameter name of the link in the dictionary must be specified as xlink. Only in this way can you jump to the specified link when clicking the image
Note that the parameter name of the value in the dictionary must be specified as value. Only in this way can the picture be displayed successfully. Otherwise, the picture cannot be displayed
Note that the parameter name of the description information in the dictionary must be specified as label. Only in this way can the description information of each column be successfully displayed, otherwise the information cannot be displayed

2, Two methods of configuration

The first method

# to configure
config = pygal.Config()
# Configure chart width
config.width = 1000
chart = pygal.Bar(config)

When there are many parameters to be configured, we usually use the first configuration method.
The above example code uses the first configuration method

The second method

chart = pygal.Bar(width=1000)

When fewer parameters need to be configured, we usually use the second configuration method.

Tags: Python Machine Learning echarts

Posted on Fri, 26 Nov 2021 17:16:09 -0500 by doucie