import matplotlib.pyplot as plt import seaborn as sns import numpy as np
# 設(shè)置風(fēng)格 sns.set(style='whitegrid')
# 生成數(shù)據(jù) x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x)
# 創(chuàng)建圖表 plt.figure(figsize=(10, 6)) plt.plot(x, y1, label='Sine Wave', color='b', linewidth=2) plt.plot(x, y2, label='Cosine Wave', color='r', linestyle='--', linewidth=2)
# 添加裝飾 plt.fill_between(x, y1, y2, color='gray', alpha=0.1) plt.title('Line Plot', fontsize=15) plt.xlabel('X-axis', fontsize=12) plt.ylabel('Y-axis', fontsize=12) plt.legend() plt.show()
2. 散點圖
3. 條形圖import matplotlib.pyplot as plt import seaborn as sns
# 設(shè)置風(fēng)格 sns.set(style='whitegrid')
# 生成數(shù)據(jù) categories = ['A', 'B', 'C', 'D'] values1 = [5, 7, 8, 6] values2 = [3, 4, 5, 2]
# 創(chuàng)建圖表 fig, ax = plt.subplots(figsize=(10, 6)) bar1 = ax.bar(categories, values1, label='Group 1') bar2 = ax.bar(categories, values2, bottom=values1, label='Group 2')
# 添加裝飾 ax.set_title('Stacked Bar Chart', fontsize=15) ax.set_xlabel('Categories', fontsize=12) ax.set_ylabel('Values', fontsize=12) ax.legend()
# 添加數(shù)值標(biāo)簽 for rect in bar1 + bar2: height = rect.get_height() ax.annotate(f'{height}', xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), textcoords='offset points', ha='center', va='bottom')
plt.show()
4. 熱力圖
5. 箱線圖import matplotlib.pyplot as plt import seaborn as sns import numpy as np
# 設(shè)置風(fēng)格 sns.set(style='whitegrid')
# 生成數(shù)據(jù) data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
# 創(chuàng)建圖表 plt.figure(figsize=(10, 6)) sns.boxplot(data=data, palette='vlag')
# 添加裝飾 plt.title('Box Plot', fontsize=15) plt.show()
6. 蜘蛛圖
7. 雙軸圖import matplotlib.pyplot as plt import seaborn as sns import numpy as np
# 設(shè)置風(fēng)格 sns.set(style='whitegrid')
# 生成數(shù)據(jù) x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x)
# 創(chuàng)建圖表 fig, ax1 = plt.subplots(figsize=(10, 6))
ax2 = ax1.twinx() ax1.plot(x, y1, 'g-') ax2.plot(x, y2, 'b-')
# 添加裝飾 ax1.set_xlabel('X-axis') ax1.set_ylabel('Sine', color='g') ax2.set_ylabel('Cosine', color='b') plt.title('Dual Axis Plot') plt.show()
8. 面積圖
9. 帶狀圖import matplotlib.pyplot as plt import seaborn as sns import numpy as np
# 設(shè)置風(fēng)格 sns.set(style='whitegrid')
# 生成數(shù)據(jù) x = np.linspace(0, 10, 100) y = np.sin(x) z = np.sin(x) + np.random.normal(0, 0.1, 100)
# 創(chuàng)建圖表 plt.figure(figsize=(10, 6)) plt.plot(x, y, label='Sine Wave') plt.fill_between(x, y, z, where=(y >= z), interpolate=True, color='green', alpha=0.3) plt.fill_between(x, y, z, where=(y < z), interpolate=True, color='red', alpha=0.3)
# 添加裝飾 plt.title('Band Chart', fontsize=15) plt.xlabel('X-axis', fontsize=12) plt.ylabel('Y-axis', fontsize=12) plt.legend() plt.show()
10. 等高線圖
11. 極坐標(biāo)圖import numpy as np import matplotlib.pyplot as plt import seaborn as sns
# 設(shè)置風(fēng)格 sns.set(style='white')
# 數(shù)據(jù)準(zhǔn)備 theta = np.linspace(0, 2*np.pi, 100) r = np.abs(np.sin(theta) * np.cos(theta))
# 創(chuàng)建極坐標(biāo)圖 plt.figure(figsize=(8, 8)) ax = plt.subplot(111, polar=True) ax.plot(theta, r, color='b', linewidth=2)
# 添加裝飾 plt.title('Polar Plot', fontsize=15) plt.show()
12. 3D曲面圖
13. 3D散點圖import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import seaborn as sns
# 設(shè)置風(fēng)格 sns.set(style='white')
# 數(shù)據(jù)準(zhǔn)備 x = np.random.standard_normal(100) y = np.random.standard_normal(100) z = np.random.standard_normal(100)
# 創(chuàng)建3D散點圖 fig = plt.figure(figsize=(10, 6)) ax = fig.add_subplot(111, projection='3d') scatter = ax.scatter(x, y, z, c=z, cmap='viridis')
# 添加裝飾 fig.colorbar(scatter) plt.title('3D Scatter Plot', fontsize=15) plt.show()
14. 3D條形圖
15. 直方圖import matplotlib.pyplot as plt import seaborn as sns import numpy as np
# 設(shè)置風(fēng)格 sns.set(style='whitegrid')
# 生成數(shù)據(jù) data = np.random.randn(1000)
# 創(chuàng)建圖表 plt.figure(figsize=(10, 6)) sns.histplot(data, kde=True, color='purple', bins=30)
# 添加裝飾 plt.title('Histogram', fontsize=15) plt.xlabel('Value', fontsize=12) plt.ylabel('Frequency', fontsize=12) plt.show()
16.小提琴圖
17.成對關(guān)系圖 import seaborn as sns import matplotlib.pyplot as plt
# 生成數(shù)據(jù) iris = sns.load_dataset('iris')
# 創(chuàng)建圖表 sns.pairplot(iris, hue='species', palette='muted') plt.suptitle('Pair Plot', y=1.02, fontsize=15) plt.show() 18. Facet Grid 圖
|
|