无法使用matplotlib设置书脊线样式 [英] Cannot set spine line style with matplotlib

查看:178
本文介绍了无法使用matplotlib设置书脊线样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置matplotlib绘图刺的线条样式,但是由于某些原因,它不起作用.我可以将它们设置为不可见或使其更细,但不能更改线条样式.

I try to set the line style of matplotlib plot spines, but for some reason, it does not work. I can set them invisible or make them thinner, but I cannot change the line style.

我的目的是提出一个图块,将其分成两部分,以在顶部显示异常值.我想将各自的底部/顶部棘刺设置为虚线,以便它们清楚地表明有休息.

My aim is to present one plot cut up into two to show outliers at the top. I would like to set the respective bottom/top spines to dotted so they clearly show that there is a break.

import numpy as np
import matplotlib.pyplot as plt

# Break ratio of the bottom/top plots respectively
ybreaks = [.25, .9]

figure, (ax1, ax2) = plt.subplots(
    nrows=2, ncols=1,
    sharex=True, figsize=(22, 10),
    gridspec_kw = {'height_ratios':[1 - ybreaks[1], ybreaks[0]]}
)

d = np.random.random(100)

ax1.plot(d)
ax2.plot(d)

# Set the y axis limits
ori_ylim = ax1.get_ylim()
ax1.set_ylim(ori_ylim[1] * ybreaks[1], ori_ylim[1])
ax2.set_ylim(ori_ylim[0], ori_ylim[1] * ybreaks[0]) 

# Spine formatting
# ax1.spines['bottom'].set_visible(False)  # This works
ax1.spines['bottom'].set_linewidth(.25)  # This works
ax1.spines['bottom'].set_linestyle('dashed')  # This does not work

ax2.spines['top'].set_linestyle('-')  # Does not work
ax2.spines['top'].set_linewidth(.25)  # Works

plt.subplots_adjust(hspace=0.05)

我希望上面的代码能绘制出顶部图的底部脊线,并绘制出底部图的顶部脊线.

I would expect the above code to draw the top plot's bottom spine and the bottom plot's top spine dashed.

我想念什么?

推荐答案

第一个应该提到,如果不更改线宽,则虚线样式会很好.

First one should mention that if you do not change the linewidth, the dashed style shows fine.

ax1.spines['bottom'].set_linestyle("dashed")

但是间距可能太紧了.这是由于默认情况下将刺的capstyle设置为"projecting"的缘故.

However the spacing may be a bit too tight. This is due to the capstyle being set to "projecting" for spines by default.

因此,可以将capstyle设置为"butt"(这也是绘图中法线的默认设置)

One can hence set the capstyle to "butt" instead (which is also the default for normal lines in plots),

ax1.spines['bottom'].set_linestyle('dashed')
ax1.spines['bottom'].set_capstyle("butt")

或者,可以将破折​​号进一步分开.例如

Or, one can separate the dashes further. E.g.

ax1.spines['bottom'].set_linestyle((0,(4,4)))

现在,如果您还将线宽设置为较小,那么您将需要成比例地增加间距.例如

Now, if you also set the linewidth so something smaller, then you would need proportionally more spacing. E.g.

ax1.spines['bottom'].set_linewidth(.2)  
ax1.spines['bottom'].set_linestyle((0,(16,16))) 

请注意,由于使用了抗锯齿功能,该行实际上在屏幕上并不会变细.它只是洗掉了,所以颜色变浅了.因此总的来说,将线宽保持在0.72点(100dpi时为0.72点= 1像素)可能有意义,而将颜色改为浅灰色.

Note that the line does not actually become thinner on screen due to the antialiasing in use. It just washes out, such that it becomes lighter in color. So in total it may make sense to keep the lineswidth at some 0.72 points (0.72 points = 1 pixel at 100 dpi) and change the color to light gray instead.

这篇关于无法使用matplotlib设置书脊线样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆