简体   繁体   English

如何通过在 matplotlib 中的 x 轴上重复字符串来制作 plot?

[英]how to make a plot by repeating strings on x-axis in matplotlib?

this is a bar graph.这是一个条形图。 As you can see 'Real time' is twice.如您所见,“实时”是两倍。 But while plotting it shows only once.但是在绘制时它只显示一次。 I need to be as it is in the proj list.我需要像项目列表中那样。 Please help.请帮忙。

import numpy as np
import matplotlib.pyplot as plt

proj=['Frank','GiftU','GiftUK','Int','Mon','Speed','PP','Real Time','Nan','Real Time','Swift']
# proj_x = [i for i in range(len(proj))]
prVal=[2.0,2.0,2.0,5.0,1.0,1.0,3.0,4.0,1.0,6.0,2.0]
fig = plt.figure(figsize=(10, 5))

# creating the bar plot 
plt.bar(proj, prVal, color='maroon',
        width=0.4)

plt.xlabel("gifts")
plt.ylabel("Value")
plt.title("Gift recieved")
plt.show()

You had the right idea with the line you commented.您对评论的行有正确的想法。 Create a dummy x-coordinates for each bar, then replace the labels with the content of proj :为每个条创建一个虚拟 x 坐标,然后用proj的内容替换标签:

proj=['Frank','GiftU','GiftUK','Int','Mon','Speed','PP','Real Time','Nan','Real Time','Swift']
proj_x = range(len(proj))
prVal=[2.0,2.0,2.0,5.0,1.0,1.0,3.0,4.0,1.0,6.0,2.0]

# creating the bar plot 
fig = plt.figure(figsize=(10, 5))
plt.bar(proj_x, prVal, color='maroon',        
        width=0.4)
plt.xticks(proj_x, proj)
plt.xlabel("gifts")
plt.ylabel("Value")
plt.title("Gift recieved")
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM