In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to share with you how to use Python's pygal library to create SVG vector graphics under Ubuntu. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
SVG is a scalable two-dimensional vector graphics standard, which is used to provide high-quality vector graphics rendering. SVG has a very strong openness, so its main object in the process of application is the network.
Environment: Ubuntu 18.04 LTS
Python version: Python 3.6.7
The first step is to install pygal:
Linuxidc@linuxidc:~/linuxidc.com$ pip3 install pygal
Collecting pygal
Downloading https://files.pythonhosted.org/packages/5f/b7/201c9254ac0d2b8ffa3bb2d528d23a4130876d9ba90bc28e99633f323f17/pygal-2.4.0-py2.py3-none-any.whl (127kB)
100% | ██ | 133kB 231kB/s
Installing collected packages: pygal
Successfully installed pygal-2.4.0
If you want to set the generation format to something other than svg, such as png,jpg image format, you need to install the following libraries:
Linuxidc@linuxidc:~/linuxidc.com$ pip3 install lxml-I https://pypi.douban.com/simple/
Collecting lxml
Downloading https://pypi.doubanio.com/packages/35/8a/5e066949f2b40caac32c7b2a77da63ad304b5fbe869036cc3fe4a198f724/lxml-4.3.3-cp36-cp36m-manylinux1_x86_64.whl (5.7MB)
100% | ██ | 5.7MB 12.6MB/s
Installing collected packages: lxml
Successfully installed lxml-4.3.3
Install it in Ubuntu by following the prompts below:
Linuxidc@linuxidc:~/linuxidc.com$ sudo apt-get install libxml2-dev libxslt1-dev python-dev
Linuxidc@linuxidc:~/linuxidc.com$ sudo apt-get install python-lxml
Linuxidc@linuxidc:~/linuxidc.com$ pip3 install cairosvg-I https://pypi.douban.com/simple/
The principle of installing the library is the same as above:
Linuxidc@linuxidc:~/linuxidc.com$ sudo apt-get install python-cairosvg
The following two libraries need only be installed with normal pip3:
Linuxidc@linuxidc:~/linuxidc.com$ pip3 install tinycss-I https://pypi.douban.com/simple/
Collecting tinycss
Downloading https://pypi.doubanio.com/packages/05/59/af583fff6236c7d2f94f8175c40ce501dcefb8d1b42e4bb7a2622dff689e/tinycss-0.4.tar.gz (87kB)
100% | ██ | 92kB 824kB/s
Building wheels for collected packages: tinycss
Running setup.py bdist_wheel for tinycss... Done
Stored in directory: / home/linuxidc/.cache/pip/wheels/00/1e/22/af03548f887c6f9a423f72f819c46a4df9e2aabbb9480be30b
Successfully built tinycss
Installing collected packages: tinycss
Successfully installed tinycss-0.4
Linuxidc@linuxidc:~/linuxidc.com$ pip3 install cssselect-I https://pypi.douban.com/simple/
Collecting cssselect
Downloading https://pypi.doubanio.com/packages/7b/44/25b7283e50585f0b4156960691d951b05d061abf4a714078393e51929b30/cssselect-1.0.3-py2.py3-none-any.whl
Installing collected packages: cssselect
Successfully installed cssselect-1.0.3
Hello SVG
Import pygal
Bar_chart = pygal.Bar ()
Bar_chart.add ('linuxidc', [0,1,1,5,6,8,13,22,35,55])
Bar_chart.render_to_file ('linuxidc.com.svg')
The black linuxidc.com.svg file generated in the current directory cannot be opened directly because it is in svg format. Choose the default browser to open it, and you can see what it looks like below:
A cooler picture:
Import pygal
Line_chart = pygal.Line ()
Line_chart.title = 'Browser usage evolution (in%)'
Line_chart.x_labels = map (str, range (2002, 2013))
Line_chart.add ('Firefox', [None, None, 0,16.6,25,31,36.4,45.5,46.3,42.8,37.1])
Line_chart.add ('Chrome', [None, 0,3.9,10.8,23.8,35.3])
Line_chart.add ('IE', [85.8,84.6,84.7,74.5,66,58.6,54.7,44.8,36.2, 26.6,20.1])
Line_chart.add ('Others', [14.2,15.4,15.3,8.9,9,10.4,8.9,5.8,6.7,6.8,7.5])
Line_chart.render_to_file ('www.linuxidc.com.svg')
The resulting diagram looks like this:
Sometimes, we don't need svg, we just need charts in png format. It doesn't matter, pygal can do it:
Import pygal
Bar_chart = pygal.Bar ()
Bar_chart.add ('linuxidc', [0, 1, 1, 5, 7, 8, 15, 21, 35, 60])
Bar_chart.render_to_file ('linux.linuxidc.com.svg')
# generate charts in png format
Bar_chart.render_to_png (filename='linux.linuxidc.com.png')
The picture in png format was generated successfully:
Let the svg images generated by Pygal be displayed on your web page. We choose flask to provide web support:
Linuxidc@linuxidc:~/linuxidc.com$ pip3 install flask-I https://pypi.douban.com/simple/
The core code is as follows, yes, it is that short:
Import pygal
From flask import Flask
App = Flask (_ _ name__)
@ app.route ('/')
Def drawSVG ():
Line_chart = pygal.Line (legend_at_bottom=True,legend_box_size=18)
Line_chart.title = 'Browser usage evolution (in%)'
Line_chart.x_labels = map (str, range (2002, 2013))
Line_chart.add ('Firefox', [None, None, 0,16.6,25,31,36.4,45.5,46.3,42.8,37.1])
Line_chart.add ('Chrome', [None, 0,3.9,10.8,23.8,35.3])
Line_chart.add ('IE', [85.8,84.6,84.7,74.5,66,58.6,54.7,44.8,36.2, 26.6,20.1])
Line_chart.add ('Others', [14.2,15.4,15.3,8.9,9,10.4,8.9,5.8,6.7,6.8,7.5])
Svg = line_chart.render_response ()
Return svg
If _ _ name__ = ='_ _ main__':
App.run ()
If you open 127.0.0.1, you can see what the following looks like:
These are all the contents of the article "how to use Python's pygal library to create SVG vector graphics under Ubuntu". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.