简体   繁体   中英

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 :

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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