python - 如何在 Python 中创建具有不同线型的主要和次要网格线

标签 python matplotlib

我目前正在使用 matplotlib.pyplot 来创建图形,并希望主要网格线为实心和黑色,次要网格线为灰色或虚线。

在网格属性中,which=both/major/mine,然后color和linestyle都是由linestyle简单定义的。有没有办法只指定次要线型?

到目前为止,我拥有的适当代码是

plt.plot(current, counts, 'rd', markersize=8)
plt.yscale('log')
plt.grid(b=True, which='both', color='0.65', linestyle='-')

最佳答案

其实很简单,只要分别设置majorminor:

In [9]: plot([23, 456, 676, 89, 906, 34, 2345])
Out[9]: [<matplotlib.lines.Line2D at 0x6112f90>]

In [10]: yscale('log')

In [11]: grid(b=True, which='major', color='b', linestyle='-')

In [12]: grid(b=True, which='minor', color='r', linestyle='--')

带有小网格的问题是您还必须打开小刻度线。在上面的代码中,这是由 yscale('log') 完成的,但也可以通过 plt.minorticks_on() 完成。

enter image description here

关于python - 如何在 Python 中创建具有不同线型的主要和次要网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9127434/

相关文章:

python - 在 Pycharm : How to turn off interactive mode? 中调试时使用 Matplotlib

python - 使用 matplotlib fill_between 在两条极坐标曲线之间填充

python - 如何更改 $$ 中的字体,它是 python 中的数学形式

python - Python 中的正则表达式用于检测整数、 float 或科学记数法中的数字

python - 根据两个元素对元组列表进行排序

python - elasticsearch-dsl-py 中的 GeoPoint 字段类型

python - 在相角图中使用没有视觉失真的循环色彩图?

python - 我如何在 Seaborn 的同一个 catplot 上显示种类 ="swarm"和种类 ="point"?

python - 如何用 Pandas 数据框中的 ASCII 替换非 ASCII

python - 使用 scikit learn 训练逻辑回归以进行多类分类