使用matplotlib为特定范围的值更改线条样式 [英] Changing line style for certain range of values using matplotlib

查看:37
本文介绍了使用matplotlib为特定范围的值更改线条样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要复制这种类型的图(分叉图)-

I need to reproduce plots of this type (bifurcation plots) -

我尝试遵循 change-matplotlib-line-style-mid中给出的示例-graph,但无法真正解决.有人可以给个小费吗?

I tried to follow the example given at change-matplotlib-line-style-mid-graph, but couldn't really work it out. Could someone give a tip?

推荐答案

在下面,我整理了4个不同的示例.它们显示不同的东西:

In the following I have put together 4 different examples. They show different things:

  1. 在您的问题中重新创建图表的最简单方法.请注意,每条线仅由两个点定义.
  2. 这就是我想拥有的:定义每行的两个x和y向量(在我的示例中,每个向量都有100个值).
  3. 这个对你来说是最有趣的(我猜):向量 solid1solid2 用于选择满足条件的 x 和 y 值.
  4. 仅向您展示如何将棘刺置于中心(因为问题中的图形将棘刺居中).
  1. The simplest way to recreate the graph in your question. Note, that each line is only defined by two points.
  2. This is what I assume you have: two x and y vectors that define each line (in my example they each have 100 values).
  3. This one is most interesting for you (I guess): The vectors solid1 and solid2 are used to select the values of x and y, that satisfy the conditions.
  4. Just to show you how you can put the spines in the center (since the graph in your question has them centered).

代码:

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(12,4))

# Example 1
plt.subplot(1,4,1)
line1 = np.array([[-1,0],[0,0]])
line2 = np.array([[0,0],[1,1]])
line3 = np.array([[-1,-1],[0,0]])
line4 = np.array([[0,0],[1,0]])
plt.plot(line1[:,0], line1[:,1], 'b', linewidth=4)
plt.plot(line2[:,0], line2[:,1], 'b', linewidth=4)
plt.plot(line3[:,0], line3[:,1], 'b--', linewidth=4)
plt.plot(line4[:,0], line4[:,1], 'b--', linewidth=4)

# Example 2
plt.subplot(1,4,2)
x1 = np.linspace(-1,1,100)
x2 = np.linspace(-1,1,100)
y1 = x1*0
y2 = x2
plt.plot(x1,y1,'r', linewidth=4)
plt.plot(x2,y2,'g', linewidth=4)

# Example 3
plt.subplot(1,4,3)
#some sort of split condition:
solid1 = x1<0
solid2 = x2>0
#plot each line in two seperate plot calls
plt.plot(x1[solid1], y1[solid1], 'r', linewidth=4)
plt.plot(x1[np.logical_not(solid1)], y1[np.logical_not(solid1)], 'r--', linewidth=4)
plt.plot(x2[solid2], y2[solid2], 'g', linewidth=4)
plt.plot(x2[np.logical_not(solid2)], y2[np.logical_not(solid2)], 'g--', linewidth=4)

# Example 4 
plt.subplot(1,4,4)
# put the spines to the middle
ax = plt.gca()
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.show()

这篇关于使用matplotlib为特定范围的值更改线条样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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