找到 10786 篇文章 关于 Python

如何在 Seaborn 中并排绘制两个 lmplots 图(Matplotlib)?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:35:10

10K+ 浏览量

要在 Seaborn 中并排绘制两个图形,我们可以采取以下步骤:- 要创建两个图形,我们可以使用 nrows=1、ncols=2 和图形大小 (7, 7)。- 使用 Pandas 创建一个包含键、col1 和 col2 的数据框。- 使用 countplot() 显示每个分类箱中观测值的计数,使用条形图。- 调整子图之间和周围的填充。- 要显示图形,请使用 show() 方法。示例import pandas as pd import numpy as np import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True f, axes = plt.subplots(1, 2) df = pd.DataFrame(dict(col1=np.linspace(1, 10, 5), col2=np.linspace(1, 10, 5))) sns.countplot(df.col1, x='col1', ... 阅读更多

将 Matplotlib 图形显示为全屏图像

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:31:38

13K+ 浏览量

要将 matplotlib 图形显示为全屏,我们可以使用 full_screen_toggle() 方法。步骤- 使用 figure() 方法创建一个图形或激活一个现有图形。- 使用两个列表绘制一条线。- 返回当前图形的图形管理器。- 要切换全屏图像,请使用 full_screen_toggle() 方法。- 要显示图形,请使用 show() 方法。示例from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.figure() plt.plot([1, 2], [1, 2]) manager = plt.get_current_fig_manager() manager.full_screen_toggle() plt.show()输出

如何使用任意数据在 Matplotlib 中绘制 4D 图?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:28:35

6K+ 浏览量

要绘制 4D 图,我们可以创建 x、y、z 和 c 标准数据点。创建一个新图形或激活一个现有图形。步骤- 使用 figure() 方法创建图形或激活现有图形。- 将图形添加为子图排列的一部分。- 使用 numpy 创建 x、y、z 和 c 数据点。- 使用 scatter 方法创建散点图。- 要显示图形,请使用 show() 方法。示例from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.random.standard_normal(100) y = np.random.standard_normal(100) z = np.random.standard_normal(100) c = np.random.standard_normal(100) img = ax.scatter(x, ... 阅读更多

如何使用输入 *.txt 文件绘制一个非常简单的条形图(Python,Matplotlib)?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:25:59

811 浏览量

要从输入文本文件绘制一个非常简单的条形图,我们可以采取以下步骤:- 为条形名称和高度创建一个空列表。- 读取文本文件并迭代每一行。- 将名称和高度追加到列表中。- 使用列表绘制条形图(步骤 1)。- 要显示图形,请使用 show() 方法。示例from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True bar_names = [] bar_heights = [] for line in open("test_data.txt", "r"):    bar_name, bar_height = line.split()    bar_names.append(bar_name)    bar_heights.append(bar_height) plt.bar(bar_names, bar_heights) plt.show()"test_data.txt" 包含以下数据:- Javed 75 Raju 65 Kiran 55 Rishi 95输出阅读更多

如何在 Matplotlib 中使两个直方图具有相同的箱宽?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:20:36

2K+ 浏览量

要使两个直方图具有相同的箱宽,我们可以计算一组数据的直方图。步骤- 创建随机数据 a 和正态分布 b。- 为相同的箱宽初始化一个变量 bins。- 使用 hist() 方法绘制 a 和 bins。- 使用 hist() 方法绘制 b 和 bins。- 要显示图形,请使用 show() 方法。示例import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True a = np.random.random(100) * 0.5 b = 1 - np.random.normal(size=100) * 0.1 bins = 10 bins = np.histogram(np.hstack((a, b)), bins=bins)[1] plt.hist(a, bins, edgecolor='black') plt.hist(b, bins, edgecolor='black') plt.show()输出阅读更多

如何在 Matplotlib 中绘制一个圆内的矩形?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:12:51

647 浏览量

要在 matplotlib 中绘制一个圆内的矩形,我们可以采取以下步骤:- 使用 figure 方法创建一个新图形或激活一个现有图形。- 向当前轴添加一个子图。- 使用 Rectangle() 和 Circle() 类创建一个矩形和一个圆实例。- 在轴上添加一个补丁。- 使用 xlim() 和 ylim() 方法缩放 x 和 y 轴。- 要显示图形,请使用 show() 方法。示例import matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) rect1 = patches.Rectangle((-2, -2), 4, 2, color='yellow') circle1 = matplotlib.patches.Circle((0, 0), radius=3, color='red') ax.add_patch(circle1) ax.add_patch(rect1) plt.xlim([-5, 5]) plt.ylim([-5, 5]) plt.axis('equal') plt.show()输出阅读更多

Matplotlib 中 set_xlim 和 set_xbound 的区别是什么?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:09:52

383 浏览量

set_xlim - 设置 X 轴视图限制。set_xbound - 设置 X 轴的下限和上限数值边界。要设置 xlim 和 xbound,我们可以采取以下步骤:- 使用 subplots(2),我们可以创建一个图形和一组子图。在这里,我们正在创建 2 个子图。- 使用 numpy 创建 x 和 y 数据点。- 使用 plot() 方法使用轴 1 绘制 x 和 y 数据点。- 使用 set_xlim() 方法设置 x 限制。- 使用轴 2 使用 plot() 方法绘制 x 和 y 数据点。- 使用 set_xbound() 方法设置 xbound。- 要显示图形,请使用 show() 方法。示例import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] ... 阅读更多

如何在 Matplotlib 中为 pcolormesh 创建动画?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:05:57

3K+ 浏览量

要在 matplotlib 中为 pcolormesh 创建动画,我们可以采取以下步骤:- 创建一个图形和一组子图。- 使用 numpy 创建 x、y 和 t 数据点。- 创建 X3、Y3 和 T3,使用 meshgrid 从坐标向量返回坐标矩阵。- 使用 pcolormesh() 方法创建具有非规则矩形网格的伪彩色图。- 使用 colormesh 轴创建颜色条。- 使用 Animation() 类方法为 pcolormesh 创建动画。- 要显示图形,请使用 show() 方法。示例import numpy as np from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(-3, 3, 91) t = np.linspace(0, 25, 30) y = np.linspace(-3, 3, 91) X3, Y3, T3 = ... 阅读更多

如何在 Matplotlib 中为 hist2d 图添加颜色条?

Rishikesh Kumar Rishi
更新于 2021年5月11日 13:01:58

4K+ 浏览量

要为 hist2d 图添加颜色条,我们可以将标量映射对象传递给 colorbar() 方法的参数。步骤- 使用 numpy 创建 x 和 y 数据点。- 使用 subplots() 方法创建一个图形和一组子图。- 使用 hist2d() 方法创建一个 2D 直方图图。- 为 hist2d 标量映射实例创建颜色条。- 要显示图形,请使用 show() 方法。示例import numpy as np from matplotlib import pyplot as plt, colors plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.randn(100) y = np.random.randn(100) + 5 fig, ax = plt.subplots() hh = ax.hist2d(x, y, bins=40, norm=colors.LogNorm()) fig.colorbar(hh[3], ax=ax) plt.show()输出阅读更多

如何在 Python Matplotlib 中绘制密度图?

Rishikesh Kumar Rishi
更新于 2021年9月29日 12:38:42

3K+ 浏览量

要在 Python 中绘制密度图,我们可以按照以下步骤操作:- 使用 numpy 创建 side、x、y 和 z。Numpy linspace 有助于根据第三个数字创建两个点之间的数据。- 使用 side 数据从坐标向量返回坐标矩阵。- 使用 x 和 y 创建指数数据(步骤 2)。- 使用 pcolormesh() 方法创建具有非规则矩形网格的伪彩色图。- 要显示图形,请使用 show() 方法。示例from matplotlib import pyplot as plt, cm, colors import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True side = np.linspace(-2, 2, 15) X, Y = np.meshgrid(side, side) Z = np.exp(-((X - 1) ... 阅读更多

广告
© . All rights reserved.