Python 常用程序包

Python Common Packages


1. Plot


1.1. Matplotlib

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations.

1.1.1. Install

1# Pip
2pip install matplotlib
3# Conda
4conda install -c conda-forge matplotlib
5conda install conda-forge::matplotlib

1.1.2. Example

1# import matplotlib.pyplot as plt
2from matplotlib import pyplot as plt
3%matplotlib inline # Inline mode
4
5fig, ax = plt.subplots()             # Create a figure containing a single Axes.
6ax.plot([1, 2, 3, 4], [1, 4, 2, 3])  # Plot some data on the Axes.
7# plt.show()                           # Show the figure.

1.1.3. Pie Example

1# import matplotlib.pyplot as plt
2from matplotlib import pyplot as plt
3%matplotlib inline # Inline mode
4
5fig, ax = plt.subplots()             # Create a figure containing a single Axes.
6ax.pie([1,2,3], labels = ['A', 'B', 'C'], autopct='%1.1f%%')

1.1.4 Chinese support

1# Install font
2sudo apt install fonts-wqy-zenhei
3
4# Checkout font localtion, the font will be located at /usr/share/fonts/turetype/....
5fc-list :lang=zh
6# https://transfonter.org/ttc-unpack convert(unpacking) ttc to ttf.
1# Checkout matplotlib font locations
2# Example /opt/conda/envs/ml-py310/lib/python3.10/site-packages/matplotlib/mpl-data/matplotlibrc
3# Font location at /opt/conda/envs/ml-py310/lib/python3.10/site-packages/matplotlib/mpl-data/fonts/ttf
4import matplotlib
5print(matplotlib.matplotlib_fname())
1# Copy ttf file into Font location
2cp xxxx.ttf (some python site-packages)/matplotlib/mpl-data/fonts/ttf
1# Try the Chinese character correctly display or not
2import matplotlib.pyplot as plt
3fig, ax = plt.subplots()
4ax.text(
5    .5, .5, "There are 几个汉字 in between!",
6    family=['DejaVu Sans', 'WenQuanYi Zen Hei'],
7    ha='center'
8)
1# Clean cache of matplotlib
2import shutil
3import matplotlib
4
5shutil.rmtree(matplotlib.get_cachedir())

1.2. Seaborn

Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.

1.2.1. Install

1# Pip
2pip install seaborn
3# Conda
4conda install conda-forge::seaborn

1.2.2. Example

1from matplotlib import pyplot as plt
2import numpy as np
3import seaborn as sns
4sns.set()  # Declearing using Seaborn Style
5%matplotlib inline # Inline mode
6
7fig, ax = plt.subplots()
8ax.plot([1, 2, 3, 4], [1, 4, 2, 3])

2. Geography


2.1. Folium

Folium builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the Leaflet.js library. Manipulate your data in Python, then visualize it in a Leaflet map via Folium.

2.1.1. Install

1# Pip
2pip install folium
3# Conda
4conda install conda-forge::folium

2.1.2. Example

1import folium
2m = folium.Map(location=(45.5236, -122.6750)) # Give a geo location
3m # The map will be shown as below.

3. Database


3.1. Pymysql

 1import pymysql.cursors
 2connection = pymysql.connect(host='host.docker.internal',
 3                             user='root',
 4                             password='6yhn*IK<',
 5                             database='reviewboard',
 6                             cursorclass=pymysql.cursors.DictCursor)
 7
 8# df_db = pd.read_sql_table("data", connection)
 9
10with connection:
11  with connection.cursor() as cursor:
12    SQL = "SELECT dt, sum(sale_amount), sum(origin_amount) FROM sale_202402_shoe_202401_202404_sales_direct WHERE store_code = 'XG02' GROUP BY dt ORDER BY dt"
13    cursor.execute(SQL)
14    result = cursor.fetchall()
15    print(result)

3.2. Sqlalchemy

1import sqlalchemy as sa
2import pymysql
3pymysql.install_as_MySQLdb()
4
5SQL = "SELECT dt as ds, sum(sale_amount) as y FROM sale_202302_shoe_202301_202304_sales_direct WHERE store_code = 'S107' AND sku = 'ARMT013-4' GROUP BY dt ORDER BY dt;"
6engine = sa.create_engine("mysql+mysqldb://root:6yhn*IK<@host.docker.internal/reviewboard")
7df_db = pd.read_sql(sa.text(SQL), engine)
8df_db.head()
作者|Author: RockSolid
发表日期|Publish Date: Jun 27, 2024
修改日期|Modified Date: Jun 27, 2024
版权许可|Copyright License: CC BY-NC-ND 3.0 CN