带有单个数据点的 Matplotlib 分组条形图
Matplotlib grouped bar chart with individual data points
我正在尝试使用 Matplotlib 在 分组 条形图中显示单个数据点。我试着用散点图来做,我发现了一个相关的 Whosebug 主题:。但是它只提供了常规条形图的解决方案,而不是分组条形图。
这是我生成没有误差线的分组条形图的代码:
# Create a list for on_target, ntc, on_target_error, and ntc_error
on_target = [df_subset['primer_pair_1_on_target'][36], df_subset['primer_pair_2_on_target'][36], df_subset['primer_pair_3_on_target'][36], df_subset['primer_pair_4_on_target'][36], df_subset['primer_pair_5_on_target'][36], df_subset['primer_pair_6_on_target'][36], df_subset['primer_pair_7_on_target'][36], df_subset['primer_pair_8_on_target'][36], df_subset['primer_pair_9_on_target'][36]]
ntc = [df_subset['primer_pair_1_NTC'][36], df_subset['primer_pair_2_NTC'][36], df_subset['primer_pair_3_NTC'][36], df_subset['primer_pair_4_NTC'][36], df_subset['primer_pair_5_NTC'][36], df_subset['primer_pair_6_NTC'][36], df_subset['primer_pair_7_NTC'][36], df_subset['primer_pair_8_NTC'][36], df_subset['primer_pair_9_NTC'][36]]
on_target_error = [df_subset['primer_pair_1_on_target_error'][36], df_subset['primer_pair_2_on_target_error'][36], df_subset['primer_pair_3_on_target_error'][36], df_subset['primer_pair_4_on_target_error'][36], df_subset['primer_pair_5_on_target_error'][36], df_subset['primer_pair_6_on_target_error'][36], df_subset['primer_pair_7_on_target_error'][36], df_subset['primer_pair_8_on_target_error'][36], df_subset['primer_pair_9_on_target_error'][36]]
ntc_error = [df_subset['primer_pair_1_NTC_error'][36], df_subset['primer_pair_2_NTC_error'][36], df_subset['primer_pair_3_NTC_error'][36], df_subset['primer_pair_4_NTC_error'][36], df_subset['primer_pair_5_NTC_error'][36], df_subset['primer_pair_6_NTC_error'][36], df_subset['primer_pair_7_NTC_error'][36], df_subset['primer_pair_8_NTC_error'][36], df_subset['primer_pair_9_NTC_error'][36]]
# Create a variable with the x locations for the primer pairs
index1 = ["F1R1", "F2R1", "F3R1", "F1R2", "F2R2", "F3R2", "F1R3", "F2R3", "F3R3"]
ind = np.arange(len(on_target))
# Style, axis and title
plt.style.use('classic')
fig, axis = plt.subplots()
plt.ylabel("RFUs")
axes = plt.gca()
axes.set_ylim([0,3000000])
axis.set_xticks(ind)
axis.set_xticklabels(index1)
axis.yaxis.grid(True)
axis.set_axisbelow(True)
plt.title('Primer Screen')
# Layout, and bar width
fig.tight_layout()
width = 0.35 # the width of the bars
# Create on_target and ntc_bar
on_target_bar = axis.bar(ind - width/2, on_target, width, yerr=on_target_error,
label='on-target', color='red')
ntc_bar = axis.bar(ind + width/2, ntc, width, yerr=ntc_error,
label='ntc', color="grey")
# Create legend
axis.legend()
# Add scientific notation
mf = mpl.ticker.ScalarFormatter(useMathText=True)
mf.set_powerlimits((-2,2))
plt.gca().yaxis.set_major_formatter(mf)
plt.show()
现在,我的分组条形图如下所示:
Primer Screen。
但是,我想包括个别数据点。
感谢您的任何建议!
我不确定您是如何生成条形图的,但是如果您可以像这样使用 seaborn (seaborn) you can combine the sns.barplot
with the sns.stripplot
:
import seaborn as sns
# Load some example data
tips = sns.load_dataset("tips")
绘制图表:
# Draw the bar chart
ax = sns.barplot(
data=tips,
x="day",
y="total_bill",
hue="sex",
alpha=0.7,
ci=None,
)
# Get the legend from just the bar chart
handles, labels = ax.get_legend_handles_labels()
# Draw the stripplot
sns.stripplot(
data=tips,
x="day",
y="total_bill",
hue="sex",
dodge=True,
edgecolor="black",
linewidth=.75,
ax=ax,
)
# Remove the old legend
ax.legend_.remove()
# Add just the bar chart legend back
ax.legend(
handles,
labels,
loc=7,
bbox_to_anchor=(1.25, .5),
)
产生:
默认情况下,条形图绘制数据的平均值。
我正在尝试使用 Matplotlib 在 分组 条形图中显示单个数据点。我试着用散点图来做,我发现了一个相关的 Whosebug 主题:
这是我生成没有误差线的分组条形图的代码:
# Create a list for on_target, ntc, on_target_error, and ntc_error
on_target = [df_subset['primer_pair_1_on_target'][36], df_subset['primer_pair_2_on_target'][36], df_subset['primer_pair_3_on_target'][36], df_subset['primer_pair_4_on_target'][36], df_subset['primer_pair_5_on_target'][36], df_subset['primer_pair_6_on_target'][36], df_subset['primer_pair_7_on_target'][36], df_subset['primer_pair_8_on_target'][36], df_subset['primer_pair_9_on_target'][36]]
ntc = [df_subset['primer_pair_1_NTC'][36], df_subset['primer_pair_2_NTC'][36], df_subset['primer_pair_3_NTC'][36], df_subset['primer_pair_4_NTC'][36], df_subset['primer_pair_5_NTC'][36], df_subset['primer_pair_6_NTC'][36], df_subset['primer_pair_7_NTC'][36], df_subset['primer_pair_8_NTC'][36], df_subset['primer_pair_9_NTC'][36]]
on_target_error = [df_subset['primer_pair_1_on_target_error'][36], df_subset['primer_pair_2_on_target_error'][36], df_subset['primer_pair_3_on_target_error'][36], df_subset['primer_pair_4_on_target_error'][36], df_subset['primer_pair_5_on_target_error'][36], df_subset['primer_pair_6_on_target_error'][36], df_subset['primer_pair_7_on_target_error'][36], df_subset['primer_pair_8_on_target_error'][36], df_subset['primer_pair_9_on_target_error'][36]]
ntc_error = [df_subset['primer_pair_1_NTC_error'][36], df_subset['primer_pair_2_NTC_error'][36], df_subset['primer_pair_3_NTC_error'][36], df_subset['primer_pair_4_NTC_error'][36], df_subset['primer_pair_5_NTC_error'][36], df_subset['primer_pair_6_NTC_error'][36], df_subset['primer_pair_7_NTC_error'][36], df_subset['primer_pair_8_NTC_error'][36], df_subset['primer_pair_9_NTC_error'][36]]
# Create a variable with the x locations for the primer pairs
index1 = ["F1R1", "F2R1", "F3R1", "F1R2", "F2R2", "F3R2", "F1R3", "F2R3", "F3R3"]
ind = np.arange(len(on_target))
# Style, axis and title
plt.style.use('classic')
fig, axis = plt.subplots()
plt.ylabel("RFUs")
axes = plt.gca()
axes.set_ylim([0,3000000])
axis.set_xticks(ind)
axis.set_xticklabels(index1)
axis.yaxis.grid(True)
axis.set_axisbelow(True)
plt.title('Primer Screen')
# Layout, and bar width
fig.tight_layout()
width = 0.35 # the width of the bars
# Create on_target and ntc_bar
on_target_bar = axis.bar(ind - width/2, on_target, width, yerr=on_target_error,
label='on-target', color='red')
ntc_bar = axis.bar(ind + width/2, ntc, width, yerr=ntc_error,
label='ntc', color="grey")
# Create legend
axis.legend()
# Add scientific notation
mf = mpl.ticker.ScalarFormatter(useMathText=True)
mf.set_powerlimits((-2,2))
plt.gca().yaxis.set_major_formatter(mf)
plt.show()
现在,我的分组条形图如下所示: Primer Screen。 但是,我想包括个别数据点。
感谢您的任何建议!
我不确定您是如何生成条形图的,但是如果您可以像这样使用 seaborn (seaborn) you can combine the sns.barplot
with the sns.stripplot
:
import seaborn as sns
# Load some example data
tips = sns.load_dataset("tips")
绘制图表:
# Draw the bar chart
ax = sns.barplot(
data=tips,
x="day",
y="total_bill",
hue="sex",
alpha=0.7,
ci=None,
)
# Get the legend from just the bar chart
handles, labels = ax.get_legend_handles_labels()
# Draw the stripplot
sns.stripplot(
data=tips,
x="day",
y="total_bill",
hue="sex",
dodge=True,
edgecolor="black",
linewidth=.75,
ax=ax,
)
# Remove the old legend
ax.legend_.remove()
# Add just the bar chart legend back
ax.legend(
handles,
labels,
loc=7,
bbox_to_anchor=(1.25, .5),
)
产生:
默认情况下,条形图绘制数据的平均值。