我对matplotlib和colorbar有疑问。我的项目中有很多热图。假设我的数组是2个数组之间的除法结果。
例如(为了可视化问题):
class ProductViewModel
{
OleDbConnection Conn;
OleDbCommand Cmd;
List<Products> Productsobj;
public ProductViewModel()
{
string ExcelFilePath = @"ProductData.xlsx";
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + ";Extended Properties=Excel 12.0;Persist Security Info=True";
Conn = new OleDbConnection(excelConnectionString);
}
/// <summary>
/// Method to Get All the Records from Excel
/// </summary>
/// <returns></returns>
public async Task<List<Products>> ReadRecordFromEXCELAsync()
{
Productsobj = new List<Products>();
await Conn.OpenAsync();
Cmd = new OleDbCommand();
Cmd.Connection = Conn;
Cmd.CommandText = "Select * from [Sheet1$]";
var Reader = await Cmd.ExecuteReaderAsync();
while (Reader.Read())
{
Productsobj.Add(new Products()
{
ProductName = Reader["Product"].ToString(),
OriginYear = Convert.ToInt32(Reader[" Origin Year"]),
DevelopmentYear = Convert.ToInt32(Reader[" Development Year"]),
IncrementalValue = Convert.ToDouble(Reader[" Incremental Value"])
});
}
Reader.Close();
Conn.Close();
return Productsobj;
}
public void DisplayData()
{
int maxYear = Productsobj.Max(t => t.OriginYear);
int minYear = Productsobj.Min(t => t.OriginYear);
DateTime max = new DateTime(maxYear, 1, 1);
DateTime min = new DateTime(minYear, 1, 1);
int years = Years(min, max);
}
public int Years(DateTime start, DateTime end)
{
return (end.Year - start.Year) +
(((end.Month > start.Month) ||
((end.Month == start.Month) && (end.Day >= start.Day))) ? 1 : 0);
}
public void CalculateAccumulativeData()
{
var paymentdata = from p in Productsobj
group p.ProductName by new { };
foreach (var data in paymentdata)
{
System.Diagnostics.Debug.WriteLine(data.Key);
foreach (var listing in data)
{
System.Diagnostics.Debug.WriteLine("\t{0}", listing);
}
}
Productsobj.ForEach(item => System.Diagnostics.Debug.WriteLine(item.OriginYear + "," + item.IncrementalValue));
}
}
==&GT;但是,即使我的数据有99%的分数是好的,我从array_1和array_2获得平均值,例如,值为1。有时我有几个点(1%),例如A / B之间的结果是1000000,并使我的颜色条错误。
==&GT;如何使用彩条来打印我的热图只能在[0; 1]之间进行打印?那就是说,要有一个动态的颜色条?
我为我非常糟糕的英语道歉:/
谢谢!
答案 0 :(得分:1)
我想有两种解决方案:设置vmin
的{{1}}和vmax
参数,或掩盖数组中的无效值。
imshow
或者,如果要屏蔽无效值(假设大于1的所有内容都无效):
import numpy as np
import matplotlib.pylab as plt
A = np.random.random((20,20))
B = np.random.random((20,20))
array_1 = A/B # A and B are 2 arrays, same shape etc ..
fig1 = plt.figure()
ax1 = plt.subplot(121)
fig_step1 = ax1.imshow(array_1, interpolation='nearest')
fig1.colorbar(fig_step1,ax=ax1)
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")
ax1.set_title("Without limits")
ax2 = plt.subplot(122)
fig_step2 = ax2.imshow(array_1, interpolation='nearest', vmin=0, vmax=1)
fig1.colorbar(fig_step2, ax=ax2)
ax2.set_xlabel("X (arcmin)")
ax2.set_ylabel("Y (arcmin)")
ax2.set_title("With limits")
结果是: