删除网格线,但保留框架(matplotlib 中的 ggplot2 样式) [英] Remove grid lines, but keep frame (ggplot2 style in matplotlib)
问题描述
使用 Matplotlib 我想删除图中的网格线,同时保留框架(即轴线).我已经尝试了下面的代码以及其他选项,但是无法正常工作.如何在删除网格线的同时简单地保留框架?
我这样做是为了在matplotlib中重现ggplot2图.我在下面创建了一个 MWE.请注意,您需要使用相对较新的matplotlib版本才能使用ggplot2样式.
import matplotlibmatplotlib.use('Agg')导入matplotlib.pyplot作为plt将pylab导入为P将numpy导入为np如果 __name__ == '__main__':值 = np.random.uniform(size=20)plt.style.use('ggplot')无花果= plt.figure()_, ax1 = P.subplots()权重 = np.ones_like(values)/len(values)plt.hist(values,bins = 20,weights = weights)ax1.set_xlabel('Value')ax1.set_ylabel('概率')ax1.grid(b=假)#ax1.yaxis.grid(False)#ax1.xaxis.grid(False)ax1.set_axis_bgcolor('white')ax1.set_xlim([0,1])P.savefig('hist.pdf', bbox_inches='tight')
好吧,我想这就是您要问的(但如果我误解了请纠正我):
您需要更改
您可以看到
如果你真的想让垃圾箱在 values.min()
和 values.max()
之间移动,你需要改变你的情节限制不再是0和1:
n,bins,patches = plt.hist(values, bins=20, weights=weights)ax.set_xlim(bins [0],bins [-1])
Using Matplotlib I'd like to remove the grid lines inside the plot, while keeping the frame (i.e. the axes lines). I've tried the code below and other options as well, but I can't get it to work. How do I simply keep the frame while removing the grid lines?
I'm doing this to reproduce a ggplot2 plot in matplotlib. I've created a MWE below. Be aware that you need a relatively new version of matplotlib to use the ggplot2 style.
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pylab as P
import numpy as np
if __name__ == '__main__':
values = np.random.uniform(size=20)
plt.style.use('ggplot')
fig = plt.figure()
_, ax1 = P.subplots()
weights = np.ones_like(values)/len(values)
plt.hist(values, bins=20, weights=weights)
ax1.set_xlabel('Value')
ax1.set_ylabel('Probability')
ax1.grid(b=False)
#ax1.yaxis.grid(False)
#ax1.xaxis.grid(False)
ax1.set_axis_bgcolor('white')
ax1.set_xlim([0,1])
P.savefig('hist.pdf', bbox_inches='tight')
OK, I think this is what you are asking (but correct me if I misunderstood):
You need to change the colour of the spines
. You need to do this for each spine
individually, using the set_color
method:
for spine in ['left','right','top','bottom']:
ax1.spines[spine].set_color('k')
You can see this example and this example for more about using spines
.
However, if you have removed the grey background and the grid lines, and added the spines, this is not really in the ggplot
style any more; is that really the style you want to use?
EDIT
To make the edge of the histogram bars touch the frame, you need to either:
Change your binning, so the bin edges go to 0 and 1
n,bins,patches = plt.hist(values, bins=np.linspace(0,1,21), weights=weights) # Check, by printing bins: print bins[0], bins[-1] # 0.0, 1.0
If you really want to keep the bins to go between
values.min()
andvalues.max()
, you would need to change your plot limits to no longer be 0 and 1:n,bins,patches = plt.hist(values, bins=20, weights=weights) ax.set_xlim(bins[0],bins[-1])
这篇关于删除网格线,但保留框架(matplotlib 中的 ggplot2 样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!