📅  最后修改于: 2023-12-03 15:34:42.635000             🧑  作者: Mango
在Python中,我们可以使用多种包来创建各种类型的图形。以下是一些常用的Python包:
Matplotlib是用Python编写的绘图库,可以创建出各种类型的图形。以下是一些Matplotlib图形的示例:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 5, 3]
plt.plot(x, y)
plt.show()
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 5, 3]
plt.bar(x, y)
plt.show()
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 5, 3]
plt.scatter(x, y)
plt.show()
Seaborn是一种基于Matplotlib的Python数据可视化库,可用于制作更加漂亮的数据可视化图表。以下是一些Seaborn图形的示例:
import seaborn as sns
import numpy as np
arr = np.random.rand(10, 10)
sns.heatmap(arr, cmap="YlGnBu")
sns.show()
import seaborn as sns
tips = sns.load_dataset("tips")
sns.displot(tips, x="total_bill", bins=10)
sns.show()
import seaborn as sns
tips = sns.load_dataset("tips")
sns.displot(tips, x="total_bill", kde=True)
sns.show()
Plotly是一种互动式数据可视化工具,可以创建交互式数据可视化图表。以下是一些Plotly图形的示例:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
size='petal_length', hover_data=['petal_width'])
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day')
fig.show()
Bokeh是一种交互式数据可视化工具,可用于创建数据可视化小部件和应用程序。以下是一些Bokeh图形的示例:
from bokeh.plotting import figure, output_file, show
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 5, 3]
output_file("line.html")
p = figure(title="折线图", x_axis_label='x', y_axis_label='y')
p.line(x, y)
show(p)
from bokeh.plotting import figure, output_file, show
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 5, 3]
y_upper = [i + 0.5 for i in y]
y_lower = [i - 0.5 for i in y]
output_file("line_with_area.html")
p = figure(title="带区域的折线图", x_axis_label='x', y_axis_label='y')
p.add_layout(p.patch(x + x[::-1], y_upper + y_lower[::-1], alpha=0.5, color='blue'))
p.line(x, y)
show(p)