- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些数据框:
Date_Week Game_Order Game_Mode
0 2010-04-13 124 a
1 2010-06-29 7 a
2 2009-03-17 40 b
3 2010-08-03 54 a
4 2009-09-29 20 e
5 2009-07-07 13 b
6 2009-04-07 29 a
7 2010-03-30 37 b
8 2010-04-13 118 a
9 2010-07-13 12 b
10 2011-08-23 38 c
11 2009-06-02 96 a
12 2010-03-16 48 a
13 2010-08-10 18 b
14 2010-11-09 1 c
其中 Date_Week
列是日期时间时间戳:
dft['Date_Week'][0]
Out[1043]: Timestamp('2010-04-13 00:00:00')
我正在使用 pandas plot_date()
函数绘制此图。
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(22,8))
ax.plot_date(dft['Date_Week'], dft['Game_Order'], marker='o', markersize=2, mew=2)
ax.tick_params('y', colors='k')
ax.set_xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Weekly Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)
xtick_locator = mpl.dates.MonthLocator(interval=6)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_major_locator(xtick_locator)
ax.xaxis.set_major_formatter(xtick_formatter)
xtick_locator = mpl.dates.MonthLocator(bymonth=[2,3,4,5,6,8,9,10,11,12], interval=1)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_minor_locator(xtick_locator)
ax.xaxis.set_minor_formatter(xtick_formatter)
plt.setp(ax.xaxis.get_minorticklabels(), rotation=90, size=10)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90, size=15)
fig.subplots_adjust(bottom=0.24)
plt.legend()
plt.show()
这给出:
我想根据 Game_Mode
列中的类别为点着色。
我做了一些研究,发现this问题,this问题,this问题和this .
基于此,我尝试了以下各项:根据类别将 float 、seaborn RGB 调色板值或特定颜色映射到颜色列:
# Colouring
categories = dft['Game_Mode'].unique()
# Possible Way 1
colors = np.linspace(0, 1, len(categories))
colordict = dict(zip(categories, colors))
# Possible Way 2
colors = sns.color_palette()
colordict_1 = dict(zip(categories, colors))
# Possible Way 3
colors = ['r', 'g', 'b', 'c' ,'k']
colordict_2 = dict(zip(categories, colors))
dft["Colour"] = dft['Game_Mode'].apply(lambda x: colordict[x])
dft["Colour_1"] = dft['Game_Mode'].apply(lambda x: colordict_1[x])
dft["Colour_2"] = dft['Game_Mode'].apply(lambda x: colordict_2[x])
dft
Date_Week Game_Order Game_Mode Colour \
0 2010-04-13 124 a 0.000000
1 2010-06-29 7 a 0.000000
2 2009-03-17 40 b 0.333333
3 2010-08-03 54 a 0.000000
4 2009-09-29 20 e 0.666667
5 2009-07-07 13 b 0.333333
6 2009-04-07 29 a 0.000000
7 2010-03-30 37 b 0.333333
8 2010-04-13 118 a 0.000000
9 2010-07-13 12 b 0.333333
10 2011-08-23 38 c 1.000000
11 2009-06-02 96 a 0.000000
12 2010-03-16 48 a 0.000000
13 2010-08-10 18 b 0.333333
14 2010-11-09 1 c 1.000000
Colour_1 Colour_2
0 (0.298039215686, 0.447058823529, 0.690196078431) r
1 (0.298039215686, 0.447058823529, 0.690196078431) r
2 (0.333333333333, 0.658823529412, 0.407843137255) g
3 (0.298039215686, 0.447058823529, 0.690196078431) r
4 (0.76862745098, 0.305882352941, 0.321568627451) b
5 (0.333333333333, 0.658823529412, 0.407843137255) g
6 (0.298039215686, 0.447058823529, 0.690196078431) r
7 (0.333333333333, 0.658823529412, 0.407843137255) g
8 (0.298039215686, 0.447058823529, 0.690196078431) r
9 (0.333333333333, 0.658823529412, 0.407843137255) g
10 (0.505882352941, 0.447058823529, 0.698039215686) c
11 (0.298039215686, 0.447058823529, 0.690196078431) r
12 (0.298039215686, 0.447058823529, 0.690196078431) r
13 (0.333333333333, 0.658823529412, 0.407843137255) g
14 (0.505882352941, 0.447058823529, 0.698039215686) c
但是,当我尝试使用任何 Colour
列进行绘图时,出现以下错误:
0:
# Colour error
fig, ax = plt.subplots(figsize=(22,8))
ax.plot_date(dft['Date_Week'], dft['Game_Order'], marker='o', markersize=2, mew=2, c=dft['Colour'])
Out[1058]: [<matplotlib.lines.Line2D at 0x289091d0>]Traceback (most recent call last):
File "path\Anaconda3\lib\site-packages\IPython\core\formatters.py", line 307, in __call__
return printer(obj)
File "C:\Users\p\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 240, in <lambda>
png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
File "C:\Users\p\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 124, in print_figure
fig.canvas.print_figure(bytes_io, **kw)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2200, in print_figure
**kwargs)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 545, in print_png
FigureCanvasAgg.draw(self)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 464, in draw
self.figure.draw(self.renderer)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1144, in draw
renderer, self, dsu, self.suppressComposite)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 2426, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\lines.py", line 828, in draw
rgbaFace = self._get_rgba_face()
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\lines.py", line 1340, in _get_rgba_face
rgbaFace = mcolors.to_rgba(facecolor, self._alpha)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\colors.py", line 143, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\colors.py", line 198, in _to_rgba_no_colorcycle
raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4
<matplotlib.figure.Figure at 0x2c4705c0>
1
# Colour_1 error:
...
File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\colors.py", line 194, in _to_rgba_no_colorcycle
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: 0 (0.298039215686, 0.447058823529, 0.690196078431)
2
# Colour 2 error:
...
File "C:\Users\p\Anaconda3\lib\site-packages\pandas\core\generic.py", line 2970, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'lower'
<matplotlib.figure.Figure at 0x29ec9dd8>
我搜索了这些错误并提出了以下问题:
https://github.com/matplotlib/matplotlib/issues/7603 https://github.com/matplotlib/matplotlib/issues/6266/ https://github.com/matplotlib/matplotlib/issues/2148 Setting colors using color cycle on date plots using `plot_date()`
这些表明这可能与plot_date
有关,但所有建议的选项都不起作用。
谁能告诉我如何解决这个问题?
最佳答案
为了 Elsa Li 的利益:
根据 @ImportanceOfBeingErnest 的回答,我必须为每种游戏类型和游戏顺序绘制单独的图。
我必须扩展创建游戏顺序列的方式,而不是使用全局顺序
计算每个游戏子类型的顺序。这意味着必须创建新列 "Game_Order_a"
"Game_order_b"
等,其中仅包含与该 game_mode
对应的行的值,并且 NaN
在其他地方(即仅具有特定游戏类型的值)。
然后,这意味着单独绘制数据子集并让Seaborn
手动分配颜色。这些颜色由图例处理程序拾取:
因此,对于 5 种不同的 Game_Mode
,我这样做了:
fig, ax = plt.subplots()
# Subset your data by choosing where Game_Mode = 'a'
ax.plot_date(df[df['Game_Mode'] == 'a']['Date_Week_a'], df[df['Game_Mode'] == 'a']['Game_Order_a'], marker='o', markersize=2, mew=2, label='a')
ax.plot_date(df[df['Game_Mode'] == 'b']['Date_Week_b'], df[df['Game_Mode'] == 'b']['Game_Order_b'], marker='o', markersize=2, mew=2, label='b')
# ... etc for each type that you have
plt.legend(fontsize=15, markerscale=4)
plt.show()
这将为您提供每种类型的绘图,并自动选择颜色。如果您喜欢使用 c=
参数,您可以指定它们,但对我来说没有必要。
关于python - Matplotlib pandas plot_date 颜色类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51280959/
我无法在此图中定位轴标签。我喜欢放置顶部标签,使管道与网格对齐,并放置左右标签,以便它们不接触绘图。 我试过了 ax.tick_params(axis='both', which='both'
我使用的是 python 2,下面的代码只是使用了一些示例数据,我的实际数据可能有不同的长度,并且可能不是很细。 import numpy as np import datetime i
给定坐标 [1,5,7,3,5,10,3,6,8]为 matplotlib.pyplot ,如何突出显示或着色线条的不同部分。例如,列表中的坐标 1-3 ( [1,5,7,3] ) 表示属性 a .我
我正在matplotlib中绘制以下图像。 我的问题是,图像看起来像这样,但是,我想使背景变暗,因为当我打印该图像时,灰度部分不会出现在打印物中。有人可以告诉我API进行此更改吗? 我使用简单的API
这是关于matplotlib的一个非常基本的问题,但是我不知道该怎么做: 我想绘制多个图形,并使用绘制窗口中的箭头从一个移到另一个。 目前,我只知道如何创建多个图并将其绘制在不同的窗口中,如下所示:
在 matplotlib 中绘制小块对象时,由于显示分辨率而引入了伪影。使用抗锯齿并不能解决问题。 这个问题有解决方案吗? import matplotlib.pyplot as plt impo
对于直方图,有一个简单的内置选项 histtype='step' .如何制作相同风格的条形图? 最佳答案 [阅读评论后添加答案] 将可选关键字设置为 fill=False对于条形图: import m
我正在尝试在 (6X3) 网格上创建子图。我对图例的位置有疑问。图例对所有子图都是通用的。 lgend 现在与 y 轴标签重叠 我尝试删除 constrained_layout=True 选项。但这在
我有一个带有一些线段( LineCollection )和一些点的图表。这些线和点有一些与它们相关的值,但没有绘制出来。我希望能够添加鼠标悬停工具提示或其他方法来轻松找到点和线的关联值。这对于点或线段
我想创建一个带有对齐不同曲线文本的图例的图。这是一个最小的工作示例: import matplotlib.pyplot as plt import numpy as np x=np.linspace(
可以说我正在用matplotlib绘制一条线并添加一个图例。 在图例中,其显示为------ Label。当绘制较小的图形尺寸以进行打印时,我发现该行的默认水平长度太长。 是否存在将------ La
我正在使用 matplotlib 构建一个 3D 散点图,但无法使生成的图形具有所有 3 个轴的共同原点。我怎样才能做到这一点? 我的代码(到目前为止),我还没有为轴规范实现任何定义,因为我对 Pyt
我有一个我想使用的绘图布局,其中 9 个不同的数据簇被布置在一个方形网格上。网格中的每个框都包含 3 个并排布置的箱线图。 我最初的想法是这将适合 3x3 子图布局,每个单独的子图本身被划分为 3x1
我的图形从y=-1变为y=10 我想在任意位置写一小段文字,例如x=2000,y=5: ax.annotate('MgII', xy=(2000.0, 5.0), xycoords='data')
我想使用LateX格式来构建一个表达式,其中出现一些数字,但这些数字是用LateX表达式中的变量表示的。 实际的目标是在axes.annotate()方法中使用它,但是为了讨论起见,这里是一个原理代码
我需要比较两组的二维分布。 当我使用 matplotlib.pyplot.contourf并覆盖图,每个等高线图的背景颜色填充整个图空间。有没有办法让每个等高线图的最低等高线级别透明,以便更容易看到每
在R中,有一个locator函数,类似于Matlab的ginput,您可以用鼠标单击图形并选择任何x,y坐标。此外,还有一个名为identify(x,y)的函数,如果您给它绘制了一组绘制的点x,y,然
我想用matplotlib生成矢量图。我尽力了-但输出是光栅图像。这是我使用的: import matplotlib matplotlib.use('Agg') import matplotlib.p
我正在尝试使用 matplotlib 制作具有非常小的灰点的散点图。由于点密度的原因,点需要很小。问题是 scatter() 函数的标记似乎既有线条又有填充。当标记很小时,只有线条可见,而看不到填充,
我不太明白为什么我无法在指定的限制内创建水平和垂直线。我想用这个框绑定(bind)数据。然而,双方似乎并没有遵守我的指示。为什么是这样? # CREATING A BOUNDING BOX # BOT
我是一名优秀的程序员,十分优秀!