Reputation: 797
dictionary to plot.bar
with two data-set and one dict.keys
show x-axis
with keys A,B,C,D,E,F,I,J,K,L,M,N
map with sorting values 1,2,3,4,5,6,7,8,9,10,11,12
, the example data sorting like this N 1
, B 2
", C 3
, D 4
, E 5
, I 6
, F 7
, J 8
, K 9
, L 10
, M 11
, A 12
as Expected bar chart
data-set
x = {'A': ['12', '100'],
'B': ['2', '101'],
'C': ['3', '102'],
'D': ['4', '103'],
'E': ['5', '104'],
'F': ['7', '105'],
'I': ['6', '106'],
'J': ['8', '107'],
'K': ['9', '108'],
'L': ['10', '109'],
'M': ['11', '110'],
'N': ['1', '111']}
Tried Python Code: (with error)
import pandas as pd
import matplotlib.pyplot as plot
# Dictionary loaded into a DataFrame
dataFrame = pd.DataFrame(x);
# Draw a vertical bar chart
dataFrame.plot.bar(rot=15, title="first record in bar second record in x-axis");
plot.show(block=True);
Upvotes: 1
Views: 512
Reputation: 24322
That's what error says the data is string so typecast it to int/float by astype()
method:
df=pd.DataFrame(x)
df=df.T.astype(int)
#If nan's are present in your dataset then typecast it to float
#df=df.T.astype(float)
#Since you only need to plot the second record so:
df[1].plot(kind='bar',title="first record in bar second record in x-axis")
OR
If you want like this:
df=pd.DataFrame(x)
df=df.T.astype(int)
df=df.set_index(0,append=True)
df=df.sort_index(level=1)
df.plot(kind='bar',title="first record in bar second record in x-axis",rot=20)
plt.legend(['second record'])
Upvotes: 1
Reputation: 865
If you don't need the sorting values in the tickers than the following works:
x = {'A': ['12', '100'],
'B': ['2', '101'],
'C': ['3', '102'],
'D': ['4', '103'],
'E': ['5', '104'],
'F': ['7', '105'],
'I': ['6', '106'],
'J': ['8', '107'],
'K': ['9', '108'],
'L': ['10', '109'],
'M': ['11', '110'],
'N': ['1', '111']}
dataFrame = pd.DataFrame(x).T
dataFrame.columns = ['level', 'value']
dataFrame = dataFrame.astype('int')
dataFrame.sort_values('level', inplace=True)
dataFrame['value'].plot.bar(title="first record in bar second record in x-axis")
Upvotes: 0