Skip to content

Commit 9e72d38

Browse files
committed
Add tests for grouped_bar()
1 parent 6ffec53 commit 9e72d38

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
8.84 KB
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,90 @@ 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+
2160+
2161+
@check_figures_equal(extensions=["png"])
2162+
def test_grouped_bar_list_of_datasets(fig_test, fig_ref):
2163+
categories = ['A', 'B']
2164+
data1 = [1, 1.2]
2165+
data2 = [2, 2.4]
2166+
data3 = [3, 3.6]
2167+
2168+
ax = fig_test.subplots()
2169+
ax.grouped_bar([data1, data2, data3], tick_labels=categories,
2170+
labels=["data1", "data2", "data3"])
2171+
ax.legend()
2172+
2173+
ax = fig_ref.subplots()
2174+
label_pos = np.array([0, 1])
2175+
bar_width = 1 / (3 + 1.5) # 3 bars + 1.5 group_spacing
2176+
data_shift = -1 * bar_width + np.array([0, bar_width, 2 * bar_width])
2177+
ax.bar(label_pos + data_shift[0], data1, width=bar_width, label="data1")
2178+
ax.bar(label_pos + data_shift[1], data2, width=bar_width, label="data2")
2179+
ax.bar(label_pos + data_shift[2], data3, width=bar_width, label="data3")
2180+
ax.set_xticks(label_pos, categories)
2181+
ax.legend()
2182+
2183+
2184+
@check_figures_equal(extensions=["png"])
2185+
def test_grouped_bar_dict_of_datasets(fig_test, fig_ref):
2186+
categories = ['A', 'B']
2187+
data_dict = dict(data1=[1, 1.2], data2=[2, 2.4], data3=[3, 3.6])
2188+
2189+
ax = fig_test.subplots()
2190+
ax.grouped_bar(data_dict, tick_labels=categories)
2191+
ax.legend()
2192+
2193+
ax = fig_ref.subplots()
2194+
ax.grouped_bar(data_dict.values(), tick_labels=categories, labels=data_dict.keys())
2195+
ax.legend()
2196+
2197+
2198+
@check_figures_equal(extensions=["png"])
2199+
def test_grouped_bar_array(fig_test, fig_ref):
2200+
categories = ['A', 'B']
2201+
array = np.array([[1, 2, 3], [1.2, 2.4, 3.6]])
2202+
labels = ['data1', 'data2', 'data3']
2203+
2204+
ax = fig_test.subplots()
2205+
ax.grouped_bar(array, tick_labels=categories, labels=labels)
2206+
ax.legend()
2207+
2208+
ax = fig_ref.subplots()
2209+
list_of_datasets = [column for column in array.T]
2210+
ax.grouped_bar(list_of_datasets, tick_labels=categories, labels=labels)
2211+
ax.legend()
2212+
2213+
2214+
@check_figures_equal(extensions=["png"])
2215+
def test_grouped_bar_dataframe(fig_test, fig_ref, pd):
2216+
categories = ['A', 'B']
2217+
labels = ['data1', 'data2', 'data3']
2218+
df = pd.DataFrame([[1, 2, 3], [1.2, 2.4, 3.6]],
2219+
index=categories, columns=labels)
2220+
2221+
ax = fig_test.subplots()
2222+
ax.grouped_bar(df)
2223+
ax.legend()
2224+
2225+
ax = fig_ref.subplots()
2226+
list_of_datasets = [df[col].to_numpy() for col in df.columns]
2227+
ax.grouped_bar(list_of_datasets, tick_labels=categories, labels=labels)
2228+
ax.legend()
2229+
2230+
21472231
def test_boxplot_dates_pandas(pd):
21482232
# smoke test for boxplot and dates in pandas
21492233
data = np.random.rand(5, 2)

0 commit comments

Comments
 (0)