- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Anaconda 2.7,我的 fill_between()
尝试没有结果。我不确定我是否遗漏了一个包,或者我的绘图语法是否正在抛出 python...
这是我的代码:
from scipy import stats
import matplotlib.pyplot as plt
from numpy import linspace
alpha_A = 11
beta_A = 41
alpha_B = 3
beta_B = 3
x = linspace(0,1,num = 1000)
postA = stats.beta(alpha_A, beta_A).pdf(x)
postB = stats.beta(alpha_B, beta_B).pdf(x)
plt.figure(2, figsize = (6,4))
plt.plot(postA, color = 'r', label = "A: Beta(" + str(alpha_A) + ',' + str(beta_A) + ')')
plt.plot(postB, color = 'b',label = "B: Beta(" + str(alpha_B) + ',' + str(beta_B) + ')')
plt.legend(loc = "best", frameon = False)
plt.fill_between(x, postA, facecolor = "red") # <---- not working
frame1 = plt.gca()
frame1.axes.get_xaxis().set_ticks([])
ax = plt.gca()
ax.set_xticks([0,200,400,600,800,1000])
ax.set_xticklabels( ['0.0','0.2','0.4','0.6','0.8','1.0']) # https://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#setting-tick-labels
ax.set_title("Posterior Distributions")
这给了我这个图表,其中没有出现红色填充:
最佳答案
它确实有效,如果您放大绘图的左侧(您可以在显示的图像上看到它,垂直线上升到 7)。
那么,这是为什么呢?
这是因为你的绘图在 x 轴上上升到 1000,而你要求将其填充到 1 (max(x))。
2 种解决方案:
快速的:
你替换这一行:
plt.fill_between(x, postA, facecolor = "red")
这个
plt.fill_between(range(len(x)), postA, facecolor = "red")
干净的:
代码的第二部分变为:
fig = plt.figure(figsize = (6,4))
ax = fig.add_subplot(111)
ax.plot(x, postA, color = 'r', label = "A: Beta(" + str(alpha_A) + ',' + str(beta_A) + ')')
ax.plot(x, postB, color = 'b',label = "B: Beta(" + str(alpha_B) + ',' + str(beta_B) + ')')
ax.legend(loc = "best", frameon = False)
ax.fill_between(x, postA, facecolor = "red")
ax.set_title("Posterior Distributions")
在这里,如果您只提供 Y 值,则您将绘图中的 x 值指定为 x,而不是 len(postA)。因此,您将直接获得正确的 x-ticks。
这里是干净溶液的结果:
希望这对您有所帮助。
关于python - 为什么 fill_between 在这里不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26300147/
我有以下数据框: symbol DAL MS QQQ SPY TLT XLE symbol
您好,我在 python 中使用 fill_between 命令,但 fill_between 似乎总是在线图下方。 看这个例子,alpha=1,所以不是不可见的。当它们位于 fill_between
我正在创建一个当前看起来像这样的“事件图”: 但是,我不知道如何为每个颜色组添加图例。这就是现在创建情节的方式: handles = dict() for i, channel_events in e
我正在创建一个当前看起来像这样的“事件图”: 但是,我不知道如何为每个颜色组添加图例。这就是现在创建情节的方式: handles = dict() for i, channel_events in e
我正在使用 Anaconda 2.7,我的 fill_between() 尝试没有结果。我不确定我是否遗漏了一个包,或者我的绘图语法是否正在抛出 python... 这是我的代码: from scip
我有一些问题与使用 mplot3D 在 Python 中创建一个简单的线图有关,其中填充了图下方的区域。我在 RedHatEnterprise 7.2、matplotlib 1.2.0 和 numpy
我有一个奇怪的情况,我需要一些帮助。我有以下数据: xdata = [ 11.125, 11.375, 11.625, 11.875, 12.125, 12.375, 12.625, 1
当您在同一张图上绘制多个内容时,大多数绘图方法(如 plot() 和 errorbar)会自动更改为 color_palette 中的下一种颜色。由于某些原因,fill_between() 不是这种情
我必须将月份名称显示为 xticks,当我绘制图形并将 x 作为月份名称传递时,它绘制错误。我还必须在折线图上叠加一个散点图。 我不能在这里粘贴完整的代码,因为它是一个 MOOC 作业,我只是在寻找我
这类似于 Matlab: Combine the legends of shaded error and solid line mean ,除了 Matplotlib。示例代码: import num
我用下面的代码在空间中绘制了随机三角形,但我想用颜色填充三角形。我知道 matplotlib 中的 fill_between() 函数,但是我不确定如何在下面的示例中实现它 import matplo
我是新手,正在尝试 .fill_between() 我图表上的两条线。我已经尝试了所有我能想到的方法来确保基础数据是正确的,但每次我绘制此图表时,.fill_between() 都会在 x 轴上出现一
我正在尝试将我的 fill_between 添加到图例中,但它不起作用?为什么它与其他地 block 不同?这是我现在的图表: plt.scatter(stageheight,discharge,co
我正在尝试对由此示例代码生成的两条曲线的交点之前的区域进行着色: import numpy as np import matplotlib.pyplot as plt x = np.arange(0,
我从两个数据帧创建了一个 Pandas 图: ttload.plot(legend=True, figsize=(12,7)) zzload.plot(legend=False) 现在,我想使用 fi
我有一种感觉,我要用这个来敲我的额头,但我正在尝试填充两个极函数的公共(public)内部 r = 4 sin(2θ ) 和 r = 2。看来我得到的与我想要的相反。有什么想法吗? import nu
我绘制了(使用 matplotlib)时间序列及其相关的置信区间上限和下限(我在 Stata 中计算)。我使用 Pandas 读取 stata.csv 输出文件,因此该系列的类型为 pandas.co
我正在尝试为 matplotlib 中的 fill_between 形状制作动画,但我不知道如何更新 PolyCollection 的数据。举个简单的例子:我有两条线,我总是在它们之间填充。当然,线条
我想 Fill_Between 正态分布的一个子部分,比如说左边的 5%tile。 import numpy as np import matplotlib.pyplot as plt from sc
我想制作一个移动图,在绘制曲线时曲线下方的区域会变色。 我用谷歌搜索了一下,发现我应该以某种方式创建一个补丁。但是,我不明白他们给出的任何例子,所以让我在这里用我的具体例子问一下: import nu
我是一名优秀的程序员,十分优秀!