开发者

proper names for the constant values while using matplotlib,

开发者 https://www.devze.com 2023-03-29 14:22 出处:网络
Being a newbie to using matplotlib ,I was trying out some code ,from examples I found on the net.I was using a lot of constants to adjust the dimensions of the plot elements.I found that I am at a los

Being a newbie to using matplotlib ,I was trying out some code ,from examples I found on the net.I was using a lot of constants to adjust the dimensions of the plot elements.I found that I am at a loss ,when it came to naming the constants properly.

for example when creating a bar chart from a pair of x_list and y_list

to scale the figure width

DIVISION_CONSTANT = 4
width_scale = len(y_list)/DIVISION_CONSTANT
size_of_figure = (8*width_scale,6)
figure = pylab.figure(figsize = size_of_figure)
...

to restrict the ylimit and the number of yticks

ANOTHER_DIVISION_CONSTANT = 10
max_y = max(ylist)
step = max_y/ANOTHER_DIVISION_CONSTANT
...
ax.set_yticks(range(0,max_y+ystep,ystep))
ax.set_ylim(0,max_y+ystep)

and while setting the distance of xlimits from the origin

DISTANCE_FROM_ORIGIN = .5
ax.set_xlim([min(xlist) - DISTANCE_FROM_ORIGIN, max(xlist) + DISTANCE_FROM_ORIGIN])
...

Can someone give better names for these constants?I couldn't find the technical names for them in any book or tutorial..and trying to think up some on my own seems silly..

Also,when the axes.bar() function is called ,

ax.bar(xlist, ylist, width=BAR_WIDTH,align='center',color='yellow')

I tried to give several values for BAR_WIDTH (.5,1,1.5..) and found that .5 causes a bar to occupy half the space between 2 xticks

A value of 1 causes all bars to touch each other

1.5 causes bars to overlap.

So,what is the meaning of this numerical value? does it represent some percent开发者_开发知识库age of space between two xticks?


First, I find that plotting figures is generally a messy business and often my code just does not look nice. It has become better over the years but in the end, to get a production-grade plot there will always be hand tweaking. And this will include many constants which probably don't have 'proper' names, like the ones you use above. As always, just try to give them useful names. So I'd call them

DIVISION_CONSTANT --> width_scale
width_scale --> fig_width
ANOTHER_DIVISION_CONSTANT  --> step_scale
DISTANCE_FROM_ORIGIN --> x_limits

but that is just a matter of taste, not right or wrong. (Don't use uppercase but that was presumably just used here for presentation purposes).

Then, the width parameter of the plt.bar plot is in the units of your xlist not percent.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号