当前位置: 首页 > 知识库问答 >
问题:

如何并排绘制具有相同X坐标的条形图(“躲开”)

尹乐邦
2023-03-14
import matplotlib.pyplot as plt

gridnumber = range(1,4)

b1 = plt.bar(gridnumber, [0.2, 0.3, 0.1], width=0.4,
                label="Bar 1", align="center")

b2 = plt.bar(gridnumber, [0.3, 0.2, 0.2], color="red", width=0.4,
                label="Bar 2", align="center")


plt.ylim([0,0.5])
plt.xlim([0,4])
plt.xticks(gridnumber)
plt.legend()
plt.show()

目前,b1和b2相互重叠。如何像这样分别绘制它们:

共有3个答案

蒋烨然
2023-03-14

有时很难找到合适的条形宽度。我一般用这个np.diff来找合适的维度。

import numpy as np
import matplotlib.pyplot as plt

#The data
womenMeans = (25, 32, 34, 20, 25)
menMeans = (20, 35, 30, 35, 27)
indices = [5.5,6,7,8.5,8.9]
#Calculate optimal width
width = np.min(np.diff(indices))/3


fig = plt.figure()
ax = fig.add_subplot(111)
# matplotlib 3.0 you have to use align
ax.bar(indices-width,womenMeans,width,color='b',label='-Ymin',align='edge')
ax.bar(indices,menMeans,width,color='r',label='Ymax',align='edge')


ax.set_xlabel('Test histogram')
plt.show()
# matplotlib 2.0 (you could avoid using align)
# ax.bar(indices-width,womenMeans,width,color='b',label='-Ymin')
# ax.bar(indices,menMeans,width,color='r',label='Ymax')

这就是结果:

#
import numpy as np
import matplotlib.pyplot as plt

# The data
womenMeans = (25, 32, 34, 20, 25)
menMeans = (20, 35, 30, 35, 27)
indices = range(len(womenMeans))
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min(np.diff(indices))/3.

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(indices-width/2.,womenMeans,width,color='b',label='-Ymin')
ax.bar(indices+width/2.,menMeans,width,color='r',label='Ymax')
#tiks = ax.get_xticks().tolist()
ax.axes.set_xticklabels(names)
ax.set_xlabel('Test histogram')
plt.show()
长孙淳
2023-03-14

以下答案将以最简单的方式解释每行代码:

# Numbers of pairs of bars you want
N = 3

# Data on X-axis

# Specify the values of blue bars (height)
blue_bar = (23, 25, 17)
# Specify the values of orange bars (height)
orange_bar = (19, 18, 14)

# Position of bars on x-axis
ind = np.arange(N)

# Figure size
plt.figure(figsize=(10,5))

# Width of a bar 
width = 0.3       

# Plotting
plt.bar(ind, blue_bar , width, label='Blue bar label')
plt.bar(ind + width, orange_bar, width, label='Orange bar label')

plt.xlabel('Here goes x-axis label')
plt.ylabel('Here goes y-axis label')
plt.title('Here goes title of the plot')

# xticks()
# First argument - A list of positions at which ticks should be placed
# Second argument -  A list of labels to place at the given locations
plt.xticks(ind + width / 2, ('Xtick1', 'Xtick3', 'Xtick3'))

# Finding the best position for legends and putting it
plt.legend(loc='best')
plt.show()
澹台浩广
2023-03-14

matplotlib站点中有一个示例。基本上,只需将x值移动宽度。以下是相关信息:

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27)
menStd =   (2, 3, 4, 1, 2)

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='royalblue', yerr=menStd)

womenMeans = (25, 32, 34, 20, 25)
womenStd =   (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='seagreen', yerr=womenStd)

# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

plt.show()
 类似资料:
  • 我有一个直方图,每个面元上有4个不同的对象,它们现在相互堆叠在一起。相反,我需要在同一个直方图框中并排绘制不同的对象(类似于https://matplotlib . org/3 . 1 . 1/gallery/statistics/histogram _ multi hist . html中的左上图): ,等在同一个样本内绘制不同标准的对象(和)。我看的所有示例都与我需要的略有不同,找不到正确的方

  • 现在有两个列表值,如下所示: 和x标签: 我想绘制基于相同x标签的不同颜色的lst1和lst2条。 我尝试的代码: 但有错误: attribute error:“none type”对象没有属性“seq”

  • 我试图画一系列给定坐标的矩形,这只是一个x和y值的列表。 这是我的密码: 此代码应绘制类似以下内容的形状: 相反,它画的东西没有连贯的形状。

  • 我有一个名为pct的数据集 我想使用ggplot2或绘图包获得正条形图和负条形图。 给定下表中的数据集 我想得到这样的东西

  • 我想为三类数据绘制一个“双”条形图,并显示基于Wilcox检验的两个“双”条形图的显著性水平。 运行下面的代码,我没有看到反映在y轴上的计数,相反,所有柱都处于相同的高度。 我试图制作的情节看起来像这样:https://drive.google.com/open?id=1EndiF-sCtXFyUOAPIToRY5hqjp97b1Px 感谢您的帮助:1.编辑我的代码,使其显示实际计数值2.为绘图添