- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个带有 MultiIndex 的 DataFrame:
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
# dataframe with dates
dates = pd.DataFrame()
dates['2016'] = pd.date_range(start='2016', periods=4, freq='60Min')
dates['2017'] = pd.date_range(start='2017', periods=4, freq='60Min')
dates['2018'] = pd.date_range(start='2018', periods=4, freq='60Min')
dates.reset_index()
dates = dates.unstack()
# multi-indexed dataframe
df = pd.DataFrame(np.random.randn(36, 3))
df['concept'] = np.repeat(np.repeat(['A', 'B', 'C'], 3), 4)
df['datetime'] = pd.concat([dates, dates, dates], ignore_index=True)
df.set_index(['concept', 'datetime'], inplace=True)
df.sort_index(inplace=True)
df.columns = ['V1', 'V2', 'V3']
df.info()
返回:
V1 V2 V3
concept datetime
A 2016-01-01 00:00:00 -0.303428 0.088180 -0.547776
2016-01-01 01:00:00 -0.893835 -2.226923 -0.181370
2016-01-01 02:00:00 2.934575 1.515822 0.343609
2016-01-01 03:00:00 -1.341694 1.681015 0.099759
2017-01-01 00:00:00 1.515894 0.519595 0.102635
2017-01-01 01:00:00 -0.266949 -0.035901 0.539084
2017-01-01 02:00:00 1.336603 0.286928 -0.352078
2017-01-01 03:00:00 0.480137 0.185785 0.595706
2018-01-01 00:00:00 -0.385640 1.813604 -0.839973
2018-01-01 01:00:00 0.568706 1.165257 -1.352020
2018-01-01 02:00:00 0.498388 0.382034 -1.190599
2018-01-01 03:00:00 1.897356 -0.293143 0.177787
B 2016-01-01 00:00:00 -1.111196 -1.644588 0.333936
2016-01-01 01:00:00 0.232206 -0.202987 -0.334564
2016-01-01 02:00:00 1.264637 -1.472229 0.888451
2016-01-01 03:00:00 1.033163 0.504090 1.325476
2017-01-01 00:00:00 -0.199445 0.088792 -0.797965
2017-01-01 01:00:00 -1.116359 0.574789 -1.055830
2017-01-01 02:00:00 1.267970 0.287501 0.001420
2017-01-01 03:00:00 1.554647 2.865833 0.089875
2018-01-01 00:00:00 0.030871 -1.783524 -1.457190
2018-01-01 01:00:00 0.073978 -0.735599 -0.420115
2018-01-01 02:00:00 0.931073 -2.543869 -0.649976
2018-01-01 03:00:00 0.325443 1.134799 0.445788
C 2016-01-01 00:00:00 -0.489454 -0.646136 -0.111308
2016-01-01 01:00:00 -0.501965 -0.197183 0.025899
2016-01-01 02:00:00 -0.714251 -1.846856 0.197658
2016-01-01 03:00:00 0.609357 0.456263 -0.041581
2017-01-01 00:00:00 -1.004726 -0.956688 -0.068980
2017-01-01 01:00:00 -0.036204 -1.236450 -0.895681
2017-01-01 02:00:00 -0.840374 0.561443 1.401854
2017-01-01 03:00:00 0.325433 1.406280 -1.033267
2018-01-01 00:00:00 -0.029315 -1.591510 -0.739032
2018-01-01 01:00:00 -0.761522 -0.896236 0.537450
2018-01-01 02:00:00 1.081961 0.126248 -0.911462
2018-01-01 03:00:00 0.070915 -1.036460 1.187859
并想在箱线图中绘制一个分组列:
# demonstrate how to customize the display different elements:
boxprops = dict(linestyle='-', linewidth=4, color='k')
medianprops = dict(linestyle='-', linewidth=4, color='k')
ax = df.boxplot(column=['V1'],
by=df.index.get_level_values('datetime').year,
showfliers=False, showmeans=True,
boxprops=boxprops,
medianprops=medianprops)
# get rid of the automatic title
plt.suptitle("")
ax.set_xlabel("")
ax.set_title("Boxplot of V1")
显然,boxplot 的某些样式选项有效,而有些则无效。
所以这是我的问题:
如何设置方框/中位数/均值的颜色?
提前致谢!
############################ 编辑 1 ############## ##############
我找到了 this 答案并调整了我的情节:
bp = data.boxplot(column=['eex_da_price_mean'],
by=data.index.get_level_values('date').year,
showfliers=False, showmeans=True,
return_type='dict')
[[item.set_linewidth(4) for item in bp[key]['boxes']] for key in bp.keys()]
[[item.set_linewidth(4) for item in bp[key]['fliers']] for key in bp.keys()]
[[item.set_linewidth(4) for item in bp[key]['medians']] for key in bp.keys()]
[[item.set_linewidth(4) for item in bp[key]['means']] for key in bp.keys()]
[[item.set_linewidth(4) for item in bp[key]['whiskers']] for key in bp.keys()]
[[item.set_linewidth(4) for item in bp[key]['caps']] for key in bp.keys()]
bp.set_xlabel("")
bp.set_title("Some plot", fontsize=60)
bp.tick_params(axis='y', labelsize=60)
bp.tick_params(axis='x', labelsize=60)
plt.suptitle("")
返回:
但现在轴格式不再起作用,我得到这样的错误:
bp.set_xlabel("")
AttributeError: 'OrderedDict' object has no attribute 'set_xlabel'
有什么提示吗?
最佳答案
我刚刚找到了另一种解决方案,可以直接从 pandas 中使用更少的代码进行绘图(之后无需操作 matplotlib 对象):
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
ax = df.plot(kind='box',
color=dict(boxes='r', whiskers='r', medians='r', caps='r'),
boxprops=dict(linestyle='-', linewidth=1.5),
flierprops=dict(linestyle='-', linewidth=1.5),
medianprops=dict(linestyle='-', linewidth=1.5),
whiskerprops=dict(linestyle='-', linewidth=1.5),
capprops=dict(linestyle='-', linewidth=1.5),
showfliers=False, grid=True, rot=0)
ax.set_xlabel('Foo')
ax.set_ylabel('Bar in X')
plt.show()
产量:
我唯一没有弄清楚的是如何在 showmeans=True
时调整均值的颜色。但在大多数情况下,这应该没问题..
希望对您有所帮助!
关于python - Pandas boxplot : set color and properties for box, 中位数,均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160956/
假设我有一个颜色数组(具有整个色谱,从红色到红色。)。较短的版本如下所示: public Color[] ColorArray = new Color[360] { Color.FromArgb(25
当我通过 http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.gamefriction.com%2FCoded&prof
我需要帮助来解决验证 CSS。在这里,我在 W3 验证中遇到了 20 个相同的错误。我不明白。如果您能解决此问题,我将不胜感激。 错误:两种上下文中 color 和 background-color
我正在尝试覆盖 bootstrap4 样式。 我没有使用 Sass 的经验,但这看起来像是 bootstrap SCSS 文件中的错误。 我的自定义文件是: /* custom.scss */
我是 sass 的新手,我写了一些 sass 代码,但它没有编译。 $classes : primary secondary success warning danger; $colors
我想制作一个散点图,其中每个点都有一个球体。点及其球体都根据某些列值着色。 一个显示我想要的最小示例: library(ggplot2) library(vcd) # only needed for
我正在尝试添加一个选项来更改网站颜色。所以,我有一个工作正常的色域,但问题是当鼠标悬停时我需要将颜色更改为深 10%。函数 darken 表示第一个参数必须是颜色。 Error: argument `
我正在尝试从下拉菜单中更改主题颜色,但出现此错误:“预期类型为'List>'的值,但类型为之一 'MappedListIterable>' Color selected ; MaterialApp(
有没有办法将 Inno Setup 底部面板的背景颜色更改为白色? 谢谢你的帮助! 最佳答案 您描述的底部面板实际上是向导表单的区域,因此您只需设置 Color WizardForm 的属性(prop
sublime text包Color Highlighter看起来很有用。但是,它的行为似乎并不像文档中描述的那样。 根据文档: Usage : Just click or move the curs
我有一个由 Excel 2007 创建的文档: ... 在 xl/styles.xml 的字体定义中包含以下颜色: 我从ECMA标准了解到这个颜色索引指的是收藏于 xl/styles.xml如果有
简单问题: (如何)是否可以设置 的值在 Chrome 中选择此类输入时,要在颜色选择器中清空吗? 编辑:我想将输入的值更改为 "" 最佳答案 我认为它不能设置为透明,但您可以使用value="#c
我正在尝试截屏,检查屏幕截图中的某种颜色,如果找到该颜色,则单击它。 我遇到的问题是颜色的 RGB 值必须准确。 我想知道是否可以将图像转换为颜色很少的图像。 抱歉打扰了。我没有受过适当的训练。我现在
有谁知道为什么 CSS 为文本提供了 color,但没有 font-color 或 text-color? 看起来很反直觉,有点像text-decoration: underline 而不是font-
我想弄清楚为什么 mix-blend-mode: color; css 选择器和值会影响除白色以外的所有颜色,而不是影响除白色和黑色以外的所有颜色。有人可以向我解释为什么会这样吗? 最佳答案 来自 t
我正在尝试创建 Angular 2 主题,我按照网站上提到的教程进行操作。这是我的主题文件。 @import '~@angular/material/theming'; @include mat-co
我正在尝试更改 ProgressBar 中栏的颜色 pBar.setStyle("-fx-accent: green"); 但我遇到了一个问题:这似乎不适合我! (或者我只是不明白一些事情) 这是代码
给定最大迭代次数 = 1000 给我一些关于如何着色(红色、绿色、蓝色)的想法。我现在能想到的只有蹩脚的 2 种颜色渐变:( 真的有可能想出像这样美丽的东西吗? 最佳答案 该死的,他们不会让我一个新手
要将 url 参数解码为颜色,我使用此 HttpMessageConverter: public class ColorHttpMessageConverter implements HttpMess
我创建了一个显示色谱的自定义控件。我正在覆盖 OnRender() 方法以使用颜色绘制控件。由于某种原因,“计算”的颜色(R G B 值)具有一定的透明度,即使我将 Alpha 值 (A) 设置为最大
我是一名优秀的程序员,十分优秀!