Matplotlib colormap,散点图传递颜色的第三个变量:无效的RGBA argumen

2024-09-30 16:21:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我们正在matplotlib上构建报告。每一页都有多个图表和一些文本。在

在报告数据中有100多个位置,每个位置都有一个密度。这个想法是在地图上绘制点,其中颜色(红色阴影)表示该位置的密度。在

但是,我不明白kwargs:c和cmap之间的联系轴向散射打电话,我也不明白颜色。标准化在此应用程序中。在

import pandas as pd
import matplotlib
import numpy as np
from pandas import Series, DataFrame
import csv
from scipy import stats
import matplotlib.pyplot as plt
import random
import matplotlib.colors as colors

# Get the data and transform
data = pd.read_csv('logHistThis.csv')
data.drop('Unnamed: 0', axis=1, inplace=True)
dataMean = data['Density'].mean()
data = list(data['Density'])

# I was under the impresion that the data for the colormap
# had to be between 1 and 0 so did this:
aColorScale = []
def myColorScale(theData):
    aColorScale = []
    for x in theData:
        this = x/100
        aColorScale.append(this)
    return aColorScale

aColorScale = myColorScale(data)

estimated_mu, estimated_sigma = stats.norm.fit(data)
xmin = min(data)
xmax = max(data)
x = np.linspace(xmin, xmax, 100)
pdf = stats.norm.pdf(x, loc=estimated_mu, scale=estimated_sigma)

thisRangeMin = np.log(27)
thisRangeMax = np.log(35)

q = [np.random.choice(data, 40)]
z = [ np.random.randint(1, 50, size=40)]
s = 100 *q


colormap = 'Reds'
normalize =matplotlib.colors.Normalize(vmin=xmin, vmax=xmax)
#plt.scatter(x,y,z,s=5, cmap=colormap, norm=normalize, marker='*')


fig = plt.figure(figsize=(10, 5), frameon=False, edgecolor='000000', linewidth = 1)

rect0 = .05, .05, .4, .9
rect1 = .5, .05, .4, .9

# This works great 
ax1 = fig.add_axes(rect0)#<-----------x2TopTenSummary
ax1.hist(data, bins=13, normed=True, color='c', alpha=0.05)
#ax1.fill_between(x, pdf, where=(), alpha=.2)
ax1.fill_between(x, pdf, where=((x <  thisRangeMax) & ( x > thisRangeMin)), alpha=.2, label='City Range')
ax1.vlines(dataMean, 0,  stats.norm.pdf(dataMean, loc=estimated_mu, scale=estimated_sigma), color='r')
ax1.plot(x, pdf, 'k')

# This does not work :
# It just gives blue dots
ax2= fig.add_axes(rect1)
ax2= fig.add_axes(rect1)
ax2.scatter(q,z, s=200,  cmap= 'Reds',norm=matplotlib.colors.Normalize(vmin=min(aColorScale) , vmax=max(aColorScale)))

# Tried to set the color map in a variety of ways:
# When kwarg 'c' is set to the variable 'aColorScale' i get the error

plt.show()
plt.close()

所以我的问题是,我们如何将colormap合并到这种应用程序中呢?在

具有预定尺寸(A4或字母)的图形上的多个轴。 颜色决定是第三个变量z,(不是x或y) 颜色行列式是一个浮点数,其中0<;z<;8 电话是ax而不是plt

我不清楚文件中对申请的描述:

axes.scatter的文档 color.normalize的文档

我见过很多例子,图中只有一把斧头,调用的是散开... 例如here

在我们的例子中,x,y是经度,latitude,变量是'data'一个0到8之间的浮动列表或数组。在

谢谢


Tags: theimportnormdatapdfmatplotlib颜色as
2条回答

好吧,答案来自Tamir Lousky的2017年以色列PyCon文件。在

数据的规范化和与颜色映射的关联发生在以下代码块中:

aColorScale = data
aColorScale = np.array(aColorScale)
norm = (aColorScale - aColorScale.min())/(aColorScale.max() - aColorScale.min())
cmap= plt.get_cmap('Reds')
colors = [cmap(tl) for tl in norm]#<   thisRightHere

然后将颜色输入ax2:

^{pr2}$

我希望那些否决我的问题的人能说为什么,我们花了几个小时寻找并试图找到这个问题。在

总之,这是最后的图像:

enter image description here

虽然我对问题本身的理解有困难,但我可以告诉你,你的答案中的解决方案可以简化为通常的绘制散点图的方法:

ax2= fig.add_axes(rect1)
ax2.scatter(q,z, c=aColorScale, s=200, cmap='Reds')

相关问题 更多 >