在matplotlib中创建离散色条

时间:2018-11-18 12:28:57

标签: python matplotlib colorbar

我已经尝试了其他线程,但是无法解决。我正在尝试创建一个离散的颜色栏。许多代码似乎可以正常工作,但确实出现了离散条,但标签错误,并且引发了错误:“未找到可用于创建颜色条的可映射对象。首先定义一个可映射对象,例如图像(带有imshow)或轮廓集(带有轮廓f)。”

可以肯定的是,该错误是因为我在plt.colorbar中缺少一个参数,但是不确定其要求什么或如何定义它。

下面是我所拥有的。任何感激之情:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
norm = mpl.colors.BoundaryNorm(np.arange(-0.5,4), cmap.N) 

ex2 = sample_data.plot.scatter(x='order_count', y='total_value',c='cluster', marker='+', ax=ax, cmap='plasma', norm=norm, s=100, edgecolor ='none', alpha=0.70)

plt.colorbar(ticks=np.linspace(0,3,4))
plt.show()

1 个答案:

答案 0 :(得分:1)

实际上,def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 28 lintOptions { disable 'InvalidPackage' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.hapi.hapiapp" minSdkVersion 16 targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" multiDexEnabled true } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.google.firebase:firebase-core:16.0.4' implementation 'com.google.firebase:firebase-analytics:16.0.4' implementation 'com.google.firebase:firebase-auth:16.0.4' implementation 'com.google.firebase:firebase-firestore:17.1.1' implementation 'com.google.firebase:firebase-functions:16.1.1' implementation 'com.google.firebase:firebase-messaging:17.3.3' implementation 'com.google.firebase:firebase-storage:16.0.3' } apply plugin: 'com.google.gms.google-services' 的第一个参数应该是colorbar,这将是散点图ScalarMappable本身。

设置

PathCollection

熊猫密谋

问题在于熊猫无法直接为您提供对此import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import pandas as pd df = pd.DataFrame({"x" : np.linspace(0,1,20), "y" : np.linspace(0,1,20), "cluster" : np.tile(np.arange(4),5)}) cmap = mpl.colors.ListedColormap(["navy", "crimson", "limegreen", "gold"]) norm = mpl.colors.BoundaryNorm(np.arange(-0.5,4), cmap.N) 的访问权限。因此,可以从轴上的集合列表中捕获它,如果仅存在一个集合:ScalarMappable,这很容易。

ax.collections[0]

Matplotlib绘图

可以考虑直接使用matplotlib绘制散点图,在这种情况下,您可以直接将fig, ax = plt.subplots() df.plot.scatter(x='x', y='y', c='cluster', marker='+', ax=ax, cmap=cmap, norm=norm, s=100, edgecolor ='none', alpha=0.70, colorbar=False) fig.colorbar(ax.collections[0], ticks=np.linspace(0,3,4)) plt.show() 函数的返回值用作scatter的参数。

colorbar

两种情况下的输出都是相同的。

enter image description here