Skip to content

Commit 556895d

Browse files
committed
Add tests for grouped_bar()
1 parent fea06c9 commit 556895d

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,91 @@ def test_bar_datetime_start():
21662166
assert isinstance(ax.xaxis.get_major_formatter(), mdates.AutoDateFormatter)
21672167

21682168

2169+
@image_comparison(["grouped_bar.png"], style="mpl20")
2170+
def test_grouped_bar():
2171+
data = {
2172+
'data1': [1, 2, 3],
2173+
'data2': [1.2, 2.2, 3.2],
2174+
'data3': [1.4, 2.4, 3.4],
2175+
}
2176+
2177+
fig, ax = plt.subplots()
2178+
ax.grouped_bar(data, tick_labels=['A', 'B', 'C'],
2179+
group_spacing=0.5, bar_spacing=0.1,
2180+
colors=['#1f77b4', '#58a1cf', '#abd0e6'])
2181+
ax.set_yticks([])
2182+
2183+
2184+
@check_figures_equal(extensions=["png"])
2185+
def test_grouped_bar_list_of_datasets(fig_test, fig_ref):
2186+
categories = ['A', 'B']
2187+
data1 = [1, 1.2]
2188+
data2 = [2, 2.4]
2189+
data3 = [3, 3.6]
2190+
2191+
ax = fig_test.subplots()
2192+
ax.grouped_bar([data1, data2, data3], tick_labels=categories,
2193+
labels=["data1", "data2", "data3"])
2194+
ax.legend()
2195+
2196+
ax = fig_ref.subplots()
2197+
label_pos = np.array([0, 1])
2198+
bar_width = 1 / (3 + 1.5) # 3 bars + 1.5 group_spacing
2199+
data_shift = -1 * bar_width + np.array([0, bar_width, 2 * bar_width])
2200+
ax.bar(label_pos + data_shift[0], data1, width=bar_width, label="data1")
2201+
ax.bar(label_pos + data_shift[1], data2, width=bar_width, label="data2")
2202+
ax.bar(label_pos + data_shift[2], data3, width=bar_width, label="data3")
2203+
ax.set_xticks(label_pos, categories)
2204+
ax.legend()
2205+
2206+
2207+
@check_figures_equal(extensions=["png"])
2208+
def test_grouped_bar_dict_of_datasets(fig_test, fig_ref):
2209+
categories = ['A', 'B']
2210+
data_dict = dict(data1=[1, 1.2], data2=[2, 2.4], data3=[3, 3.6])
2211+
2212+
ax = fig_test.subplots()
2213+
ax.grouped_bar(data_dict, tick_labels=categories)
2214+
ax.legend()
2215+
2216+
ax = fig_ref.subplots()
2217+
ax.grouped_bar(data_dict.values(), tick_labels=categories, labels=data_dict.keys())
2218+
ax.legend()
2219+
2220+
2221+
@check_figures_equal(extensions=["png"])
2222+
def test_grouped_bar_array(fig_test, fig_ref):
2223+
categories = ['A', 'B']
2224+
array = np.array([[1, 2, 3], [1.2, 2.4, 3.6]])
2225+
labels = ['data1', 'data2', 'data3']
2226+
2227+
ax = fig_test.subplots()
2228+
ax.grouped_bar(array, tick_labels=categories, labels=labels)
2229+
ax.legend()
2230+
2231+
ax = fig_ref.subplots()
2232+
list_of_datasets = [column for column in array.T]
2233+
ax.grouped_bar(list_of_datasets, tick_labels=categories, labels=labels)
2234+
ax.legend()
2235+
2236+
2237+
@check_figures_equal(extensions=["png"])
2238+
def test_grouped_bar_dataframe(fig_test, fig_ref, pd):
2239+
categories = ['A', 'B']
2240+
labels = ['data1', 'data2', 'data3']
2241+
df = pd.DataFrame([[1, 2, 3], [1.2, 2.4, 3.6]],
2242+
index=categories, columns=labels)
2243+
2244+
ax = fig_test.subplots()
2245+
ax.grouped_bar(df)
2246+
ax.legend()
2247+
2248+
ax = fig_ref.subplots()
2249+
list_of_datasets = [df[col].to_numpy() for col in df.columns]
2250+
ax.grouped_bar(list_of_datasets, tick_labels=categories, labels=labels)
2251+
ax.legend()
2252+
2253+
21692254
def test_boxplot_dates_pandas(pd):
21702255
# smoke test for boxplot and dates in pandas
21712256
data = np.random.rand(5, 2)

0 commit comments

Comments
 (0)