Matplotlib:我们可以在同一个图表上绘制直方图和箱形图吗?

时间:2012-08-26 04:35:32

标签: python matplotlib visualization histogram boxplot

我有一个关于绘制直方图和箱形图Matplotlib的问题。

我知道我可以单独绘制直方图和箱形图。我的问题是,是否可以在同一个图表上绘制它们,例如本网站显示的图表? Springer Images

非常感谢!

4 个答案:

答案 0 :(得分:2)

使用matplotlib可以通过多种方法实现这一目标。 plt.subplots()方法以及AxesGrid1gridspec工具包都提供了非常优雅的解决方案,但可能需要一些时间来学习。

一种简单,强力的方法是自己手动将轴对象添加到图形中。

import numpy as np
import matplotlib.pyplot as plt

# fake data
x = np.random.lognormal(mean=2.25, sigma=0.75, size=37)

# setup the figure and axes
fig = plt.figure(figsize=(6,4))
bpAx = fig.add_axes([0.2, 0.7, 0.7, 0.2])   # left, bottom, width, height:
                                            # (adjust as necessary)
histAx = fig.add_axes([0.2, 0.2, 0.7, 0.5]) # left specs should match and
                                            # bottom + height on this line should
                                            # equal bottom on bpAx line
# plot stuff
bp = bpAx.boxplot(x, notch=True, vert=False)
h = histAx.hist(x, bins=7)

# confirm that the axes line up 
xlims = np.array([bpAx.get_xlim(), histAx.get_xlim()])
for ax in [bpAx, histAx]:
    ax.set_xlim([xlims.min(), xlims.max()])

bpAx.set_xticklabels([])  # clear out overlapping xlabels
bpAx.set_yticks([])  # don't need that 1 tick mark
plt.show()

答案 1 :(得分:1)

这里有一个通用的解决方案,它是一个免费的包含17个matplotlib图形实用程序的库+用户指南:https://www.mlbridgeresearch.com/products/free-article-2。我已经厌倦了中断研究以编写实用程序软件的工作,因此我积累了满足通用需求的库。该代码有充分的文档证明,并且运行良好。这是一个例子。

from sklearn.datasets import load_iris

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

from statistics_utilities import histogram
from statistics_utilities import box_plot_basic

data = load_iris()
# iris, using the label column as a categorical
df = pd.DataFrame(data.data, columns=data.feature_names)
df['label'] = data.target

print(data.feature_names)

# setup the plot grid
plt.style.use('seaborn-darkgrid')
fig, ax = plt.subplots(1, 2)
ax = np.reshape(ax, (1, 2))

variable_name = 'sepal length (cm)'

# Place the histogram on the grid - pass the Axes.
# Plots a single histogram for a quantitative variable using seaborn's distplot().
# See also histogram_grid(), which plots a grid of histograms for a list of
# quantitative variables
hist_type = 'frequency'
# displays summary statistics in a custom legend, set legend=False to turn off.
ax[0, 0] = histogram(df, variable_name=variable_name, bins=20, kde=False, statistics='all',
                     hist_type=hist_type, title=None, ax=ax[0, 0])

# Place the box plot on the grid - pass the Axes
# Plots a single box_plot for a quantitative variable using matplotlib's boxplot().
# See also box_plot() and box_plot_groupby, which plots a quantitative variable
# by one or two categorical variables.
box_orientation = 'vertical'
box_width = .2
ax[0, 1] = box_plot_basic(df, variable_name=variable_name,
                          box_orientation=box_orientation, box_width=box_width, title=None,
                          ax=ax[0, 1])

# adjustments to plot size and spacing
fig.set_size_inches(13, 6)
fig.subplots_adjust(wspace=.55, left=0.035, right=.985, top=.925, bottom=.1)
fig.suptitle('iris dataset', fontsize=13)

plt.show()
plt.close()

hist+box

答案 2 :(得分:0)

我正在寻找类似的东西,这对我有用。图片链接在https://raw.githubusercontent.com/preetihemant/preetihemant.github.io/master/images/hist_boxplot.png

plt.subplot(2,1,1)
plt.hist(data,histtype='bar',bins=[values]) 
plt.xlabel('x-label')
plt.ylabel('y-label')
plt.title('Graph title')

plt.subplot(2,1,2)
plt.boxplot(values)

plt.show()

答案 3 :(得分:0)

是的,here是我所见过的处理此问题的最佳方法。 代码和图形的副本:

# Import library and dataset
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')
 
# Cut the window in 2 parts
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)})
 
# Add a graph in each part
sns.boxplot(df["sepal_length"], ax=ax_box)
sns.distplot(df["sepal_length"], ax=ax_hist)
 
# Remove x axis name for the boxplot
ax_box.set(xlabel='')

enter image description here