如何对齐重叠图中的直方图 bin 边缘
How to align histogram bin edges in overlaid plots
我已经设法让两个直方图重叠,但如果仔细观察,条形开始倾斜并且没有完全重叠。
我已经调整了线宽和宽度,并没有改善
我的目标是让所有的条形排列在彼此的顶部,黑边没有歪斜。
关于如何解决这个问题的任何想法
这是我的代码:
import matplotlib.pyplot as plt
import numpy
True_Distance = sort_by_Distance_below_4kpc_and_retrabmag_no_99s["true distance"].tolist()
Retr_Distance = sort_by_Distance_below_4kpc_and_retrabmag_no_99s["retrieved distance from observed parallax"].tolist()
plt.figure(figsize=(8,6))
plt.hist(True_Distance, normed=True, bins = 40, alpha=0.75, color = "mediumorchid", label="True Distance", edgecolor='black', linewidth=0.1, width=200)
plt.hist(Retr_Distance, normed=True, bins = 20, alpha=0.5, color = "lightskyblue", label="Retrieved Distance", edgecolor='black', linewidth=0.1, width=200)
# Add title and axis names
plt.title('Number distribution of stars with distance')
plt.xlabel('Distance (parsecs)')
plt.ylabel('Number of stars')
plt.legend()
输出如下:
您是否为直方图和 x 轴定义了 bin 大小。您应该将两个直方图的 x 轴定义为相同,并在每个直方图中为它们提供相同数量的 bin。然后,当您绘制它们时,它们应该沿 x 轴是相同的,但不同箱子的箱子高度不同。
- 有几种方法可以处理 bin 边缘对齐
- 如果
'distance'
类别(例如 'methods'
)和值以整齐的格式单独提供,seaborn.histplot
API 将正确对齐各种类别,当使用 hue
参数时。
- 要使用此选项,您的列必须堆叠,因此测量方法在一列中,距离在另一列中,这可以通过以下代码行完成。
df = sort_by_Distance_below_4kpc_and_retrabmag_no_99s[['true distance', 'retrieved distance from observed parallax']].stack().reset_index(level=1).rename(columns={'level_1': 'method', 0: 'distance'})
- 如评论中 JohanC 所述,如果您单独绘制数据,如 OP 中所示,则必须指定 bin 边缘。
seaborn
is a high-level API for matplotlib
.
- 此示例的数据集是从
seaborn
示例数据集导入的,并在 NASA Exoplanet Explorations 中进行了说明。与地球的距离是光年。
示例数据和导入
plants
数据集与您的星距数据集非常吻合。这里,'method'
. 有几个值
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["patch.force_edgecolor"] = True
# import some test data
df = sns.load_dataset('planets')
# display(df.head())
method number orbital_period mass distance year
0 Radial Velocity 1 269.300 7.10 77.40 2006
1 Radial Velocity 1 874.774 2.21 56.95 2008
2 Radial Velocity 1 763.000 2.60 19.84 2011
3 Radial Velocity 1 326.030 19.40 110.62 2007
4 Radial Velocity 1 516.220 10.50 119.47 2009
将所有'methods'
画在一起
- 如您所见,无论如何指定
bins
,边缘始终对齐
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(10, 10))
data = df[df.distance < 801]
sns.histplot(data=data, x='distance', hue='method', ax=ax1, bins=np.arange(0, 801, 80))
sns.histplot(data=data, x='distance', hue='method', ax=ax2, bins=20)
sns.histplot(data=data, x='distance', hue='method', ax=ax3)
Select 'method'
单独和 plot
- 只有当两组数据的边缘定义相同时,bin 边缘才对齐
ax2
。
- 使用
sns.histplot
绘图,而不使用 hue
,“大部分”等同于使用 plt.hist(...)
绘图
- 有一些不同的默认值。例如
bins
:sns.hist
使用 auto
而 plt.hist
默认为 10,正如 seaborn
的创建者 mwaskom 所指出的那样。
# create a dataframe for two values from the method column
radial = data[data.method == 'Radial Velocity']
transit = data[data.method == 'Transit']
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(10, 10))
# number of bins and edges determined by the API
sns.histplot(data=transit, x='distance', color="lightskyblue", ax=ax1)
sns.histplot(data=radial, x='distance', color="mediumorchid", ax=ax1)
# bin edges defined the same for both plots
sns.histplot(data=transit, x='distance', bins=np.arange(0, 801, 40), color="lightskyblue", ax=ax2)
sns.histplot(data=radial, x='distance', bins=np.arange(0, 801, 40), color="mediumorchid", ax=ax2)
# a number of bins is specifice, edges determined by API based on the data
sns.histplot(data=transit, x='distance', bins=20, color="lightskyblue", ax=ax3)
sns.histplot(data=radial, x='distance', bins=20, color="mediumorchid", ax=ax3)
我已经设法让两个直方图重叠,但如果仔细观察,条形开始倾斜并且没有完全重叠。
我已经调整了线宽和宽度,并没有改善
我的目标是让所有的条形排列在彼此的顶部,黑边没有歪斜。
关于如何解决这个问题的任何想法
这是我的代码:
import matplotlib.pyplot as plt
import numpy
True_Distance = sort_by_Distance_below_4kpc_and_retrabmag_no_99s["true distance"].tolist()
Retr_Distance = sort_by_Distance_below_4kpc_and_retrabmag_no_99s["retrieved distance from observed parallax"].tolist()
plt.figure(figsize=(8,6))
plt.hist(True_Distance, normed=True, bins = 40, alpha=0.75, color = "mediumorchid", label="True Distance", edgecolor='black', linewidth=0.1, width=200)
plt.hist(Retr_Distance, normed=True, bins = 20, alpha=0.5, color = "lightskyblue", label="Retrieved Distance", edgecolor='black', linewidth=0.1, width=200)
# Add title and axis names
plt.title('Number distribution of stars with distance')
plt.xlabel('Distance (parsecs)')
plt.ylabel('Number of stars')
plt.legend()
输出如下:
您是否为直方图和 x 轴定义了 bin 大小。您应该将两个直方图的 x 轴定义为相同,并在每个直方图中为它们提供相同数量的 bin。然后,当您绘制它们时,它们应该沿 x 轴是相同的,但不同箱子的箱子高度不同。
- 有几种方法可以处理 bin 边缘对齐
- 如果
'distance'
类别(例如'methods'
)和值以整齐的格式单独提供,seaborn.histplot
API 将正确对齐各种类别,当使用hue
参数时。- 要使用此选项,您的列必须堆叠,因此测量方法在一列中,距离在另一列中,这可以通过以下代码行完成。
df = sort_by_Distance_below_4kpc_and_retrabmag_no_99s[['true distance', 'retrieved distance from observed parallax']].stack().reset_index(level=1).rename(columns={'level_1': 'method', 0: 'distance'})
- 如评论中 JohanC 所述,如果您单独绘制数据,如 OP 中所示,则必须指定 bin 边缘。
- 如果
seaborn
is a high-level API formatplotlib
.- 此示例的数据集是从
seaborn
示例数据集导入的,并在 NASA Exoplanet Explorations 中进行了说明。与地球的距离是光年。
示例数据和导入
plants
数据集与您的星距数据集非常吻合。这里,'method'
. 有几个值
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["patch.force_edgecolor"] = True
# import some test data
df = sns.load_dataset('planets')
# display(df.head())
method number orbital_period mass distance year
0 Radial Velocity 1 269.300 7.10 77.40 2006
1 Radial Velocity 1 874.774 2.21 56.95 2008
2 Radial Velocity 1 763.000 2.60 19.84 2011
3 Radial Velocity 1 326.030 19.40 110.62 2007
4 Radial Velocity 1 516.220 10.50 119.47 2009
将所有'methods'
画在一起
- 如您所见,无论如何指定
bins
,边缘始终对齐
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(10, 10))
data = df[df.distance < 801]
sns.histplot(data=data, x='distance', hue='method', ax=ax1, bins=np.arange(0, 801, 80))
sns.histplot(data=data, x='distance', hue='method', ax=ax2, bins=20)
sns.histplot(data=data, x='distance', hue='method', ax=ax3)
Select 'method'
单独和 plot
- 只有当两组数据的边缘定义相同时,bin 边缘才对齐
ax2
。 - 使用
sns.histplot
绘图,而不使用hue
,“大部分”等同于使用plt.hist(...)
绘图- 有一些不同的默认值。例如
bins
:sns.hist
使用auto
而plt.hist
默认为 10,正如seaborn
的创建者 mwaskom 所指出的那样。
- 有一些不同的默认值。例如
# create a dataframe for two values from the method column
radial = data[data.method == 'Radial Velocity']
transit = data[data.method == 'Transit']
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(10, 10))
# number of bins and edges determined by the API
sns.histplot(data=transit, x='distance', color="lightskyblue", ax=ax1)
sns.histplot(data=radial, x='distance', color="mediumorchid", ax=ax1)
# bin edges defined the same for both plots
sns.histplot(data=transit, x='distance', bins=np.arange(0, 801, 40), color="lightskyblue", ax=ax2)
sns.histplot(data=radial, x='distance', bins=np.arange(0, 801, 40), color="mediumorchid", ax=ax2)
# a number of bins is specifice, edges determined by API based on the data
sns.histplot(data=transit, x='distance', bins=20, color="lightskyblue", ax=ax3)
sns.histplot(data=radial, x='distance', bins=20, color="mediumorchid", ax=ax3)