areuexperienced
areuexperienced

Reputation: 2071

Matplotlib linestyle inconsistent dashes

I am plotting just a simple scatterplot with MPL 1.4.0. I want to control the number of dashes on the figures I am plotting because currently even though I set a linestyle, the dashes are too close to each other so it doesn't look like a properly dashed line.

#load cdeax,cdeay,gsix,gsiy,reich all are arrays of shape (380,)
figfit = plt.figure(); axfit = figfit.gca() 

axfit.plot(cdeax,np.log(cdeay),'ko', alpha=.5); axfit.plot(gsix,np.log(gsiy), 'kx')
axfit.plot(cdeax,cdeafit,'k-'); axfit.plot(gsix,gsifit,'k:')
longevityregplot[1].plot(gsix,np.log(reich_l),'k-.')

Why I want to control dashes


#load cdeax,cdeay,gsix,gsiy,reich all are arrays of shape (380,)
figfit = plt.figure(); axfit = figfit.gca() 

axfit.plot(cdeax,np.log(cdeay),'ko', alpha=.5); axfit.plot(gsix,np.log(gsiy), 'kx')
axfit.plot(cdeax,cdeafit,'k-',dashes = [10,10]); axfit.plot(gsix,gsifit,'k:',dashes=[10,10])
longevityregplot[1].plot(gsix,np.log(reich_l),'k-.')

What I end up the 1st time

However the above is what I get. Rather than a uniformly-dashed line, the lines get dashed at the ends to varying degrees but no matter what values I use for dashes, the dashing is never uniform.

I'm afraid I really don't know what the problem is here... Any ideas?

I have pasted the arrays I am using here: http://pastebin.com/rJ5Jjfmm You should be able to just copy/paste them to your IDE for the above code to run.

Cheers!

EDIT:

Just with the single line plotted:

axfit.plot(cdeax,cdeafit,'k-',dashes = [10,10]); 

enter image description here

EDIT2: pastebin link changed to include all data

EDIT3: Histogram of point density along the x axis:

enter image description here

Upvotes: 3

Views: 2238

Answers (1)

wordsforthewise
wordsforthewise

Reputation: 15787

I think what @cphlewis said is correct, you may have some x-axis backtracking. If I sort everything it looks ok to me (did my own fitting since I still don't see the fits on pastebin)

# import your data here
import math
figfit = plt.figure(); axfit = figfit.gca() 

cdea = zip(cdeax,cdeay)
cdea = np.array(sorted(cdea, key = lambda x: x[0]))

gsi = zip(gsix,gsiy)
gsi = np.array(sorted(gsi, key = lambda x: x[0]))

cdeafit2 = np.polyfit(cdea[:,0],cdea[:,1],1)
gsifit2 = np.polyfit([x[0] for x in gsi],[math.log(x[1]) for x in gsi],1)

cdeafit = [x*cdeafit2[0] + cdeafit2[1] for x in cdea[:,0]]

gsifit = [math.exp(y) for y in [x*gsifit2[0] + gsifit2[1] for x in gsi[:,0]]]

axfit.plot(cdea[:,0],cdea[:,1],'ko', alpha=.5); axfit.plot(gsi[:,0],gsi[:,1], 'kx')
axfit.plot(cdea[:,0],cdeafit,'k-',dashes = [10,10]); axfit.plot(gsi[:,0],gsifit,'k:',dashes=[10,10])
#longevityregplot[1].plot(gsix,np.log(reich_l),'k-.') # not sure what this is
axfit.set_yscale('log')
plt.show()

demo plot

fits only

Upvotes: 3

Related Questions