I am trying to create a graph using altair in Python and I am getting the error code “AttributeError: ‘tuple’ object has no attribute ‘configure_title” I have tried but search for similar problems but it seems like there not many resources.
I have created a graph similar to what I want to accomplish but for some reason I am getting this problem.
This is my data
JavaScript
x
8
1
chicago = {'Year': [2022, 2023, 2024, 2025, 2026, 2022, 2023, 2024, 2025, 2026],
2
'Level': ['Low', 'Low', 'Low', 'Low', 'Low', 'Medium','Medium','Medium','Medium','Medium'],
3
'Leads': [1795, 3590, 5388, 7184, 8980, 2154,4308,6462,8616,10770],
4
'Gross Profit': [131475, 262950, 394425, 525900, 657375, 94845,189690,284535,379380,474225]}
5
6
df3 = pd.DataFrame(chicago)
7
df3
8
My Attempt:
JavaScript
1
39
39
1
base = alt.Chart(df3).encode(
2
x=alt.X('Year:O', title= 'Year',axis=alt.Axis(labelAngle=325))
3
)
4
#Add line
5
line = base.mark_line(color='#55F546').encode(
6
y=alt.Y('Gross Profit',title='Gross Profit', axis=alt.Axis(grid=True)),
7
strokeDash ='Level',
8
)
9
#Add background
10
bar = base.mark_bar(color='red').encode(
11
y='Leads',
12
color = alt.Color('Level', scale=alt.Scale(scheme = 'set1')),
13
)
14
15
#configure graph (size & colors)
16
de = (bar + line ).resolve_scale(y='independent').properties(title= '$100 Vs $120'),
17
d= de.configure_title(fontSize=14).configure(background='#888888')
18
d.configure_axisLeft(
19
labelColor='white',
20
titleColor='white',
21
labelFontSize=15,
22
titleFontSize=15
23
).configure_axisRight(
24
labelColor='#55F546',
25
titleColor='#55F546',
26
labelFontSize=15,
27
titleFontSize=15
28
).configure_axisBottom(
29
labelColor='white',
30
titleColor='white',
31
labelFontSize=13,
32
titleFontSize=13
33
).configure_legend(
34
labelColor='white',
35
titleColor='white',
36
labelFontSize=16,
37
titleFontSize=16
38
)
39
Advertisement
Answer
This is the problematic line:
JavaScript
1
2
1
de = (bar + line ).resolve_scale(y='independent').properties(title= '$100 Vs $120'),
2
You need to remove the trailing comma, because in Python a trailing comma indicates a tuple. For example:
JavaScript
1
7
1
>>> x = 1
2
>>> type(x)
3
int
4
>>> x = 1,
5
>>> type(x)
6
tuple
7