更改网格间隔并在Matplotlib中指定刻度标签 [英] Change grid interval and specify tick labels in Matplotlib
问题描述
-
以5为间隔点缀网格
-
只有每20个主要的刻度标签
-
我希望刻度在图之外。
$ b -
在这些网格中有计数
这是我的代码。 p>
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator,FormatStrFormatter
for key ,sort(data.items())中的值:
x = value [0] [2]
y = value [0] [3]
count = value [0] [4]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.annotate(count,xy =(x,y),size = 5)
#覆盖我只得到最后一个数据点
plt.close()
#没有这个,我得到未能分配位图错误
plt.suptitle( 'count count',fontsize = 12)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.axes()。set_aspect('equal')
plt.axis([0,1000,0,1000])
#这个间隔为200
majorLocator = MultipleLocator(20)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(5)
#我希望小网格为5,主网格为20
plt.grid()
filename ='C:\Users\Owl\Desktop\Plot.png'
plt.savefig(文件名,dpi = 150)
plt.close()
这就是我得到的。
我也有问题覆盖数据点,我也遇到了麻烦...任何人都可以请帮助我是否有问题?
您的代码有几个问题。 >首先是大的:
-
您在循环的每次迭代中创建一个新图形和一个新坐标轴→
把fig = plt.figure
和ax = fig.add_subplot(1,1,1)
外部循环。 -
不要使用定位器。使用正确的关键字调用函数
ax.set_xticks()
和ax.grid()
。 -
使用
plt.axes()
,您可以再次创建新的轴。使用ax.set_aspect('equal')
。
次要的东西:
您不应该将类似MATLAB的语法(如 plt.axis()
)与目标语法混合使用。
使用 ax.set_xlim(a,b)
和 ax.set_ylim(a,b)
这应该是一个最小的例子:
import numpy as np
pre>
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#Major每20个小刻度,每5个小元刻度b $ b major_ticks = np.arange(0,101,20)
minor_ticks = np.arange(0,101,5)
ax。 set_xticks(major_ticks)
ax.set_xticks(minor_ticks,minor = True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks,minor = True)
#和一个相应的网格
ax.grid(which ='both')
#或者如果你想为网格设置不同的设置:
ax.grid(which ='minor ',alpha = 0.2)
ax.grid(which ='major',alpha = 0.5)
plt.show()
输出是这样的:
I am trying to plot counts in gridded plots, but I am not being able to figure out how I go about it. I want to:
Have dotted grids at an interval of 5
Have major tick labels only every 20
I want the ticks to be outside the plot.
Have "counts" inside those grids
I have checked for potential duplicates such as here and here, but I have not been able to figure it out.
This is my code.
import matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator, FormatStrFormatter for key, value in sorted(data.items()): x = value[0][2] y = value[0][3] count = value[0][4] fig = plt.figure() ax = fig.add_subplot(111) ax.annotate(count, xy = (x, y), size = 5) # Overwrites and I only get the last data point plt.close() # Without this, I get "fail to allocate bitmap" error plt.suptitle('Number of counts', fontsize = 12) ax.set_xlabel('x') ax.set_ylabel('y') plt.axes().set_aspect('equal') plt.axis([0, 1000, 0, 1000]) # This gives an interval of 200 majorLocator = MultipleLocator(20) majorFormatter = FormatStrFormatter('%d') minorLocator = MultipleLocator(5) # I want minor grid to be 5 and major grid to be 20 plt.grid() filename = 'C:\Users\Owl\Desktop\Plot.png' plt.savefig(filename, dpi = 150) plt.close()
This is what I get.
I also have a problem of overwriting the data points, which I am also having trouble with... Could anybody PLEASE help me with this problem?
解决方案There are several problems in your code.
First the big ones:
You are creating a new figure and a new axes in every iteration of your loop → put
fig = plt.figure
andax = fig.add_subplot(1,1,1)
outside of the loop.Don't use the Locators. Call the functions
ax.set_xticks()
andax.grid()
with the correct keywords.With
plt.axes()
you are creating a new axes again. Useax.set_aspect('equal')
.The minor things: You should not mix the MATLAB-like syntax like
plt.axis()
with the objective syntax. Useax.set_xlim(a,b)
andax.set_ylim(a,b)
This should be a working minimal example:
import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) # Major ticks every 20, minor ticks every 5 major_ticks = np.arange(0, 101, 20) minor_ticks = np.arange(0, 101, 5) ax.set_xticks(major_ticks) ax.set_xticks(minor_ticks, minor=True) ax.set_yticks(major_ticks) ax.set_yticks(minor_ticks, minor=True) # And a corresponding grid ax.grid(which='both') # Or if you want different settings for the grids: ax.grid(which='minor', alpha=0.2) ax.grid(which='major', alpha=0.5) plt.show()
Output is this:
这篇关于更改网格间隔并在Matplotlib中指定刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!