matplotlib 高级画图--填充--建立子图

 

1, 填充

这个教学网址不错

fill正常就画出来曲线了,然后填充了正常将于x轴围成的区域填充。

plt.vlines(Sim_T, [0], 1, color="green", linewidth=3, linestyles="dashed")
    ax.fill_betweenx(p, Sim_T, s, facecolor="orange", color="white")
 

2,建立子图--设置坐标轴属性----这个代码相当值得学习了。

fig.tight_layout(); 这条语句最好加上,每一个子图后面都要加上

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5 * np.pi, 1000)
y1 = np.sin(x)
fig = plt.figure(figsize=(15, 8), dpi=80)
axs = fig.subplots(3, 3, sharex=True, sharey=True)

'''图一'''
axs[0,0].fill(x, y1, color="g", alpha=0.3) # 正常情况下
fig.tight_layout(); # 当子图存在时候,这条语句最好加上
'''图二'''
axs[0,1].fill(x, y1, color="g", alpha=0.3)
axs[0,1].spines['right'].set_visible(False) # 第二张图的效果,容易理解
fig.tight_layout();
'''图三'''
axs[0,2].fill(x, y1, color="g", alpha=0.3) # 三,
axs[0,2].spines['bottom'].set_position(('data',0)) # 这个语句将坐标轴移到了中间了。 0 代表:将横轴移动到了y = 0的位置。
axs[0,2].spines['left'].set_position(('data',0)) # 将纵轴移动到x=0的位置
fig.tight_layout();
'''图四'''
axs[1,0].fill(x, y1, color="g", alpha=0.3)
axs[1,0].spines['top'].set_color('none') # 第四张图的效果,容易理解
fig.tight_layout();

'''图五'''
axs[1,1].fill(x, y1, color="g", alpha=0.3)
axs[1,1].xaxis.set_ticks_position('top') # 第五张图的效果,tick变到上面了。
axs[1,1].yaxis.set_ticks_position('right')
fig.tight_layout();
'''图六'''
axs[1,2].fill(x, y1, color="g", alpha=0.3)
axs[1,2].spines['left'].set_position(('data',0)) # 将纵轴移动到0的位置
fig.tight_layout();
'''图七'''
axs[2,0].set_title('picture 7  title ')
axs[2,0].annotate('zhu  shi ', xy=(0, 0))
fig.tight_layout();
plt.show()

柱状图:

https://www.cnblogs.com/linyujin/p/9895328.html

不同的子图画不同的图像,比如1图画散点图,二图画柱状图subplot2grid

函数subplot2grid()的使用方法

http://book.51cto.com/art/201809/584017.htm

 

 

以下是几个基于 `matplotlib` 的绘示例代码及其说明: ### 示例 1:绘制简单折线 此示例展示了如何使用 `plt.plot()` 函数绘制一条简单的折线。 ```python import matplotlib.pyplot as plt # 数据准备 x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # 创建折线 plt.plot(x, y, label="Line", color="blue", linestyle="--", marker="o") # 添加标签和标题 plt.xlabel("X-axis Label") plt.ylabel("Y-axis Label") plt.title("Simple Line Plot Example") plt.legend() # 显示像 plt.show() ``` 这段代码利用了 `plt.plot()` 方法来生成一个带有标记点的虚线表[^1]。 --- ### 示例 2:水平柱状 下面是一个展示不同国家人口数量的水平柱状的例。 ```python import matplotlib.pyplot as plt name_list = ['China', 'USA', 'India', 'Russia'] num_list = [14, 3.3, 7.8, 1.46] plt.barh(name_list, num_list, color=['red', 'green', 'blue', 'purple']) plt.xlabel("Population (Billion)") plt.ylabel("Country Names") plt.title("Horizontal Bar Chart of Population by Country") plt.tight_layout() # 自动调整参数,使之填充整个像区域 plt.show() ``` 这个例中的 `plt.barh()` 方法被用来制作水平方向上的条形统计[^2]。 --- ### 示例 3:决策树分类边界可视化 这是一个更高级的应用场景——显示决策树模型对于二维空间内的分类边界的描绘方式。 ```python from sklearn.datasets import make_moons from sklearn.tree import DecisionTreeClassifier import numpy as np import matplotlib.pyplot as plt # 构建月牙形状数据集 X, y = make_moons(n_samples=100, noise=0.1) # 训练决策树模型 tree_clf = DecisionTreeClassifier(max_depth=3).fit(X, y) # 定义网格并预测每个网格单元所属类别 xx, yy = np.meshgrid(np.linspace(-2, 3, 500), np.linspace(-1, 2, 500)) Z = tree_clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # 可视化结果 fig, ax = plt.subplots(figsize=(6, 4)) ax.contourf(xx, yy, Z, alpha=0.4, cmap=plt.cm.Paired) scatter = ax.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolors='k') legend = scatter.legend_elements(prop="colors", fmt=None)[0] ax.legend(legend, ["Class 0", "Class 1"], loc="upper left") plt.title("Decision Boundary Visualization Using Decision Tree Classifier") plt.show() ``` 这里通过调用 `contourf()` 和 `scatter()` 来呈现连续变化以及离散分布两种形式的数据特征[^4]。 --- ### 示例 4:带颜色条的高斯噪声热力 最后介绍一种涉及渐变色彩表现手法的情况——即应用颜色映射技术表达数值差异程度的地样式形。 ```python import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors np.random.seed(0) data = np.random.randn(10, 10) * 10 cmap = plt.cm.get_cmap('coolwarm') # 获取指定名称的颜色映射方案 norm = mcolors.Normalize(vmin=data.min(), vmax=data.max()) # 归一化处理原始矩阵元素值域至[0,1]区间范围内 fig, ax = plt.subplots() heatmap = ax.imshow(data, interpolation='nearest', cmap=cmap, norm=norm) cb = fig.colorbar(heatmap, ax=ax) # 插入右侧垂直放置的颜色比例尺组件对象实例变量赋值给局部变量名'cb' cb.set_label('Intensity') plt.title("Gaussian Noise Heatmap with Colorbar Progression") plt.xticks([]) plt.yticks([]) plt.show() ``` 在此案例里运用到了 `imshow()` 结合自定义好的颜色梯度表单完成最终渲染效果[^5]。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值