Qt Views
Python quantitative data visualization can use the Matplotlib and Seaborn libraries.
You can install Matplotlib and Seaborn by running in the terminal or command prompt:
pip install matplotlib seaborn
For more details on Matplotlib, refer to: (#)
This chapter mainly introduces the use of the Seaborn library.
* * *
## Seaborn Library
Seaborn is a data visualization library based on Matplotlib, focused on drawing statistical graphics.
Seaborn provides some high-level interfaces and color themes, making it easier to create beautiful statistical charts in Python.
The goal of Seaborn is to make data visualization simpler while also making charts more attractive.
### 1. Simple Creation of Statistical Graphics
Seaborn provides a series of built-in plotting functions that can easily create various statistical graphics, such as scatter plots, histograms, box plots, etc.
## Example
```python
import seaborn as sns
import matplotlib.pyplot as plt
# Create Scatter Plot
sns.scatterplot(x='sepal_length', y='sepal_width', data=iris)
plt.show()
### 2. Built-in Color Themes
Seaborn provides built-in color themes that allow you to easily change the appearance of charts to make them more attractive.
```python
# Use Seaborn Color Themes sns.set(style="whitegrid")
### 3. Dataset Visualization
Seaborn contains some built-in datasets that can be directly used for plotting, such as the tips and flights datasets.
```python
# Use Built-in Dataset tips = sns.load_dataset("tips")
### 4. Visualization of Categorical Data
Seaborn is very convenient for handling categorical data, allowing you to easily create grouped bar charts, box plots, etc.
```python
# Create Grouped Bar Chart sns.barplot(x="day", y="total_bill", hue="sex", data=tips)
### 5. Visualization of Matrix Data
Seaborn provides some functions specifically for visualizing matrix data, such as heatmaps.
```python
# Create Heatmap corr_matrix = df.corr() sns.heatmap(corr_matrix, annot=True, cmap="coolwarm")
### 6. Faceted Plotting
Seaborn supports faceted plotting, which can create multiple small plots based on subsets of data to more comprehensively display the data.
```python
# Facet Plotting sns.relplot(x="total_bill", y="tip", hue="day", col="time", data=tips)
Seaborn provides a large number of graphical options and parameters to meet different types of data visualization needs.
Overall, Seaborn is a powerful and easy-to-use library, suitable for beginners and professional data scientists, helping users more easily create attractive statistical charts. If you are already familiar with Matplotlib, Seaborn is a great supplement that can make your data visualization more efficient.
* * *
## Example
Next, we will use Python for a simple quantitative example. You can combine yfinance to get stock data for Kweichow Moutai (600519.SS), and then use seaborn for data visualization.
The following is a simple example demonstrating how to download Moutai stock data and use seaborn to draw the stock's closing price trend chart:
## Example
```python
import yfinance as yf
import seaborn as sns
import matplotlib.pyplot as plt
# Fetch Kweichow Moutai Stock Data
maotai_data = yf.download("600519.SS", start="2020-01-01", end="2023-01-01")
# Select Closing Price Data
closing_prices = maotai_data['Close']
# Plot Trend Chart Using seaborn
plt.figure(figsize=(12,6))
sns.lineplot(x=closing_prices.index, y=closing_prices.values, label='Maotai Closing Prices')
plt.title('Maotai Stock Closing Prices Over Time')
plt.xlabel('Date')
plt.ylabel('Closing Price (CNY)')
plt.legend()
plt.show()
Executing the above code produces the following output:
!(#)
YouTip