条形图中偶数x刻度间距

梅西耶51

我正在尝试使用matplotlib使我的x刻度均匀分布。

这是问题图;代码在下面在此处输入图片说明

问题:尽管我已尽力而为,但x轴的条形和值的间距不是均匀的

我将不胜感激,谢谢!

这是我正在尝试绘制的代码

    # define and store student IDs
student_IDs = np.array([1453,1454,1456,1457,1459,1460,1462,1463,1464,1465,1466, 1467, 1468, 1469,1470])

before_IP_abs_scores = np.array([51,56,73,94,81,83,71,36,43,83,66,62,70,50,83])
after_IP_abs_scores = np.array([65,60,82,71,65,85,78,51,34,80,63,63,62,55,77])
change_IP_abs_scores = after_IP_abs_scores - before_IP_abs_scores

这是我从此阵列中存储贵重物品的方式

ip_sc = collections.OrderedDict()

for ii in student_IDs:
  ip_sc[ii]  = []
for count, key in enumerate(student_IDs):
  sci_id[key] = [before_science_ID_abs_scores[count],after_science_ID_abs_scores[count],change_science_ID_abs_scores[count]]
  ip_sc[key]  = [before_IP_abs_scores[count],after_IP_abs_scores[count],change_IP_abs_scores[count]]

这是我的绘图代码:

fig = plt.figure(4)
fig.set_figheight(18)
fig.set_figwidth(18)

ax = plt.subplot(111)
plt.grid(True)
# ax = fig.add_axes([0,0,1,1])


for ii in student_IDs:
  # plt.plot([1,2], ip_sc[ii][:-1],label=r'${}$'.format(ii))
  ax.bar(ii, ip_sc[ii][0], width=.5, color='#30524F',edgecolor="white",hatch="//",align='center')
  ax.bar(ii, ip_sc[ii][1], width=.5, color='#95BC89',align='center')
  ax.bar(ii, ip_sc[ii][2], width=.5, color='#4D8178',align='center')
  
plt.ylabel('Absolute Score',size=30)
plt.xlabel('Student ID',size=30)
plt.title('IP Scale Scores',size=30)
plt.axhspan(0, 40, facecolor='navy', alpha=0.2,)
plt.axhspan(40, 60, facecolor='#95BC89', alpha=0.2)
plt.axhspan(60, 100, facecolor='seagreen', alpha=0.3)
ax.tick_params(axis='x', which='major', labelsize=16)
ax.tick_params(axis='y', which='major', labelsize=16)
plt.xticks(student_IDs, ['1453','1454','1456','1457','1459', '1460', '1462', '1463', '1464','1465', '1466', '1467', '1468', '1469', '1470'])
# ax.set_yticks(np.arange(0, 81, 10))
plt.ylim(-25,100)
ax.legend(labels=["Intense and Frequent IP ","Moderate IP ","few IP ",'Pre', 'Post','Change'],fontsize=15)
plt.show()
约翰·C

学生编号不是连续编号的。

除了将student_ids用作x值之外,您还可以将其索引用作x。有了set_xticklabels你之后可以设定对应于这些位置student_ids的标签。

因此,您可以对代码进行以下修改(省去对的调用plt.xticks):

for ind, stud_id in enumerate(student_IDs):
    ax.bar(ind, ip_sc[stud_id][0], width=.5, color='#30524F', edgecolor="white", hatch="//", align='center')
    ax.bar(ind, ip_sc[stud_id][1], width=.5, color='#95BC89', align='center')
    ax.bar(ind, ip_sc[stud_id][2], width=.5, color='#4D8178', align='center')

ax.set_xticks(range(len(student_IDs)))
ax.set_xticklabels(student_IDs)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

d3仅在带有条形图中数据的刻度线上显示标签

条形图中条形之间的特定间距-ggplot2-R

在Material Google条形图中更改X轴刻度

条形图中的颜色

matplotlib:在条形图中更改x网格的时间戳格式

MATLAB中条形图中的等间距

如何在Python(Pandas或matplotlib)的条形图中移动对数轴刻度位置(对于10)

刻度标签在熊猫条形图中重叠

如何获得条形图中条形的X和Y的起点

无法在Seaborn条形图中使用'x'和'+'标记

在Google条形图中自定义hAxis刻度线颜色

如何解决 d3在条形图中X轴上的刻度线数?

如何在条形图中定义附加条的间距?

如何在刻度线之间对齐条形图中的条形(matplotlib)?

C3.js-在条形图中的X轴和图例之间添加间距

带有对数刻度轴的ggplot条形图中条的方向

R Plotly:如何在水平反转的条形图中更改刻度文本的位置?

R:调整条形图刻度之间的间距

x轴在熊猫条形图中的订购月份

R:条形图中的x轴-条形图中间的x轴和中心刻度线的宽度

如何在条形图中获取条形的x坐标

在条形图中使用对数刻度

在Highcharts.js条形图中控制条之间的间距和高度

如何控制堆积条形图中x轴标签位置的间距?

在条形图中的特定 x 轴值处放置条形

如何使用 Plotly 删除堆叠和分组条形图中的 x 轴刻度标签

更改条形图中的 x 轴刻度大小

更改刻度名称后,如何修复 seaborn 条形图中丢失的条形?

如何更改facet-wrapt堆积条形图中的x轴刻度--ggplot R