如何根据条件(Python)绘制不同透明胶片的多条线?
我有房屋的一些能耗数据。在此数据框中,每一行显示每日消耗曲线(能量值是从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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以计算每个类别(热和冷)的几天的umber,并相应地设置Alphya值,类似的内容:
考虑到您的评论,您可以尝试一下。在这里,我使用的是颜色由4个值组成,最后一个值对应于alpha通道(即透明度),并且我在temp_avg中使用该值 +热和冷值的比例来确定哪个是哪个是最之前的一个和相应地设置alpha值。
由于元组是Python中不变的对象,因此我将其列出以列出,修改Alpha值并将其归还给元组。
You could count the umber of days for each category (hot and cold) and set the alphya value accordingly, something like this:
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.