几个颜色栏而不是一个

时间:2016-04-04 12:56:37

标签: python arrays numpy matplotlib

我在我的天体物理实习项目中创建了一个噪声地图信号,我的子图有点问题,因为相反,每个热图获得一个颜色条,我通过热图获得3个颜色条:

Signal over Noise map

这是我的剧本(很容易理解):

# VERIFICATION DE LA CARTE S/N
# convolution_X and convolution_mask_X are arrays

fig10 = plt.subplot(1,3,1)
step1 = convolution_locale - convolution_mask_locale
fig_step1 = plt.imshow(step1, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

fig10 = plt.subplot(1,3,2)
step2 = convolution_grande - convolution_mask_grande
fig_step2 = plt.imshow(step2, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

fig10 = plt.subplot(1,3,3)
S_N_map = step1 - step2
fig_S_N_map = plt.imshow(S_N_map, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

fig10 = plt.savefig(outname10)

您是否需要有关数组的更多信息?

如果您有任何想法,谢谢!

2 个答案:

答案 0 :(得分:2)

您的代码变得混乱,因为您没有告诉它哪个颜色栏属于哪个子图。

使用matplotlib面向对象的方法在这里会有很多帮助。如果您创建了图形和子图实例(例如使用plt.subplots()),那么您可以在每个实例上调用绘图(例如ax1.imshow)和标记(例如ax1.set_xlabel)方法,无需使用plt。然后,当您创建颜色栏(fig10.colorbar())时,可以使用ax kwarg,告诉它哪个子图创建旁边的颜色栏。

您还可以将可映射对象(例如fig_step1)提供给fig10.colorbar的调用,以确保正在使用正确的数组来创建颜色条比例。

import matplotlib.pyplot as plt
import numpy as np

# Some fake data
outname10 = 'test.png'
convolution_locale = np.random.rand(250,250)
convolution_mask_locale = np.random.rand(250,250)
convolution_grande = np.random.rand(250,250)
convolution_mask_grande = np.random.rand(250,250)

# use a nice colormap
plt.viridis()

# Create the figure and subplot instances
fig10, (ax1, ax2, ax3) = plt.subplots(1,3)

step1 = convolution_locale - convolution_mask_locale
fig_step1 = ax1.imshow(step1, interpolation='nearest')
fig10.colorbar(fig_step1,ax=ax1)
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")

step2 = convolution_grande - convolution_mask_grande
fig_step2 = ax2.imshow(step2, interpolation='nearest')
fig10.colorbar(fig_step2,ax=ax2)
ax2.set_xlabel("X (arcmin)")
ax2.set_ylabel("Y (arcmin)")

S_N_map = step1 - step2
fig_S_N_map = ax3.imshow(S_N_map, interpolation='nearest')
fig10.colorbar(fig_S_N_map,ax=ax3)
ax3.set_xlabel("X (arcmin)")
ax3.set_ylabel("Y (arcmin)")

# Create space for labels between subplots
fig10.set_tight_layout(True)

fig10.savefig(outname10)

enter image description here

答案 1 :(得分:1)

由于某些原因,fig10正在积累多余的彩条;这会产生你想要的东西(我想):

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<link rel="stylesheet" href="css/jquery-ui.min.css">
<script src="js/jquery-ui.min.js"></script>
</head>
<style>

.highlight{background-color:yellow;
    }

#wrapper {
    width:500px;
    margin:auto;
    }

.ui-menu {
    width:15em;
    }

 #cartDiv {
    border-style:solid;
    border-width:5px;
    }

 </style>


<body>

<div id = "wrapper">

<ul id = "shMenu">
    <li><a href = "Javascript:void(0)">Super Heroes</a>
        <ul>
            <li><a href = "Javascript:void(0)">Hulk</a></li>
            <li><a href = "Javascript:void(0)">Batman</a></li>
            <li><a href = "Javascript:void(0)">Spider-Man</a></li>
            </li><a href = "Javascript:void(0)">Thor</a></li>
        </ul>
    </li>

    <li><a href = "Javascript:void(0)">Comic Books</a>
        <ul>
            <li><a href = "Javascript:void(0)">Hulk</a></li>
            <li><a href = "Javascript:void(0)">Batman</a></li>
            <li><a href = "Javascript:void(0)">Spider-Man</a></li>
            <li><a href = "Javascript:void(0)">Thor</a></li>
        </ul>
    </li>
</ul><br/>  



<div id = "accordion">
<h3>Batman</h3>
<div>
A young Bruce Wayne (Christian Bale) travels to the Far East,
where he's trained in the martial arts by Henri Ducard (Liam Neeson),
a member of the mysterious League of Shadows. When Ducard reveals the League's true purpose
-- the complete destruction of Gotham City -- Wayne returns to Gotham intent on cleaning up the city without resorting to murder.
With the help of Alfred (Michael Caine), his loyal butler, and Lucius Fox (Morgan Freeman),
a tech expert at Wayne Enterprises, Batman is born.
</div>


<h3>Thor</h3>
<div>
As the son of Odin (Anthony Hopkins), king of the Norse gods,
Thor (Chris Hemsworth) will soon inherit the throne of Asgard from his aging
father. However, on the day that he is to be crowned, Thor reacts with brutality when the gods' enemies,
the Frost Giants, enter the palace in violation of their treaty. As punishment, Odin banishes Thor to Earth. While Loki (Tom Hiddleston),
Thor's brother, plots mischief in Asgard,
Thor, now stripped of his powers, faces his greatest threat..
</div>


<h3>SpiderMan</h3>
<div>
"Spider-Man" centers on student Peter Parker (Tobey Maguire) who, 
after being bitten by a genetically-altered spider, gains superhuman
strength and the spider-like ability to cling to any surface. He vows
to use his abilities to fight crime, coming to understand the words of his
beloved Uncle Ben:
"With great power comes great responsibility."
</div>


<div id = "shTabs">
<ul>
    <li><a href = "#ironman">Ironman</a></li>
    <li><a href = "#hulk">Hulk</a></li>
    <li><a href = "#thor">thor</a></li>
    <li><a href = "#spiderman">SpiderMan</a></li>
</ul>
</div>

<div id = "ironman">
A billionaire industrialist and genius inventor, Tony Stark (Robert Downey Jr.),
is conducting weapons tests overseas, but terrorists kidnap him to force him to build a devastating 
weapon. Instead, he builds an armored suit and upends his captors. Returning to America,
Stark refines the suit and uses it to combat crime and terrorism.

</div>

<div id = "hulk">
Eric Bana ("Black Hawk Down") stars as scientist Bruce Banner, 
whose inner demons transform him in the aftermath of a catastrophic experiment;
Jennifer Connelly portrays Betty Ross, whose scientific genius unwittingly helps unleash the Hulk; 
Nick Nolte plays Banner's brilliant father, who passes on a tragic legacy to his son; and Sam Elliott
portrays the commander of a top-secret military research center.

</div>

<div id = "thor">
As the son of Odin (Anthony Hopkins), king of the Norse gods,
Thor (Chris Hemsworth) will soon inherit the throne of Asgard from his aging
father. However, on the day that he is to be crowned, Thor reacts with brutality when the gods' enemies,
the Frost Giants, enter the palace in violation of their treaty. As punishment, Odin banishes Thor to Earth. While Loki (Tom Hiddleston),
Thor's brother, plots mischief in Asgard,
Thor, now stripped of his powers, faces his greatest threat..

</div>

<div id = "spiderman">
"Spider-Man" centers on student Peter Parker (Tobey Maguire) who, 
after being bitten by a genetically-altered spider, gains superhuman
strength and the spider-like ability to cling to any surface. He vows
to use his abilities to fight crime, coming to understand the words of his
beloved Uncle Ben:
"With great power comes great responsibility." 
</div>
<div></br>

<div id = "customDialog" title = "custom Dialog">
<p>A random dialog box that contains important information</p>
</div>

<a href = "Javascript:void(0)" id = "openDialog" title = "Open Dialog Box">Open Dialog</a><br/><br/>



<script type = "text/javascript">

 $('document').ready(function() {
   $("#shMenu").menu({
     position: {
        my: "center top" 
        at: "center bottom"
      };

   });

 });


</script>

</div>


</body>
</html>

请注意,我修改了绘制的数组的构造(在代码中指出)

enter image description here