- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Matplotlib 中有两条曲线,我想让它们正好在彼此的下方。我天真的方法是将一条曲线的值减少一个常数因子。奇怪的是,在 Matplotlib(与 Excel 相对)中,创建的曲线有一些偏差,并且并不总是在彼此下方。
这是我的代码:
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
load = [0.0, 0.1, 0.5, 0.7, 0.4, 0.5, 0.4, 0.3, 0.4, 0.5, 0.65, 0.75, 0.8, 0.65, 0.15, 0.55, 0.1, 0.4, 0.0, 0.25, 0.25, 0.35, 0.6, 0.25, 0.05]
#demand = [0.35, 0.25, 0.15, 0.1, 0.1, 0.2, 0.40, 0.50, 0.50, 0.40, 0.40, 0.47, 0.47, 0.40, 0.35, 0.35, 0.4, 0.5, 0.65, 0.7, 0.65, 0.5, 0.4, 0.35, 0.25]
help = 0.025;
demand = [0.0-help, 0.1-help, 0.5-help, 0.7-help, 0.4-help, 0.5-help, 0.4-help, 0.3-help, 0.4-help, 0.5-help, 0.65-help, 0.75-help, 0.8-help, 0.65-help, 0.15-help, 0.55-help, 0.1-help, 0.4-help, 0.0-help, 0.25-help, 0.25-help, 0.35-help, 0.6-help, 0.25-help, 0.05-help]
hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]
f = interp1d(hours, load, kind='cubic')
f2 = interp1d(hours, demand, kind='cubic')
xnew = np.linspace(0, 24, num=500, endpoint=True)
plt.xticks(np.arange(0, 25, step=1)) # Set label locations.
plt.xticks(np.arange(25), labels) # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90)
plt.plot(hours, load, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
#plt.legend(['data', 'linear', 'cubic'], loc='best')
plt.ylabel("Electrical power in W", fontsize=14, labelpad=8)
plt.xlabel("Time of day", fontsize=14, labelpad=8)
plt.show()
plt.xticks(np.arange(0, 25, step=1)) # Set label locations.
plt.xticks(np.arange(25), labels) # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90)
plt.xlim(xmin=0.0)
xnew = np.linspace(0, 24, num=500, endpoint=True)
plt.xticks(np.arange(0, 25, step=1)) # Set label locations.
plt.xticks(np.arange(25), labels) # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90)
plt.plot(xnew, f(xnew), color="gold", linewidth=3)
plt.plot(xnew, f2(xnew), color="green", linewidth=3)
#plt.fill_between(xnew, f(xnew), color="gold", alpha=0.30, edgecolor=None)
#plt.fill_between(xnew, f2(xnew), color="red", alp_ha=0.30, edgecolor=None)
plt.legend( ['Electricity generation', 'Electricity demand'], bbox_to_anchor=(0.5, 1.2), loc='upper center' )
plt.ylabel("Electrical power", fontsize=14, labelpad=8)
plt.xlabel("Time of day", fontsize=14, labelpad=8)
plt.tick_params(labelleft=False)
plt.savefig('Generation_Demand_DSM.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()
在这里你会看到结果。我举例说明了一些曲线几乎相同的区域(因此一个与另一个相距不远)和偏差过高的区域。
最佳答案
正如许多人已经在评论中建议的那样,当您减去 y 值时,只有 y 轴发生了变化,而 x 轴保持不变,因此不会产生您想要的效果。这主要是因为曲线的斜率不同。但是,我们可以使用斜率信息人为地创建偏移。
效果需要进一步细化和微调才能获得准确的位移。我在这里分享代码作为起点。
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
load = [0.0, 0.1, 0.5, 0.7, 0.4, 0.5, 0.4, 0.3, 0.4, 0.5, 0.65, 0.75, 0.8, 0.65, 0.15, 0.55, 0.1, 0.4, 0.0, 0.25, 0.25, 0.35, 0.6, 0.25, 0.05]
shift = 0.02;
demand = [l-shift for l in load]
hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]
fig = plt.figure(figsize=(20,10))
f = interp1d(hours, load, kind='quadratic')
f2 = interp1d(hours, demand, kind='quadratic')
xnew = np.linspace(0, 24, num=500, endpoint=True)
plt.xticks(np.arange(0, 25, step=1)) # Set label locations.
plt.xticks(np.arange(25), labels) # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90)
plt.xlim(xmin=0.0)
xnew = np.linspace(0, 24, num=500, endpoint=True)
plt.xticks(np.arange(0, 25, step=1)) # Set label locations.
plt.xticks(np.arange(25), labels) # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90)
xshift = xnew+np.concatenate(([0],np.diff(f(xnew))*7))
plt.plot(xnew, f(xnew), color="gold", linewidth=5)
plt.plot(xshift, f2(xnew), color="green", linewidth=5)
# plt.fill_between(xnew+np.concatenate(([0],np.diff(f(xnew))*7)),
# f(xnew)-0.02, f(xnew), color="gold", alpha=0.30, edgecolor=None)
#plt.fill_between(xnew, f2(xnew), color="red", alp_ha=0.30, edgecolor=None)
plt.legend( ['Electricity generation', 'Electricity demand'], bbox_to_anchor=(0.5, 1.2), loc='upper center' )
plt.ylabel("Electrical power", fontsize=14, labelpad=8)
plt.xlabel("Time of day", fontsize=14, labelpad=8)
plt.tick_params(labelleft=False)
plt.savefig('Generation_Demand_DSM.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()
注释:
# plt.plot(xnew, f(xnew), color="gold", linewidth=5)
plt.plot(xshift, f2(xnew), color="green", linewidth=5)
# plt.fill_between(xnew+np.concatenate(([0],np.diff(f(xnew))*7)),
# f(xnew)-0.02, f(xnew), color="gold", alpha=0.30, edgecolor=None)
#plt.fill_between(xnew, f2(xnew), color="red", alp_ha=0.30, edgecolor=None)
# plt.legend( ['Electricity generation', 'Electricity demand'], bbox_to_anchor=(0.5, 1.2), loc='upper center' )
# plt.ylabel("Electrical power", fontsize=14, labelpad=8)
# plt.xlabel("Time of day", fontsize=14, labelpad=8)
# plt.tick_params(labelleft=False)
plt.xticks([])
plt.yticks([])
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.gca().spines['bottom'].set_visible(False)
plt.savefig('Generation_Demand_DSM_second.png', edgecolor='none', dpi=400, bbox_inches='tight', transparent=True)
plt.show()
它会生成这样的输出(请注意,这里以白色背景呈现,但原始 png 将保留透明度):
plt.savefig('Generation_Demand_DSM_second.pdf', bbox_inches='tight')
生成的 pdf 可在inkscape 中完全编辑。您只需要像打开任何其他文件一样打开它(大多数默认设置应该可以工作,或者您可以在打开的对话框中选择 poppler 导入)。每个元素都可以单独选择,如此处的屏幕截图所示:
关于python - 在 Matplotlib 中将一条曲线正好放在另一条曲线的下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63954726/
我当前项目中的许多类都有几个只能从类本身内部调用的属性和方法。此外,根据类(class)的当前状态,它们可能会扰乱类(class)的工作。 目前,所有这些接口(interface)都在 .h 文件的主
我想使用ngbTabSet将nav-pills in card-header 和tab-content in card-body。但我不知道该怎么做。 这是我尝试实现的示例(使用 bootstrap.
我正在浏览文档以查找如何允许放置在停靠栏图标上。据我所知,建议您使用 LSItemContentTypes,因为 CFBundleTypeOSTypes 已弃用。但是,我无法让 LSItemConte
我正在尝试在书签中使用 jquery UI 作为 slider 。并且 jquery ui 要求在普通 jquery 文件之后包含该文件。 所以到目前为止我所尝试的只是将脚本附加到 header ,同
您好,我尝试了广泛的谷歌搜索,但似乎没有任何帮助。 这是我的场景: 公司 Logo 存储在外部域/网址(矩形)中。 带有谷歌地图的 Ionic 应用程序,将这些 Logo 作为标记放置在 map 上。
我今天在阅读我的一些 C# 代码时发现了这一行: if (ProgenyList.ItemContainerGenerator.Status != System.Windows.Controls.Pr
我刚遇到this question在 Go FAQ 中,它让我想起了困扰我一段时间的事情。不幸的是,我真的不明白答案是什么。 似乎几乎所有非 C 类语言都将类型放在变量名之后,如下所示: var :
这是有效的 HTML 吗? 最佳答案 作为元素可以包含 phrasing content ,以及属于该组,则有效。 从语义上(并且具有一些常识),如果唯一的内容是 , 不;如果它同时包含文本和图像
这是有效的 HTML 吗? 最佳答案 作为元素可以包含 phrasing content ,以及属于该组,则有效。 从语义上(并且具有一些常识),如果唯一的内容是 , 不;如果它同时包含文本和图像
我有两本 T.Parr 写的关于 ANTLR 的书,我到处都看到美元符号和符号的引用。它也对我有用: term : IDENT -> { new TokenNode($IDENT) }; 或更复杂的东
我在实现段控制时遇到了一些问题。因为我希望它是一个固定的标题,所以当我滚动时我总是可以看到它,我已经在 - (UIView *)tableView:(UITableView *)tableView v
我有一个 20x36px (10x18pt) 的箭头图像,当我选择一个 UIImageView 时,将它拖到我的 View Controller 上然后设置图像,它总是模糊的。我只在我的项目中包含 @
How can I put background image when I hover a link Insert Bg in this a when hov
我需要在我的 .container 中添加(最新的第一个)新分区,但在 .controls (按钮)之后。可能吗? 注意:添加新的分区来保存按钮下方的前置控件对我来说不是一个选择。 HTML 需要保持
我是一名优秀的程序员,十分优秀!