Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib次/双轴-用圆圈和箭头标记-用于黑白(bw)出版_Python_Matplotlib_Publish_Axes_Twinx - Fatal编程技术网

Python Matplotlib次/双轴-用圆圈和箭头标记-用于黑白(bw)出版

Python Matplotlib次/双轴-用圆圈和箭头标记-用于黑白(bw)出版,python,matplotlib,publish,axes,twinx,Python,Matplotlib,Publish,Axes,Twinx,通常,两个y轴用不同的颜色分开,如下例所示 对于出版物来说,经常需要保持其可区分性,即使它是黑白印刷的 这通常是通过围绕一条线绘制圆来完成的,该线在相应轴的方向上有一个箭头 如何使用matplotlib实现这一点?或者有没有更好的方法来实现没有这些圆圈的黑白可读性 代码来源: 可以使用matplotlib绘制y轴箭头。您需要在绘图中找到箭头应该从哪里开始的点。但是,这不会在直线周围绘制圆。如果确实要绘制圆,可以使用或绘制覆盖相关区域的适当圆 import numpy as np import m

通常,两个y轴用不同的颜色分开,如下例所示

对于出版物来说,经常需要保持其可区分性,即使它是黑白印刷的

这通常是通过围绕一条线绘制圆来完成的,该线在相应轴的方向上有一个箭头

如何使用matplotlib实现这一点?或者有没有更好的方法来实现没有这些圆圈的黑白可读性

代码来源:

可以使用matplotlib绘制y轴箭头。您需要在绘图中找到箭头应该从哪里开始的点。但是,这不会在直线周围绘制圆。如果确实要绘制圆,可以使用或绘制覆盖相关区域的适当圆

import numpy as np
import matplotlib.pyplot as plt

# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax1.annotate('', xy=(7, 1096), xytext=(-0.5, 1096), # start the arrow from x=7 and draw towards primary y-axis
            arrowprops=dict(arrowstyle="<-", color=color))

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

# plt.arrow()
ax2.annotate('', xy=(6,0),  xytext=(10.4, 0), # start the arrow from x=6 and draw towards secondary y-axis
            arrowprops=dict(arrowstyle="<-", color=color))

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
下面是示例输出


此方法基于此。它使用arc,可按如下方式配置:

import matplotlib.pyplot as plt
from matplotlib.patches import Arc

# Generate example graph
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1)
ax.plot([1,2,3,4,5,6], [2,4,6,8,10,12])

# Configure arc
center_x = 2            # x coordinate
center_y = 3.8          # y coordinate
radius_1 = 0.25         # radius 1
radius_2 = 1            # radius 2 >> for cicle: radius_2 = 2 x radius_1
angle = 180             # orientation
theta_1 = 70            # arc starts at this angle
theta_2 = 290           # arc finishes at this angle
arc = Arc([center_x, center_y],
          radius_1,
          radius_2,
          angle = angle,
          theta1 = theta_1,
          theta2=theta_2,
          capstyle = 'round',
          linestyle='-',
          lw=1,
          color = 'black')

# Add arc
ax.add_patch(arc)

# Add arrow
x1 = 1.9            # x coordinate
y1 = 4              # y coordinate    
length_x = -0.5     # length on the x axis (negative so the arrow points to the left)
length_y = 0        # length on the y axis
ax.arrow(x1,
         y1,
         length_x,
         length_y,
         head_width=0.1,
         head_length=0.05,
         fc='k',
         ec='k',
         linewidth = 0.6)

结果如下所示:


我需要至少10个声誉来发布图像。很抱歉。。。请运行代码以查看自己的结果。干杯
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax1.annotate('', xy=(7, 1096), xytext=(-0.5, 1096), # start the arrow from x=7 and draw towards primary y-axis
            arrowprops=dict(arrowstyle="<-", color=color))

# circle1 = Circle((5, 3000), color='r')
# ax1.add_artist(circle1)
plt.scatter(7, 1096, s=100, facecolors='none', edgecolors='r')

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

# plt.arrow()
ax2.annotate('', xy=(6.7,0),  xytext=(10.5, 0), # start the arrow from x=6.7 and draw towards secondary y-axis
            arrowprops=dict(arrowstyle="<-", color=color))
plt.scatter(6,0, s=2000, facecolors='none', edgecolors=color)


fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.savefig('fig')
plt.show()
import matplotlib.pyplot as plt
from matplotlib.patches import Arc

# Generate example graph
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1)
ax.plot([1,2,3,4,5,6], [2,4,6,8,10,12])

# Configure arc
center_x = 2            # x coordinate
center_y = 3.8          # y coordinate
radius_1 = 0.25         # radius 1
radius_2 = 1            # radius 2 >> for cicle: radius_2 = 2 x radius_1
angle = 180             # orientation
theta_1 = 70            # arc starts at this angle
theta_2 = 290           # arc finishes at this angle
arc = Arc([center_x, center_y],
          radius_1,
          radius_2,
          angle = angle,
          theta1 = theta_1,
          theta2=theta_2,
          capstyle = 'round',
          linestyle='-',
          lw=1,
          color = 'black')

# Add arc
ax.add_patch(arc)

# Add arrow
x1 = 1.9            # x coordinate
y1 = 4              # y coordinate    
length_x = -0.5     # length on the x axis (negative so the arrow points to the left)
length_y = 0        # length on the y axis
ax.arrow(x1,
         y1,
         length_x,
         length_y,
         head_width=0.1,
         head_length=0.05,
         fc='k',
         ec='k',
         linewidth = 0.6)