2

我目前正在尝试使用以下代码在我的日志图上放置一条水平虚线。K2H_HUBp[:,1]DivR是两个 [1x6000] 数组。变量one 是一个 [ 1x6000 ] 数组,里面全是 1。

该图的重点是显示“土豆”的半径与“红薯”的比较。因此,如果它们相同,则所有数据点都应该落在这条 y = 1 线上。

plt.scatter(K2H_HUBp[:,1],DivR,s=2.5,alpha=0.15,c = '#A9A9A9')
plt.loglog(K2H_HUBp[:,1], ones, '--',dashes=(1, 1),linewidth=0.9,c='#3C323C')
plt.ylim((0.1,10))
plt.xlim((0.35,12))
ax = plt.gca()
ax.tick_params(which = 'both', direction = 'in',right='on',top='on')
ax.set_xscale('log')
ax.set_yscale('log')
plt.ylabel("Radius (Potatos/Sweet Potatos)")
plt.xlabel("Radius (Potatos)")

我希望那些线在情节中同样虚线。我在此处获取此图表时遇到的问题是线条的间距不相等。

我正在寻找与这个非常相似的图(是的,这是一个线性图,我正在使用对数对数图)

我试过修改 dashes() 参数,但没有成功。

提前感谢您的指导。:)

4

2 回答 2

1

您可以使用其他loglog-plot 或使用标准绘制它plot。此代码是否为您提供了您所追求的东西?

import numpy as np
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2, 1)

x = np.linspace(0.01, 10, 100)
y = x**5

ax1.loglog(x, y, '.')
ax1.plot([x[0], x[-1]], [y[0], y[-1]], '--', label='with plot')
ax1.legend()

ax2.loglog(x, y, '.')
ax2.loglog([x[0], x[-1]], [y[0], y[-1]], '--', label='with loglog')
ax2.legend()

fig.show()
# plt.show()

在此处输入图像描述

于 2017-07-27T08:48:37.630 回答
0

So it turns out Pyplot has a nifty function called hlines. This function just draws a horizontal line using the following arguments:

matplotlib.pyplot.hlines(y, xmin, xmax, colors='k', linestyles='solid', label='', hold=None, data=None, **kwargs)

In my case i've now completely removed the code:

plt.loglog(K2H_HUBp[:,1], ones, '--',dashes=(1, 1),linewidth=0.9,c='#3C323C')

and have replaced it with:

plt.hlines(1, 0.001, 20, linestyles='dashed',linewidth=0.9,colors='#3C323C')

plotting a y = 1 line from x 0.001 to x 20. This then gives me my desired result being this graph.

Thanks for all your guidance and I hope this helps someone else in the future!

于 2017-07-28T01:55:15.087 回答