Skip to content
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

Aligning axvline and axhline with increasing linewidth #3731

Closed
djrobust opened this issue Oct 28, 2014 · 4 comments
Closed

Aligning axvline and axhline with increasing linewidth #3731

djrobust opened this issue Oct 28, 2014 · 4 comments
Labels
status: needs clarification Issues that need more information to resolve.

Comments

@djrobust
Copy link

I would like to draw a vertical line at x=0.5, and I want it to be visible, so I use

ax.axvline(x=0.5, linewidth=2)

However, it seems that the line gets fatter asymmetrically, expanding only to the right. If I set linewidth=20 this becomes even more visible.

How can I align the line to remain centered around x=0.5, even with increasing width?

Is there an argument in axvline/axhline equivalent to ax.bar's align='center'?

@tacaswell
Copy link
Member

What backend are you using? I can not reproduce this:

(I made the ticks huge via ax.tick_params(width=5, length=15, color='k'))

so

@tacaswell tacaswell added this to the unassigned milestone Oct 28, 2014
@tacaswell tacaswell added the status: needs clarification Issues that need more information to resolve. label Oct 28, 2014
@djrobust
Copy link
Author

Okay I found out what the issue was. Using an IPython Notebook I had a graph like this

#!/usr/bin/python3
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

np.random.seed(42)

plt.rcParams.update({'font.size': 23, 'font.family': 'serif'})
f, ax = plt.subplots(1, 1, figsize=(16,10), dpi=600)

data = np.random.rand(50)
hist, bins = np.histogram(data, bins=99, range=(0.0, 1.0))

midpoints, hist = [0.5, 0.25]
ax.bar(midpoints, hist, align='center', width=0.9*(bins[1]-bins[0]),
           color='green')
ax.axis([0, 1, 0, 0.35])
ax.grid(True)
ax.axvline(x=0.5, linewidth=2, alpha=0.5)

index

The vertical line is not centered on my screen - because the bar and vertical line width are displayed using 7 and 2 pixels respectively. I then increased the scale of the graphic in my IPython Notebook using the bottom right edge, but the graphic did not rerender (why?) and hence the imbalance remained even after rescaling.

In that sense if we increase the linewidth from 1 to 2 pixels, we get an imbalance. I cannot reproduce the originally reported issue of 'expanding only to the right'.

In sum, this is probably not an issue with matplotlib, but just a property that comes from using pixels to display stuff.

@tacaswell
Copy link
Member

There is also a snap property to artists, turning that off might help.

@djrobust
Copy link
Author

Yes, snap=False fixed the issue. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs clarification Issues that need more information to resolve.
Projects
None yet
Development

No branches or pull requests

2 participants