import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 创建数据
X = np.arange(0,10,1)
Y = X +5* np.random.random((5, X.size))# 我们可以使用4种类型的基线:
baseline =["zero","sym","wiggle","weighted_wiggle"]# 让我们绘制4个图,每个基线1个for n, v inenumerate(baseline):if n<3:
plt.tick_params(labelbottom='off')
plt.subplot(2,2, n +1)
plt.stackplot(X,*Y, baseline=v)
plt.title(v)
plt.tight_layout()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 创建数据集:
df=pd.DataFrame({
'x_values':range(1,101),'y_values': np.random.randn(100)*15+range(1,101)})# 情节
plt.plot('x_values','y_values', data=df, linestyle='none', marker='o')
plt.show()
5.基本气泡图
import matplotlib.pyplot as plt
import numpy as np
# 创建数据
x = np.random.rand(40)
y = np.random.rand(40)
z = np.random.rand(40)# 使用散射函数
plt.scatter(x, y, s=z*1000, alpha=0.5)# 显示图表
plt.show()
6.面积图和饰面
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# 设定海狮风格
sns.set_style("whitegrid")# 调色板
blue,= sns.color_palette("muted",1)# 创建数据
x = np.arange(23)
y = np.random.randint(8,20,23)# 绘制绘图
fig, ax = plt.subplots()
ax.plot(x, y, color=blue, lw=3)
ax.fill_between(x,0, y, alpha=.3)
ax.set(xlim=(0,len(x)-1), ylim=(0,None), xticks=x)# 显示图形
plt.show()
7.自定义matplotlib标题
import matplotlib.pyplot as plt
import numpy as np
# 数据
x = np.random.normal(size=50000)
y = x *3+ np.random.normal(size=50000)# 2D直方图
plt.hist2d(x, y, bins=(50,50), cmap=plt.cm.Reds)# 添加基本标题
plt.title("A 2D histogram")# 显示图形
plt.show()
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
# 数据
x = np.linspace(0,2*np.pi,100)
y = np.sin(x)+ np.random.uniform(size=len(x))-0.2# 如果y轴值等于或大于0,则创建颜色
my_color = np.where(y>=0,'orange','skyblue')# 使用vline函数绘制垂直图
plt.vlines(x=x, ymin=0, ymax=y, color=my_color, alpha=0.4)
plt.scatter(x, y, color=my_color, s=1, alpha=1)# 添加标题和轴名称
plt.title("Evolution of the value of ...", loc='left')
plt.xlabel('Value of the variable')
plt.ylabel('Group')# 显示图形
plt.show(
10.基本堆积面积图
import numpy as np
import matplotlib.pyplot as plt
# 你的x轴和y轴
x=range(1,6)
y=[[1,4,6,8,9],[2,2,7,10,12],[2,8,5,10,6]]# 基本堆积面积图。
plt.stackplot(x,y, labels=['A','B','C'])
plt.legend(loc='upper left')
plt.show()
11.控制堆叠区域图中的颜色
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 你的x轴和y轴
x =range(1,6)
y =[[10,4,6,5,3],[12,2,7,10,1],[8,18,5,7,6]]# 使用已知的调色板
pal = sns.color_palette("Set1")
plt.stackplot(x,y, labels=['A','B','C'], colors=pal, alpha=0.4)
plt.legend(loc='upper right')# 创建调色板
pal =["#9b59b6","#e74c3c","#34495e","#2ecc71"]
plt.stackplot(x,y, labels=['A','B','C'], colors=pal