英文:
Colorize based on condition
问题
以下是您要翻译的代码部分:
我有以下代码来显示一个图表。我想根据0.05的阈值条件性地格式化值。
from matplotlib.colors import to_rgba
# 生成 x 轴
x = np.linspace(0, len(data_formula), len(data_formula))
colors = np.where(data_formula <= 0.05, "blue", "green")
plt.plot(x, data_formula, c=colors)
# 添加标签和标题
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
# 显示图表
plt.show()
不幸的是,我收到了错误消息:“array(['blue', 'blue', 'blue', ..., 'blue', 'blue', 'blue'], dtype='<U5') is not a valid value for color”,这表明我向颜色参数传递了错误的值。我尝试过使用列表等方式,但似乎不起作用。出了什么问题?参数“color”是否根本不接受任何数据结构,还是格式不正确?
供参考:`data_formula` 定义如下:
def energy(x):
return (x**2)
data_formula = np.apply_along_axis(energy, axis=0, arr=data_normalized)
它的数据类型是:`numpy.ndarray`。
英文:
I am having the following code to display a plot. I would like to conditionally format the values based on a threshold of 0.05.
from matplotlib.colors import to_rgba
# Generate x-axis
x = np.linspace(0, len(data_formula), len(data_formula))
colors = np.where(data_formula <= 0.05, "blue", "green")
plt.plot(x, data_formula, c=colors)
# Add labels and title
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
# Display the plot
plt.show()
Unfortunately I do receive the error: array(['blue', 'blue', 'blue', ..., 'blue', 'blue', 'blue'], dtype='<U5') is not a valid value for color
, which indicates that I have passed the wrong value to the color parameter. I have tried it with lists, etc. But it doesn't seem to work. What went wrong? Does the argument color
just not accept any datastructure or is the format wrong?
For reference: data_formula
is defined as follow:
def energy(x):
return (x**2)
data_formula = np.apply_along_axis(energy, axis=0, arr=data_normalized)
It is of datatype: numpy.ndarray
.
答案1
得分: 1
Here are the translated code portions:
要使用不同的颜色,您应该将数据分段,每个段都有自己的颜色:
x = np.linspace(0, len(data_formula), len(data_formula))
crossings = np.where(np.diff(data_formula > 0.05))[0]
start = 0
# 绘制数据段
for ind in crossings:
plt.plot(x[start:ind+1], data_formula[start:ind+1],
color='blue' if data_formula[start] <= 0.05 else 'green')
start = ind+1
plt.plot(x[start:], data_formula[start:],
color='blue' if data_formula[start] <= 0.05 else 'green')
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
plt.show()
如果您只想要点而不是段,请执行以下操作:
for x, y in enumerate(data_formula):
plt.scatter(x, y, color='blue' if y <= 0.05 else 'green')
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
plt.show()
如果前一个答案太耗时,请尝试:
xy_blue = [(x, y) for x, y in enumerate(data_formula) if y <= 0.05]
xy_green = [(x, y) for x, y in enumerate(data_formula) if y > 0.05]
plt.scatter([t[0] for t in xy_blue], [t[1] for t in xy_blue], color='blue')
plt.scatter([t[0] for t in xy_green], [t[1] for t in xy_green], color='green')
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
plt.show()
请注意,这是代码的翻译部分,不包括其他内容。
英文:
For using different colors you should split data segments each one with its color:
#%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, len(data_formula), len(data_formula))
crossings = np.where(np.diff(data_formula > 0.05))[0]
start = 0
# Plot segments
for ind in crossings:
plt.plot(x[start:ind+1], data_formula[start:ind+1],
color='blue' if data_formula[start] <= 0.05 else 'green')
start = ind+1
plt.plot(x[start:], data_formula[start:],
color='blue' if data_formula[start] <= 0.05 else 'green')
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
plt.show()
I assumed your data is like:
data_formula = np.array([0, 0.01, 0.02, 0.04, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
If instead of segments you just want the points do the following:
import numpy as np
import matplotlib.pyplot as plt
for x, y in enumerate(data_formula):
plt.scatter(x, y, color='blue' if y <= 0.05 else 'green')
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
plt.show()
this last one inspired from stackoverflow: different colors series scatter plot on matplotlib.
If the previous answer takes to much time try:
import numpy as np
import matplotlib.pyplot as plt
# data_formula = np.array([0, 0.01, 0.02, 0.04, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
xy_blue = [(x, y) for x, y in enumerate(data_formula) if y <= 0.05]
xy_gree = [(x, y) for x, y in enumerate(data_formula) if y > 0.05]
plt.scatter([t[0] for t in xy_blue], [t[1] for t in xy_blue], color='blue')
plt.scatter([t[0] for t in xy_gree], [t[1] for t in xy_gree], color='green')
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
plt.show()
Anyway, I've never see a plot to take more than 30sec so maybe you have to reload your environment, you have way too much data to plot and should make different plots, or you have a low RAM issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论