I am creating a bar chart using matplotlib. To populate the bar information I was originally using numpy.linspace to create a range of values. I am now trying to populate the bar information with an array of information taken from a mySQLdb.
My issue is that the numpy.linspace output arrays seems to be different than the numpy.array created from a select query and I don't quite understand what is happening.
Here is my original code:
theta = np.linspace(0.0, 2 * np.pi, 20, endpoint=False) #array of indexes for each bar
radii = max_height*np.random.rand(20) #array of heights/radii for each bar
width = (8*np.pi) / 20 #width of each bar
ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width, bottom=bottom) #setting each bar value
I am trying to replace the theta variable from above with this code below:
sql = "SELECT theta FROM table LIMIT 20"
cursor.execute(sql)
thetaArray = cursor.fetchall()
thetaSQL = np.array(thetaArray)
When I attempt to use the thetaSQL array I get the following error:
TypeError: only size-1 arrays can be converted to Python scalars
Printing out both arrays returns two slightly different formats:
thetaSQL:
[[0. ]
[0.00314159]
[0.00628319]
[0.00942478]
[0.0125664 ]
[0.015708 ]
[0.0188496 ]
[0.0219911 ]
[0.0251327 ]
[0.0282743 ]]
linspace theta:
[0. 0.62831853 1.25663706 1.88495559 2.51327412 3.14159265
3.76991118 4.39822972 5.02654825 5.65486678]
How can I make this work with the sql data?
As mentioned by @hpaulj in comments, your options are:
thetaSQL = np.array(thetaArray).ravel()
thetaSQL = np.array(thetaArray).squeeze()
thetaSQL = np.array(thetaArray).reshape(-1)
thetaSQL = np.array(thetaArray)[:,0]
They all convert the 2-D array into 1-D array.
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.