如何根据条件(Python)绘制不同透明胶片的多条线?

发布于 2025-02-06 17:26:35 字数 2041 浏览 2 评论 0原文

我有房屋的一些能耗数据。在此数据框中,每一行显示每日消耗曲线(能量值是从0到1的数字,每小时1个值),最后一列“ temp_avg”代表当天的平均室外温度。 在下面,您可以找到一个与我的结构相同的数据框架,并填充了随机数。

之后,我根据temp_avg的值创建了一个colormap,以便以蓝色绘制寒冷的日子,并以红色绘制温暖的日子。温度越高,深色将是该线的红色,而寒冷的日子则越来越糟糕。

我想做的是更改“炎热日” (temp_avg> 15)的透明度:由于寒冷的日子在数据框架中更相关(6天中有4天有temp_avg< 15 )我不希望蓝线被一些在数据帧中不太相关的红线所打扰。

因此,我想将“炎热日”的alpha设置为较低的值:仍然必须根据颜色图对它们进行颜色,但是它们需要更透明,而更相关的行必须保持alpha = 1。

我该怎么做?有没有办法自动化此过程?含义:如果炎热的日子少于寒冷的日子,使炎热的日子更加透明...但是如果相反,寒冷的日子少于炎热的日子,使寒冷的日子更加透明。


    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    from matplotlib.colors import ListedColormap, LinearSegmentedColormap, Normalize
    
    # Create 2D panda of 6 rows and 24 columns,
    # filled with random values from 0 to 1
    
    random_data = np.random.rand(6,24)
    df = pd.DataFrame(random_data)
    df.index = ['Day_1', 'Day_2', 'Day_3', 'Day_4', 'Day_5', 'Day_6']
    df['Temp_avg'] = [30, 28, 4, 6, 5, 9]
    print(df)
    
    # create a color map
    
    cmapR = cm.get_cmap('coolwarm')
    norm = Normalize(vmin=df['Temp_avg'].min(), vmax=df['Temp_avg'].max())
    colors = [cmapR(norm(v)) for v in df['Temp_avg']]
    
    df.iloc[:, 0:24].T.plot(kind='line', color=colors, legend=False)
    plt.show()

             0         1         2  ...        22        23  Temp_avg
Day_1  0.806990  0.406354  0.095396  ...  0.492237  0.205613        30
Day_2  0.437527  0.172589  0.285325  ...  0.781534  0.964967        28
Day_3  0.434903  0.175761  0.386593  ...  0.282011  0.539182         4
Day_4  0.465063  0.223923  0.094135  ...  0.372364  0.608879         6
Day_5  0.993202  0.089822  0.976565  ...  0.515035  0.739329         5
Day_6  0.561553  0.759399  0.500864  ...  0.909193  0.723620         9

I have some energy consumption data of a house. In this DataFrame every row shows a daily consumption profile (energy values are numbers from 0 to 1, one value per hour) and the last column 'Temp_avg' represents the Average Outdoor Temperature of that day.
Below you can find a DataFrame with the same structure as mine, filled with random numbers.

After that, I create a colormap based on the values of Temp_avg, so that the cold days will be plotted in blue and the warm days in red. The more the temperature is high, the darker will be the red color of that line, and viceversa for cold days.

What I want to do is to change the transparency of "hot days" (Temp_avg > 15): since the cold days are more relevant in the DataFrame (4 days out of 6 have Temp_avg < 15) I don't want the blue lines to be disturbed by some red lines that are less relevant in the DataFrame.

So I want to set the alpha of "hot days" to a lower value: they still have to be colored based on the color map, but they need to be more transparent, while the more relevant lines have to keep alpha=1.

How can I do it? And is there a way to automate this process? Meaning: if hot days are fewer than cold days make hot days more transparent... but if, instead, cold days are fewer than hot days make cold days more transparent.


    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    from matplotlib.colors import ListedColormap, LinearSegmentedColormap, Normalize
    
    # Create 2D panda of 6 rows and 24 columns,
    # filled with random values from 0 to 1
    
    random_data = np.random.rand(6,24)
    df = pd.DataFrame(random_data)
    df.index = ['Day_1', 'Day_2', 'Day_3', 'Day_4', 'Day_5', 'Day_6']
    df['Temp_avg'] = [30, 28, 4, 6, 5, 9]
    print(df)
    
    # create a color map
    
    cmapR = cm.get_cmap('coolwarm')
    norm = Normalize(vmin=df['Temp_avg'].min(), vmax=df['Temp_avg'].max())
    colors = [cmapR(norm(v)) for v in df['Temp_avg']]
    
    df.iloc[:, 0:24].T.plot(kind='line', color=colors, legend=False)
    plt.show()

             0         1         2  ...        22        23  Temp_avg
Day_1  0.806990  0.406354  0.095396  ...  0.492237  0.205613        30
Day_2  0.437527  0.172589  0.285325  ...  0.781534  0.964967        28
Day_3  0.434903  0.175761  0.386593  ...  0.282011  0.539182         4
Day_4  0.465063  0.223923  0.094135  ...  0.372364  0.608879         6
Day_5  0.993202  0.089822  0.976565  ...  0.515035  0.739329         5
Day_6  0.561553  0.759399  0.500864  ...  0.909193  0.723620         9

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2025-02-13 17:26:35

您可以计算每个类别(热和冷)的几天的umber,并相应地设置Alphya值,类似的内容:

nb_of_hot_days = (df['Temp_avg']>15).sum()
nb_of_cold_days = (df['Temp_avg']<15).sum()
alpha_cold = 1.0 if nb_of_cold_days > nb_of_hot_days else nb_of_cold_days/nb_of_hot_days
alpha_hot = 1.0 if nb_of_hot_days > nb_of_cold_days else nb_of_hot_days/nb_of_cold_days

考虑到您的评论,您可以尝试一下。在这里,我使用的是颜色由4个值组成,最后一个值对应于alpha通道(即透明度),并且我在temp_avg中使用该值 +热和冷值的比例来确定哪个是哪个是最之前的一个和相应地设置alpha值。
由于元组是Python中不变的对象,因此我将其列出以列出,修改Alpha值并将其归还给元组。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap, Normalize
   
# Create 2D panda of 6 rows and 24 columns,
# filled with random values from 0 to 1

random_data = np.random.rand(6,24)
df = pd.DataFrame(random_data)
df.index = ['Day_1', 'Day_2', 'Day_3', 'Day_4', 'Day_5', 'Day_6']
df['Temp_avg'] = [30, 28, 4, 6, 5, 9]
print(df)

 
nb_of_hot_days = (df['Temp_avg']>15).sum()
nb_of_cold_days = (df['Temp_avg']<15).sum()

if nb_of_cold_days > nb_of_hot_days:
    cold_days_alpha = 1.0
    hot_days_alpha = nb_of_hot_days/nb_of_cold_days
else:
    cold_days_alpha = nb_of_cold_days/nb_of_hot_days
    hot_days_alpha = 1.0

cmapR = cm.get_cmap('coolwarm')
norm = Normalize(vmin=df['Temp_avg'].min(), vmax=df['Temp_avg'].max())
colors = [tuple(list(cmapR(norm(v))[:3]) + [hot_days_alpha]) if v > 15 else tuple(list(cmapR(norm(v))[:3]) + [cold_days_alpha]) for v in df['Temp_avg']]

# colors_alphas = [(c[:3], 1.0) if ]

df.iloc[:, 0:24].T.plot(kind='line', color=colors, legend=False)
plt.show()

You could count the umber of days for each category (hot and cold) and set the alphya value accordingly, something like this:

nb_of_hot_days = (df['Temp_avg']>15).sum()
nb_of_cold_days = (df['Temp_avg']<15).sum()
alpha_cold = 1.0 if nb_of_cold_days > nb_of_hot_days else nb_of_cold_days/nb_of_hot_days
alpha_hot = 1.0 if nb_of_hot_days > nb_of_cold_days else nb_of_hot_days/nb_of_cold_days

taking you comment into account you could try this. Here I use the fact that color are compose of 4 values and the last one correspond to the alpha channel (i.e. the transparency) and I use the value in Temp_avg + the proportion of Hot and Cold value to determine which is the most present one and set alpha value accordingly.
As tuple are immutable object in python, I cast them to list, modify the alpha value and cast them back to tuple.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap, Normalize
   
# Create 2D panda of 6 rows and 24 columns,
# filled with random values from 0 to 1

random_data = np.random.rand(6,24)
df = pd.DataFrame(random_data)
df.index = ['Day_1', 'Day_2', 'Day_3', 'Day_4', 'Day_5', 'Day_6']
df['Temp_avg'] = [30, 28, 4, 6, 5, 9]
print(df)

 
nb_of_hot_days = (df['Temp_avg']>15).sum()
nb_of_cold_days = (df['Temp_avg']<15).sum()

if nb_of_cold_days > nb_of_hot_days:
    cold_days_alpha = 1.0
    hot_days_alpha = nb_of_hot_days/nb_of_cold_days
else:
    cold_days_alpha = nb_of_cold_days/nb_of_hot_days
    hot_days_alpha = 1.0

cmapR = cm.get_cmap('coolwarm')
norm = Normalize(vmin=df['Temp_avg'].min(), vmax=df['Temp_avg'].max())
colors = [tuple(list(cmapR(norm(v))[:3]) + [hot_days_alpha]) if v > 15 else tuple(list(cmapR(norm(v))[:3]) + [cold_days_alpha]) for v in df['Temp_avg']]

# colors_alphas = [(c[:3], 1.0) if ]

df.iloc[:, 0:24].T.plot(kind='line', color=colors, legend=False)
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文