【问题标题】:How to plot secondary_y in log scale in pyplot如何在pyplot中以对数比例绘制secondary_y
【发布时间】:2016-11-30 01:19:56
【问题描述】:

我想在一个图中有两条线(或更好的散点图)。

辅助 Y 线应采用对数刻度。用python matplotlib怎么做?

【问题讨论】:

  • ax2.set_yscale('log') ?

标签: matplotlib


【解决方案1】:

您可以使用ax2 = ax.twinx() 创建第二个 y 轴。然后,正如 tacaswell 在 cmets 中指出的那样,您可以将第二个轴设置为对数刻度。

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(5,3))
ax = fig.add_subplot(111)
ax2 = ax.twinx()

x = np.random.rand(10)
y = np.random.rand(10)
y2 = np.random.randint(1,10000, size=10)

l1 = ax.scatter(x,y, c="b", label="lin")
l2 = ax2.scatter(x,y2,  c="r", label="log")

ax2.set_yscale("log")
ax2.legend(handles=[l1, l2])

ax.set_ylabel("Linear axis")
ax2.set_ylabel("Logarithmic axis")
plt.tight_layout()

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多