Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

python - Matplotlib margins

Look at the chart below. In this chart, I want to draw to circle with the centre coordinates at (45, 0). But, as you see, bottom limit of chart is zero and half of my circle is not shown. So, I need to extend this chart to the bottom a little bit. "Margins" method of axes (ax.margins()) doesn't work, since bottom line of this chart is zero and zero multiplied by any number is equal to zero.

Note: Please, do not post replies like ax.set_ylim(...) or ax.set_yticks(...). I am looking for a general solution for this like problems.

Code
import matplotlib.pyplot as plt
import numpy as np

values = np.array([0.00388632352941, 0.00375827941176, 0.00355033823529,     0.00328273529412, 0.00294677941176, 0.00272142647059, 0.00246463235294,     0.00227766176471, 0.00213151470588, 0.00202594117647, 0.00183544117647,     0.00162102941177, 0.00148372058824, 0.00128380882353, 0.00112252941176,     0.000931544117647, 0.000786573529412, 0.000658220588235, 0.000584485294118,     0.000524044117647, 0.000562485294118, 0.000716441176471, 0.000872617647059,     0.00109039705882, 0.00124138235294, 0.00136894117647, 0.00143985294118,     0.00134760294118, 0.00121794117647, 0.00112772058824, 0.00109435294118,     0.00102432352941, 0.00101069117647, 0.00102417647059, 0.00104895588235,     0.00101776470588, 0.00101494117647, 0.000885558823529, 0.00078075,     0.000752647058824, 0.000667691176471, 0.000593220588236, 0.000658647058823,     0.000742117647059, 0.000651470588235, 0.000604647058824, 0.000584573529412,     0.00049530882353, 0.000281235294118, 0.000355029411765])
fig, ax = plt.subplots()
ax.bar(np.arange(values.shape[0]), values)
plt.show()

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the question there is no circle, so my answer doesn't include any circle either.

In order to have some space around the data in the plot, you can use ax.margins(y=ymargin), where ymargin is the percentage of space to add on each side of the data. I.e. if data goes from 0 to 1 and you add ymargin = 0.1 of margin, the ylimits will be (-0.1, 1.1). (This is independent on whether or not one limit would be zero or not.)

Now, by default this does not work for a bar plot, as it would in the general case be undesireable to have the bars start somewhere in the air, as opposed to the bottom axis. This behaviour is steered using a flag called use_sticky_edges. We can set this flag to False to get back the behaviour of margins being applied to both ends of the axis. For taking effect, we need to call ax.autoscale_view afterwards.

import matplotlib.pyplot as plt
import numpy as np

values = np.array([3.89, 3.76, 3.55, 3.28, 2.95, 2.72, 2.46, 2.28, 2.13, 2.03, 1.84, 
                   1.62, 1.48, 1.28, 1.12, 0.93, 0.79, 0.66, 0.58, 0.52, 0.56, 0.72, 
                   0.87, 1.09, 1.24, 1.37, 1.44, 1.35, 1.22, 1.13, 1.09, 1.02, 1.01, 
                   1.02, 1.05, 1.02, 1.01, 0.89, 0.78, 0.75, 0.67, 0.59, 0.66, 0.74, 
                   0.65, 0.60, 0.58, 0.50, 0.28, 0.36])

fig, ax = plt.subplots()
ax.bar(np.arange(values.shape[0]), values)

ax.margins(y=0.3)
ax.use_sticky_edges = False
ax.autoscale_view(scaley=True)
plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...