This picture is an example of what I have up until this point. 我将值存储在一些代表每个条形的列表中。而且,我想在同一张图表中显示具有不同图案或颜色的组成。有人可以帮我这个忙,或者给我任何可以帮助的链接吗?谢谢!
import matplotlib.pyplot as plt
def plotFigure(challenged_list, noeffect_list, reinforced_list,challenged_extra_list,challenged_agree_list,
challenged_cons_list,challenged_neuro_list,challenged_open_list):
labels = ['Topic 1', 'Topic 2', 'Topic 3', 'Topic 4', 'Topic 5', 'Topic 6', 'Topic 7', 'Topic 8', 'Topic 9',
'Topic 10', 'Topic 11', 'Topic 12', 'Topic 13',
'Topic 14', 'Topic 15', 'Topic 16', 'Topic 17', 'Topic 18', 'Topic 19', 'Topic 20', 'Topic 21',
'Topic 22', 'Topic 23', 'Topic 24', 'Topic 25', 'Topic 26',
'Topic 27']
x = np.arange(len(labels))
width = 0.30
fig, ax = plt.subplots()
bar1 = ax.bar(x - width / 2, challenged_list, width, label='Challenged')
bar2 = ax.bar(x + width / 2, noeffect_list, width, label='No Effect')
bar3 = ax.bar(x + 3 * width / 2, reinforced_list, width, label='Reinforced')
ax.set_ylabel('Personality Traits Count')
ax.set_title('All In One')
ax.set_xticks(x)
ax.tick_params(axis='both', which='major', pad=15)
plt.xticks(rotation=60)
ax.set_xticklabels(labels)
ax.legend()
plt.show()
答案 0 :(得分:0)
我不确定完全您要问的是什么,但是我想您是在问将条形图中的模式用于不同数据集的问题吗?如果是这样,那么在使用cycler
之前,我已经做了类似的事情。使用循环仪,您可以在遍历大量数据集时一起组合不同的属性(颜色,线条粗细,不透明度,阴影(图案)等)。下面是一个简单的示例……有点混乱,但是我使用了5个数据集来说明图案的工作原理。
编辑:版本2现在可以选择堆叠条形图
#@ title Version 2
import matplotlib.pyplot as plt
import numpy as np
from cycler import cycler
from matplotlib.patches import Patch
import random
def patterned_bars_v2(N, num_sets=3, stacked=True, **prop_kwargs):
make_data = lambda n: [random.randint(1, 10) for _ in range(n)]
data = [make_data(N) for _ in range(num_sets)]
colors = ['red', 'green', 'blue', 'orange']
# These patterns can be made more coarse or fine. For more fine, just increase
# the number of identifiers. E.g. '/' -> coarse, '///' -> fine
patterns = ['//', '++', '**', 'xx']
color_cycler = cycler(color=colors)
# Patterns are specified using the "hatch" property
pattern_cycler = cycler(hatch=patterns)
# 'Multiplication' of cyclers results in the 'outer product' of the two
prop_cycler = pattern_cycler * color_cycler
fig, ax = plt.subplots(figsize=(20, 10))
width = 0.5
xmax = (width * num_sets * N)* 1.1
legend_handles = []
bottom = np.zeros(N)
for i, (data, props) in enumerate(zip(data, prop_cycler)):
# Add passed in kwargs to props
props.update(prop_kwargs)
if stacked:
xvals = np.arange(N)
ax.bar(xvals, data, width=width, bottom=bottom, **props)
bottom += data
else:
xvals = np.linspace(width * i, xmax + (width * i), N)
ax.bar(xvals, data, width=width, **props)
# Make patches for legend
legend_handles.append(
Patch(**props, label=f'Dataset {i}')
)
# Add legend
leg = ax.legend(handles=legend_handles,
bbox_to_anchor=(1, 1),
labelspacing=1.8,
bbox_transform=ax.transAxes)
# make legend patches a little bit bigger
for patch in leg.get_patches():
patch.set_height(22)
patch.set_y(-10)
patterned_bars_v2(10, num_sets=10, stacked=True, alpha=0.6)