Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何用条形图中的组成数据填充每个条形图?_Python_Matplotlib_Bar Chart - Fatal编程技术网

在python中,如何用条形图中的组成数据填充每个条形图?

在python中,如何用条形图中的组成数据填充每个条形图?,python,matplotlib,bar-chart,Python,Matplotlib,Bar Chart,我在一些列表中存储了一些值,这些值表示每个条的组成。我想在同一张图表中展示不同图案或颜色的构图。有人能帮我吗,或者给我任何能帮我的链接?谢谢大家! import matplotlib.pyplot as plt def plotFigure(challenged_list, noeffect_list, reinforced_list,challenged_extra_list,challenged_agree_list, challenged_cons_list,cha

我在一些列表中存储了一些值,这些值表示每个条的组成。我想在同一张图表中展示不同图案或颜色的构图。有人能帮我吗,或者给我任何能帮我的链接?谢谢大家!

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)

我不太清楚你在问什么,但我想你是在问如何在条形图中为不同的数据集添加模式?如果是这样的话,那么在使用
循环器之前我已经做过类似的事情。使用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)

感谢您的解决方案。但是我需要在单个条(每个条)中显示这些模式。假设像您一样,每个条有9个数据集要显示。我想显示每个条内每个数据集的体积。简言之,9个图案构成一个条形。可能吗?完全可能!我编辑了该函数,以便可以堆叠钢筋。这更符合你的要求吗?您可能会发现,如果您有足够的数据集,那么
道具的不同组合的数量将用完。如果发生这种情况,只需向循环器添加更多颜色和图案。假设第一个条表示数据集a。其高度为50,基于X的计数。在该条中,我有数据集0-9。它们基于Y的计数,计数范围为0-1000。我如何将这些数字转换成百分比,将条形图的高度设为100%,并将分布可视化(就像您在这里叠加它们一样)?我感谢你的帮助,很抱歉我不能很好地解释。谢谢你的解决方案。但是我需要在单个条(每个条)中显示这些模式。假设像您一样,每个条有9个数据集要显示。我想显示每个条内每个数据集的体积。简言之,9个图案构成一个条形。可能吗?完全可能!我编辑了该函数,以便可以堆叠钢筋。这更符合你的要求吗?您可能会发现,如果您有足够的数据集,那么
道具的不同组合的数量将用完。如果发生这种情况,只需向循环器添加更多颜色和图案。假设第一个条表示数据集a。其高度为50,基于X的计数。在该条中,我有数据集0-9。它们基于Y的计数,计数范围为0-1000。我如何将这些数字转换成百分比,将条形图的高度设为100%,并将分布可视化(就像您在这里叠加它们一样)?我感谢你的帮助,很抱歉我不能很好地解释。