[英]In python, how can I fill up each bar in a bar chart with its constituent data?
这张照片是我到目前为止所拥有的一个例子。 我将值存储在一些代表每个条形图组成的列表中。 而且,我想在同一张图表中展示具有不同模式或 colors 的组合。 有人可以帮我解决这个问题或给我任何可以帮助的链接吗? 谢谢!
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()
我不确定你在问什么,但我想你是在问关于将模式放入不同数据集的条形图中? 如果是这样,那么我在使用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.