简介:本文将介绍使用Python Matplotlib库绘制各种常见图表的方法,包括线形图、柱状图、散点图、饼图、热力图等。我们将使用简明扼要的语言,结合实例和图表,帮助读者快速掌握Matplotlib的使用技巧。
在Python中,Matplotlib是一个非常流行的可视化库,用于绘制各种图表。以下是一些常用的图表绘制代码的示例:
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]plt.plot(x, y)plt.xlabel('X轴标签')plt.ylabel('Y轴标签')plt.title('线形图')plt.show()
import matplotlib.pyplot as pltcategories = ['Category1', 'Category2', 'Category3']values = [10, 15, 7]plt.bar(categories, values)plt.xlabel('类别')plt.ylabel('值')plt.title('柱状图')plt.show()
import matplotlib.pyplot as pltimport numpy as npx = np.random.rand(50) * 10y = np.random.rand(50) * 10plt.scatter(x, y)plt.xlabel('X轴标签')plt.ylabel('Y轴标签')plt.title('散点图')plt.show()
import matplotlib.pyplot as pltlabels = ['A', 'B', 'C']sizes = [15, 30, 55]plt.pie(sizes, labels=labels)plt.title('饼图')plt.show()
import pandas as pdimport numpy as npimport seaborn as sns; sns.set() # for plot styling consistency with Matplotlib's defaults (optional)import matplotlib.pyplot as plt; plt.rcParams['figure.figsize'] = (8, 6) # for consistent figure sizes (optional)