Skip to content

[Bug]: Changing color using ax.tick_params #24227

Closed
@ruthpozuelo

Description

@ruthpozuelo

Bug summary

When trying to change the color of the x and y axis I am using ax.tick_param.
If I use label color 'r' or labelcolor='#00000077', but if I write label color='grey' or a hex value: #bdbdbd then the background changes too. Is that expected?

This works:

ax.tick_params(labelsize=22,  labelcolor='#00000077', top = False,  left=False)

This doesnt:

ax.tick_params(labelsize=22,  labelcolor='#BDBDBD', top = False,  left=False)

Code for reproduction

url='https://raw.githubusercontent.com/Curbal-Data-Labs/Matplotlib-Labs/master/2018w48.csv'
dataset = pd.read_csv(url)

dataset.drop_duplicates(['city','item'], inplace=True)
dataset.pivot_table(index='city', columns='item', values='cost', aggfunc='sum', margins = True).sort_values('All', ascending=True).drop('All', axis=1)

df = dataset.pivot_table(index='city', columns='item', values='cost', aggfunc='sum', margins = True).sort_values('All', ascending=True).drop('All', axis=1).sort_values('All', ascending=False, axis=1).drop('All').reset_index()

# comma replace
for c in df.columns[1:]:
    df[c] = df[c].str.replace(',','.').astype(float)

fig, axes = plt.subplots(nrows=1, ncols=5, figsize=(30,10), sharey=True)

colors = ['green','blue','red','black','brown'] 
col_names = ['Dinner','Drinks at Dinner','2 Longdrinks','Club entry','Cinema entry']

for i, (ax,col,c) in enumerate(zip(axes.ravel(), col_names, colors)):
    ax.plot(df.loc[:,col], df['city'], marker='o', markersize=25, lw=0, color=c)
    ax.set_title(col)
    for i,j in zip(df[col], df['city']):
        ax.annotate('$'+str(i), xy=(i, j), xytext=(i-4,j), color="white", fontsize=8)
    ax.set_xticks([])
    ax.spines[['top', 'right', 'left', 'bottom']].set_visible(False)
    ax.grid(True, axis='y', linestyle='solid', linewidth=2)
    ax.grid(True, axis='x', linestyle='solid', linewidth=0.2)
    ax.xaxis.tick_top() 
    ax.xaxis.set_label_position('top')
    ax.set_xlim(xmin=0, xmax=160)
    ax.xaxis.set_major_formatter('${x:1.0f}')
    ax.tick_params(labelsize=8, labelcolor = #bdbdbd, top=False, left=False)

plt.show()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

Win11

Matplotlib Version

3.5.1

Matplotlib Backend

No response

Python version

3.10

Jupyter version

3.3.2

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions