Reputation: 53806
Using below code I'm attempting to plot x axis values as strings with matplotlib :
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
fig = plt.figure()
plt.figure(figsize=(20,10))
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.plot( 'x', 'y', data=pd.DataFrame({'x': np.array(['a' , 'b' , 'c']) , 'y': np.array([1,2,3]) }), marker=None, color='blue')
plt.show()
This error is returned :
ValueError: could not convert string to float: 'c'
It appears matplotlib is inferring the axis value as string. How to change this ?
I've tried setting the type but this does not fix :
astype(str)
Upvotes: 0
Views: 1043
Reputation: 1762
I can reproduce the error in python2
with matplotlib==1.5.3
.
Upgrading matplotlib
to newest can solve this problem:
pip3 install matplotlib --upgrade
Because the newest matplotlib
doesn't support 2.x, you can upgrade to matplotlib==2.1
:
pip2 install matplotlib==2.1
Try this:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.figure(figsize=(20,10))
plt.yticks(fontsize=24)
x = np.array(['a' , 'b' , 'c'])
xn = range(len(x))
plt.xticks(xn, x, fontsize=24)
plt.plot(xn, 'y', data=pd.DataFrame({'y': np.array([1,2,3]) }), marker=None, color='blue')
plt.show()
Upvotes: 1