- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在时间轴上放置一系列 (matplotlib) 箱线图。它们是在一年中的不同日子进行的一系列测量。日期分布不均,我对随时间的变化很感兴趣。
我有一个带有索引和一系列数字的 pandas DataFrame,或多或少像这样:(注意索引):
np.random.seed(12345)
data = np.array( [ np.random.normal( i, 1, 10 ) for i in range(3) ] )
ii = np.array([ 3, 5, 8 ] )
df = pd.DataFrame( data=data, index=ii )
对于每个索引,我需要制作一个箱线图,这没问题:
plt.boxplot( [ df.loc[i] for i in df.index ], vert=True, positions=ii )
问题是,我需要将盒子放在时间轴上,即将盒子放在具体日期上
np.random.seed(12345)
data = np.array( [ np.random.normal( i, 1, 10 ) for i in range(3) ] )
dates = pd.to_datetime( [ '2015-06-01', '2015-06-15', '2015-08-30' ] )
df = pd.DataFrame( data=data, index=dates )
plt.boxplot( [ df.loc[i] for i in df.index ], vert=True )
但是,如果我合并这些职位:
ax.boxplot( [ df.loc[i] for i in df.index ], vert=True, positions=dates )
我得到一个错误:
TypeError: Cannot compare type 'Timedelta' with type 'float'
查看文档显示:
plt.boxplot?
positions : array-like, default = [1, 2, ..., n]
Sets the positions of the boxes. The ticks and limits are automatically set to match the positions.
此代码旨在澄清、缩小问题范围。方框应该出现在那里,即下图中蓝色点所在的位置。
np.random.seed(12345)
data = np.array( [ np.random.normal( i, 1, 10 ) for i in range(3) ] )
dates = pd.to_datetime( [ '2015-06-01', '2015-06-15', '2015-08-30' ] )
df = pd.DataFrame( data=data, index=dates )
fig, ax = plt.subplots( figsize=(10,5) )
x1 = pd.to_datetime( '2015-05-01' )
x2 = pd.to_datetime( '2015-09-30' )
ax.set_xlim( [ x1, x2 ] )
# ax.boxplot( [ df.loc[i] for i in df.index ], vert=True ) # Does not throw error, but plots nothing (out of range)
# ax.boxplot( [ df.loc[i] for i in df.index ], vert=True, positions=dates ) # This is what I'd like (throws TypeError)
ax.plot( dates, [ df.loc[i].mean() for i in df.index ], 'o' ) # Added to clarify the positions I aim for
有没有一种方法可以在时间轴上放置箱线图?
我正在使用:
python: 3.4.3 + numpy: 1.11.0 + pandas: 0.18.0 + matplotlib: 1.5.1
最佳答案
到目前为止,我最好的解决方案是将轴的单位转换为合适的 int
单位并相应地绘制所有内容。就我而言,那是几天。
np.random.seed(12345)
data = np.array( [ np.random.normal( i, 1, 10 ) for i in range(3) ] )
dates = pd.to_datetime( [ '2015-06-01', '2015-06-15', '2015-08-30' ] )
df = pd.DataFrame( data=data, index=dates )
fig, ax = plt.subplots( figsize=(10,5) )
x1 = pd.to_datetime( '2015-05-01' )
x2 = pd.to_datetime( '2015-09-30' )
pos = ( dates - x1 ).days
ax.boxplot( [ df.loc[i] for i in df.index ], vert=True, positions=pos )
ax.plot( pos, [ df.loc[i].mean() for i in df.index ], 'o' )
ax.set_xlim( [ 0, (x2-x1).days ] )
ax.set_xticklabels( dates.date, rotation=45 )
箱线图放置在正确的位置,但代码对我来说似乎有点麻烦。
更重要的是:x轴的单位不再是“时间”。
关于python - 放置在时间轴上的箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38576692/
我正在练习课本上的练习,但无法获得应有的输出。 这是我所拥有的: #include #include #include //Initialize OpenGL void init(void) {
我有一系列的点,我将把它们变成图表上的一条线。我想要的是给图表下方的区域进行渐变填充。它看起来有点类似于这样的彭博图表; 我的问题实际上分为三个部分; 首先,我应该如何只填充图表下方的区域? 第二,如
我正在尝试根据数组中保存的数据绘制折线图。我已经成功绘制了 X 轴和 Y 轴。然而,当我在图表上绘制带有数据的线时,它与 X 轴和 Y 轴值不对齐。我不确定如何解决这个问题。我已经展示了下面的代码:
我有一个对象 DataEntry 定义如下: public class DataEntry { private long timestamp; private double value; public
我正在使用 MPAndroidChart-v2.1.6,我面临以下问题。 当我有一个数据条目时,它开始在 X 轴上显示从 0 索引开始的值。这就是它的样子。 当我只有一个数据输入时,我希望它居中对齐,
我有一个 pandas 数据框,看起来像这样 - Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Marks 0 30 31 29 1
enter image description here嗨, 我正在尝试重新创建我们看到的一些 covid-19 图表。我正在使用来自约翰霍普金斯大学数据库的数据。 数据的排列方式是城市名称在行中,列
我正在使用 Pandas 来绘制一些数据。 如果我绘制这个: import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame
当我使用 Seaborn 在同一轴上绘制多个线图时,不会创建任何图例。即使我在 sns.lineplot 中为图例提供参数“brief”或“full”,也不会显示任何内容,并且调用 ax.get_le
根据 Shanes 优秀 solution另一个问题,我现在意识到我不知道该怎么做。 我最初的方法是使用融化数据(再次感谢 shane): dm1 <- melt(d[,c("Type","I.alt
我有一个多索引数据框,两个索引是 Sample 和 Lithology Sample 20EC-P 20EC-8 20EC-10-1 ... 20EC-43
我有一个关于 ggplot2 中的图例的问题。我设法在同一张图中绘制了三条线,并想添加使用三种颜色的图例。这是使用的代码 library(ggplot2) ## edit from original
我如何使用 seaborn 中的 lineplot 绘图功能来创建点之间没有连接线的绘图。我知道该函数称为 lineplot,但它具有合并所有具有相同 x 值的数据点并绘制单个均值和置信区间的有用功能
我如何使用 seaborn 中的 lineplot 绘图功能来创建点之间没有连接线的绘图。我知道该函数称为 lineplot,但它具有合并所有具有相同 x 值的数据点并绘制单个均值和置信区间的有用功能
我昨天发布了类似的内容,但什么也没得到。我今天花了几个小时解决问题,但没有取得任何进展。 我正在使用处理(语言)并尝试实现一种在两点之间绘制一条线的方法。 (我不想使用库的 line() 方法。) 我
我有一个如下所示的数据框 df: ind group people value value_50 1 1 5 100 1 1 2 2 90
我正在使用 seaborn.lineplot()创建一个像这样的线图(一条代表平均值的线,被一个代表标准的带包围): sns.lineplot(x="trial", y="rvalues", hue=
我有 jqplot 线图,其中 y 轴作为金额,x 轴作为日期。我需要详细说明将鼠标悬停在图表线上时显示的金额。如果有人知道请重播。谢谢.... 最佳答案 您尝试过荧光笔插件吗? http://www
我已经实现了折线图,并且运行良好。但对于某些值,该线不可见或被切断。我附上了截图。对于诸如 [Entry, x: 0.0 y: 0.0, Entry, x: 1.0 y: 0.0, Entry, x:
我正在尝试改编动画线图示例,来自:http://bl.ocks.org/benjchristensen/1148374 function draw(id, width, height, u
我是一名优秀的程序员,十分优秀!