Issue
I would like to plot a bar chart using pyplot where I want to display a series of values. On the x axis, I would like to have a range of the length of the y values which are requested from a pandas pivot table (the pivot table is displayed well and works fine). This is the code I've written to do it:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
...
pvt = pd.pivot_table(df, columns='movexkod', aggfunc='sum', values='EC_diff')
pvt = pvt.transpose()
ind = pvt.index.to_numpy()
x = np.arange(len(ind))
y = pvt.values
fig, ax = plt.subplots()
plt.bar(x, y)
ax.set_xticklabels(ind)
This code seems to work just fine when I change the plotting to plt.plot(x, y)
, but when I try to plot a bar plot, I get the following error message: TypeError: only size-1 arrays can be converted to Python scalars
.
Solution
Sheldore's answer was a step in the right direction, however, I recognized that the problem was that the arrays were nested: instead of having an array of values, those values were one-length arrays themselves, so I wrote some basic code the solve the problem, and now the plot is displayed:
x = np.arange(len(ind))
y = []
[ y.append(value[0]) for value in pvt.values]
fig, ax = plt.subplots()
plt.bar(x, y)
ax.set_xticklabels(ind)
Answered By - Szelmat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.