Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ruthpozuelo opened this issue Oct 20, 2022 · 6 comments
Closed

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

ruthpozuelo opened this issue Oct 20, 2022 · 6 comments

Comments

@ruthpozuelo
Copy link

ruthpozuelo commented Oct 20, 2022

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

@suuuehgi
Copy link

This exact code gives me
plot
using 'grey' although I think to recall seeing that once as well within Jupyter Lab ... but I think it was just related to the inline display, not the actual output of .savefig().

@ruthpozuelo
Copy link
Author

I am using Jupyter Lab and I am indeed looking at the inline display. Got it!

@timhoffm
Copy link
Member

I'm sorry, I cannot follow your description.

A wild guess is that you use labelcolor='#77', which is not a valid color name. This is only checked at draw time and can result in drawing some parts of the figure and then raising an exception.

If that's not it, please clean up your original description

  • Use inline code formatting (i.e. backticks) to mark code fragments, .e.g. labelcolor='grey'.
  • Be precise on what you write, sometimes you write "label color", sometimes you write "labelcolor".
  • This works / this does not work contains exactly the same code
  • The code for reproduction misses qoutes on the labelcolor value and is thus not executable.

@ruthpozuelo
Copy link
Author

Hi @timhoffm ,
It is github that trims the text, it says # 00 00 00 00 77 in reality, same with labelcolor= 'r'.
I think the issue is what @suuuehgi describes, I am using jupiter labs and the issue shows only on the inline display. When I moved the code to power bi it worked.

@QuLogic
Copy link
Member

QuLogic commented Oct 21, 2022

When writing code in Markdown, you should surround it with backticks. I have inserted some where I think they should go, but I am not 100% certain, so please edit again and correct any that are wrong.

@ruthpozuelo
Copy link
Author

Done @QuLogic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants