Skip to content

Commit 8963636

Browse files
committed
Add tests for grouped_bar()
1 parent 121c21d commit 8963636

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Loading

lib/matplotlib/tests/test_axes.py

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

21462146

2147+
@image_comparison(["grouped_bar.png"], style="mpl20")
2148+
def test_grouped_bar():
2149+
data = {
2150+
'data1': [1, 2, 3],
2151+
'data2': [1.2, 2.2, 3.2],
2152+
'data3': [1.4, 2.4, 3.4],
2153+
}
2154+
2155+
fig, ax = plt.subplots()
2156+
ax.grouped_bar(data, tick_labels=['A', 'B', 'C'],
2157+
group_spacing=0.5, bar_spacing=0.1,
2158+
colors=['#1f77b4', '#58a1cf', '#abd0e6'])
2159+
ax.set_yticks([])
2160+
2161+
2162+
@check_figures_equal(extensions=["png"])
2163+
def test_grouped_bar_list_of_datasets(fig_test, fig_ref):
2164+
categories = ['A', 'B']
2165+
data1 = [1, 1.2]
2166+
data2 = [2, 2.4]
2167+
data3 = [3, 3.6]
2168+
2169+
ax = fig_test.subplots()
2170+
ax.grouped_bar([data1, data2, data3], tick_labels=categories,
2171+
labels=["data1", "data2", "data3"])
2172+
ax.legend()
2173+
2174+
ax = fig_ref.subplots()
2175+
label_pos = np.array([0, 1])
2176+
bar_width = 1 / (3 + 1.5) # 3 bars + 1.5 group_spacing
2177+
data_shift = -1 * bar_width + np.array([0, bar_width, 2 * bar_width])
2178+
ax.bar(label_pos + data_shift[0], data1, width=bar_width, label="data1")
2179+
ax.bar(label_pos + data_shift[1], data2, width=bar_width, label="data2")
2180+
ax.bar(label_pos + data_shift[2], data3, width=bar_width, label="data3")
2181+
ax.set_xticks(label_pos, categories)
2182+
ax.legend()
2183+
2184+
2185+
@check_figures_equal(extensions=["png"])
2186+
def test_grouped_bar_dict_of_datasets(fig_test, fig_ref):
2187+
categories = ['A', 'B']
2188+
data_dict = dict(data1=[1, 1.2], data2=[2, 2.4], data3=[3, 3.6])
2189+
2190+
ax = fig_test.subplots()
2191+
ax.grouped_bar(data_dict, tick_labels=categories)
2192+
ax.legend()
2193+
2194+
ax = fig_ref.subplots()
2195+
ax.grouped_bar(data_dict.values(), tick_labels=categories, labels=data_dict.keys())
2196+
ax.legend()
2197+
2198+
2199+
@check_figures_equal(extensions=["png"])
2200+
def test_grouped_bar_array(fig_test, fig_ref):
2201+
categories = ['A', 'B']
2202+
array = np.array([[1, 2, 3], [1.2, 2.4, 3.6]])
2203+
labels = ['data1', 'data2', 'data3']
2204+
2205+
ax = fig_test.subplots()
2206+
ax.grouped_bar(array, tick_labels=categories, labels=labels)
2207+
ax.legend()
2208+
2209+
ax = fig_ref.subplots()
2210+
list_of_datasets = [column for column in array.T]
2211+
ax.grouped_bar(list_of_datasets, tick_labels=categories, labels=labels)
2212+
ax.legend()
2213+
2214+
2215+
@check_figures_equal(extensions=["png"])
2216+
def test_grouped_bar_dataframe(fig_test, fig_ref, pd):
2217+
categories = ['A', 'B']
2218+
labels = ['data1', 'data2', 'data3']
2219+
df = pd.DataFrame([[1, 2, 3], [1.2, 2.4, 3.6]],
2220+
index=categories, columns=labels)
2221+
2222+
ax = fig_test.subplots()
2223+
ax.grouped_bar(df)
2224+
ax.legend()
2225+
2226+
ax = fig_ref.subplots()
2227+
list_of_datasets = [df[col].to_numpy() for col in df.columns]
2228+
ax.grouped_bar(list_of_datasets, tick_labels=categories, labels=labels)
2229+
ax.legend()
2230+
2231+
21472232
def test_boxplot_dates_pandas(pd):
21482233
# smoke test for boxplot and dates in pandas
21492234
data = np.random.rand(5, 2)

0 commit comments

Comments
 (0)