0

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)
2
  • I don't get any error using this script Commented Oct 26, 2018 at 8:29
  • No errors here either, python 2 or 3. Could you describe in more detail what you want the plot to look like? And also provide more of the stack trace -- is it plt.plot() that returns the error? Commented Oct 26, 2018 at 9:17

1 Answer 1

1

I can reproduce the error in python2 with matplotlib==1.5.3.


If you are using python3

Upgrading matplotlib to newest can solve this problem:

pip3 install matplotlib --upgrade

If you are using python2

Because the newest matplotlib doesn't support 2.x, you can upgrade to matplotlib==2.1:

pip2 install matplotlib==2.1

If you don't want to upgrade

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.