AttributeError: 'tuple' object has no attribute python-altair

Question:

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

chicago = {'Year': [2022, 2023, 2024, 2025, 2026, 2022, 2023, 2024, 2025, 2026],
        'Level': ['Low', 'Low', 'Low', 'Low', 'Low', 'Medium','Medium','Medium','Medium','Medium'],
        'Leads': [1795, 3590, 5388, 7184, 8980, 2154,4308,6462,8616,10770],
        'Gross Profit': [131475, 262950, 394425, 525900, 657375, 94845,189690,284535,379380,474225]}

df3 = pd.DataFrame(chicago)
df3

My Attempt:

base = alt.Chart(df3).encode(
x=alt.X('Year:O', title= 'Year',axis=alt.Axis(labelAngle=325))
)
#Add line
line = base.mark_line(color='#55F546').encode(
y=alt.Y('Gross Profit',title='Gross Profit', axis=alt.Axis(grid=True)),
strokeDash ='Level',
)
#Add background
bar = base.mark_bar(color='red').encode(
y='Leads',
color = alt.Color('Level', scale=alt.Scale(scheme = 'set1')),
)

#configure graph (size & colors)
de = (bar + line ).resolve_scale(y='independent').properties(title= '$100 Vs $120'),
d= de.configure_title(fontSize=14).configure(background='#888888')
d.configure_axisLeft(
labelColor='white',
titleColor='white',
labelFontSize=15,
titleFontSize=15
).configure_axisRight(
labelColor='#55F546',
titleColor='#55F546',
labelFontSize=15,
titleFontSize=15
).configure_axisBottom(
labelColor='white',
titleColor='white',
labelFontSize=13,
titleFontSize=13
).configure_legend(
labelColor='white',
titleColor='white',
labelFontSize=16,
titleFontSize=16
)
Asked By: Leonardo Urbiola

||

Answers:

I think the error is that each merged graph is stored in a tuple format and no title can be set for it, so the error can be resolved by following the merging settings.

base = alt.Chart(df3).encode(
    x=alt.X('Year:O', title= 'Year',axis=alt.Axis(labelAngle=325))
)
#Add line
line = base.mark_line(color='#55F546').encode(
    y=alt.Y('Gross Profit',title='Gross Profit', axis=alt.Axis(grid=True)),
    strokeDash ='Level',
)
#Add background
bar = base.mark_bar(color='red').encode(
    y='Leads',
    color = alt.Color('Level', scale=alt.Scale(scheme = 'set1')),
)
(bar + line).resolve_scale(
    y='independent'
).properties(
    title='$100 Vs $120'
).configure_title(
    fontSize=14
).configure(
    background='#888888'
).configure_axisLeft(
    labelColor='white',
    titleColor='white',
    labelFontSize=15,
    titleFontSize=15
).configure_axisRight(
    labelColor='#55F546',
    titleColor='#55F546',
    labelFontSize=15,
    titleFontSize=15
).configure_axisBottom(
    labelColor='white',
    titleColor='white',
    labelFontSize=13,
    titleFontSize=13
).configure_legend(
    labelColor='white',
    titleColor='white',
    labelFontSize=16,
    titleFontSize=16
)

enter image description here

Answered By: r-beginners

This is the problematic line:

de = (bar + line ).resolve_scale(y='independent').properties(title= '$100 Vs $120'),

You need to remove the trailing comma, because in Python a trailing comma indicates a tuple. For example:

>>> x = 1
>>> type(x)
int
>>> x = 1,
>>> type(x)
tuple
Answered By: jakevdp
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.