Habil Ganbarli
Habil Ganbarli

Reputation: 223

Python Bar string on x axis

I have 2 arrays like below:

x = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November',
 'December']
y = [5.3, 6.1, 25.5, 27.8, 31.2, 33.0, 33.0, 32.8, 28.4, 21.1, 17.5, 11.9]

and i need to put the months on x axis and max temperatures for months on y axis. However, when I use the code below:

    import matplotlib.pyplot as plt
import numpy as np

    x = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November',
         'December']
    y = [5.3, 6.1, 25.5, 27.8, 31.2, 33.0, 33.0, 32.8, 28.4, 21.1, 17.5, 11.9]

    plt.bar(x, y, color='green',align='center')
    plt.title('Max Temperature for Monthes')
    plt.legend()
    plt.show()

I get this value error: ValueError: could not convert string to float: 'January'

How can i solve this? How can i put string values on x axis?

Upvotes: 2

Views: 13302

Answers (4)

sacha-cs
sacha-cs

Reputation: 71

You can use plt.xticks as documented here

Here's how to use it with your code:

import matplotlib.pyplot as plt
import numpy as np

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
monthsRange = np.arange(len(months))
temperatures = [5.3, 6.1, 25.5, 27.8, 31.2, 33.0, 33.0, 32.8, 28.4, 21.1, 17.5, 11.9]

plt.bar(monthsRange, temperatures, color='green')
plt.title('Max Temperature for Monthes')
plt.xticks(monthsRange, months)
plt.legend()
plt.show()

Upvotes: 2

finbarr
finbarr

Reputation: 759

Matplotlib has an example that accomplishes what you're trying to do.

To create a plot, you have to have numerical data for both dimensions as otherwise, matplotlib doesn't know what to do. Instead, you have to add the strings as labels to your ticks:

import matplotlib.pyplot as plt
import numpy as np

x = range(12)
x_labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November',
     'December']
y = [5.3, 6.1, 25.5, 27.8, 31.2, 33.0, 33.0, 32.8, 28.4, 21.1, 17.5, 11.9]

plt.bar(x, y, color='green', align='center')
plt.title('Max Temperature for Monthes')
plt.xticks(x, x_labels, rotation='vertical')
plt.legend()
plt.show()

Upvotes: 5

Marlon Abeykoon
Marlon Abeykoon

Reputation: 12465

plt.bar(range(len(x)), y, color='green',align='center')
plt.xticks(range(len(x)), x)
plt.xticks(rotation=45)

Use xticks. Refer this

Upvotes: 1

datawrestler
datawrestler

Reputation: 1567

You need to represent the labels as numbers then set the labels separately. Finally, you need to rotate the labels otherwise they will all be stacked on top of one another.

import matplotlib.pyplot as plt
import numpy as np



labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November',
     'December']

x = list(range(12))
y = [5.3, 6.1, 25.5, 27.8, 31.2, 33.0, 33.0, 32.8, 28.4, 21.1, 17.5, 11.9]

plt.bar(x, y, color='green',align='center')
plt.title('Max Temperature for Monthes')
plt.xticks(x, labels)
plt.xticks(rotation=70)
plt.legend()


plt.show()

Upvotes: 1

Related Questions