基于条件着色

huangapple go评论51阅读模式
英文:

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 &lt;= 0.05, &quot;blue&quot;, &quot;green&quot;)
plt.plot(x, data_formula, c=colors)

# Add labels and title
plt.ylabel(&#39;Volume&#39;)
plt.xlabel(&#39;time&#39;)
plt.title(&#39;Energy&#39;)

# Display the plot
plt.show()

Unfortunately I do receive the error: array([&#39;blue&#39;, &#39;blue&#39;, &#39;blue&#39;, ..., &#39;blue&#39;, &#39;blue&#39;, &#39;blue&#39;], dtype=&#39;&lt;U5&#39;) 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 &gt; 0.05))[0]
start = 0

# Plot segments
for ind in crossings:
    plt.plot(x[start:ind+1], data_formula[start:ind+1], 
             color=&#39;blue&#39; if data_formula[start] &lt;= 0.05 else &#39;green&#39;)
    start = ind+1

plt.plot(x[start:], data_formula[start:], 
         color=&#39;blue&#39; if data_formula[start] &lt;= 0.05 else &#39;green&#39;)

plt.ylabel(&#39;Volume&#39;)
plt.xlabel(&#39;time&#39;)
plt.title(&#39;Energy&#39;)
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=&#39;blue&#39; if y &lt;= 0.05 else &#39;green&#39;)

    
plt.ylabel(&#39;Volume&#39;)
plt.xlabel(&#39;time&#39;)
plt.title(&#39;Energy&#39;)
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 &lt;= 0.05]
xy_gree = [(x, y) for x, y in enumerate(data_formula) if y &gt; 0.05]

plt.scatter([t[0] for t in xy_blue], [t[1] for t in xy_blue], color=&#39;blue&#39;)
plt.scatter([t[0] for t in xy_gree], [t[1] for t in xy_gree], color=&#39;green&#39;)

    
plt.ylabel(&#39;Volume&#39;)
plt.xlabel(&#39;time&#39;)
plt.title(&#39;Energy&#39;)
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.

huangapple
  • 本文由 发表于 2023年5月13日 18:57:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定