隐藏matplotlib中的轴值

时间:2016-05-04 23:16:46

标签: python matplotlib

我有这张照片:

plt.plot(sim_1['t'],sim_1['V'],'k')
plt.ylabel('V')
plt.xlabel('t')
plt.show()

enter image description here

如果我使用

,我不想隐藏这些数字
plt.axis('off')

我得到这张图片:

enter image description here

它还会隐藏标签Vt。如何在隐藏值的同时保留标签?

7 个答案:

答案 0 :(得分:71)

如果你使用matplotlib object-oriented approach,这是一项使用ax.set_xticklabels()ax.set_yticklabels()的简单任务:

import matplotlib.pyplot as plt

# Create Figure and Axes instances
fig,ax = plt.subplots(1)

# Make your plot, set your axes labels
ax.plot(sim_1['t'],sim_1['V'],'k')
ax.set_ylabel('V')
ax.set_xlabel('t')

# Turn off tick labels
ax.set_yticklabels([])
ax.set_xticklabels([])

plt.show()

答案 1 :(得分:51)

如果没有subplots,您可以普遍删除这样的刻度:

plt.xticks([])
plt.yticks([])

答案 2 :(得分:20)

这很有效。只需在<html> <head> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyBrzfgGntjIVECT_T8yHY5kf3dwH6ltz6c"></script> <script type="text/javascript"> $(document ).ready(function(){ var lax; var lox; var zlevel = Math.round(14-Math.log(20)/Math.LN2); var userLatLng = new google.maps.LatLng(lax, lox); var myOptions = { zoom : zlevel , center : userLatLng, mapTypeId : google.maps.MapTypeId.ROADMAP }; // Draw the map var mapObject = new google.maps.Map(document.getElementById("myMap"), myOptions); var userLatLng = new google.maps.LatLng(23.022505, 72.571362); var marker = new google.maps.Marker({ map: mapObject, icon: 'https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi2.png', position: userLatLng, title:"one", url: './index.php', }); var userLatLng = new google.maps.LatLng(21.170240, 72.831061); var marker = new google.maps.Marker({ map: mapObject, icon: 'https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi2.png', position: userLatLng, title:"two", url: './index.php', }); google.maps.event.addListener(marker, 'click', function() { window.location.href = this.url; //changed from markers[i] to this[i] }); marker.setMap(mapObject); }); </script> </head> <body> <div id="myMap" style="height:400px;width:400px"> </div> </body> </html> 之前粘贴它:

plt.show()

动臂。

答案 3 :(得分:4)

要完全删除刻度线,请使用:

ax.set_yticks([])
ax.set_xticks([])

否则,ax.set_yticklabels([])ax.set_xticklabels([])将保留刻度线。

答案 4 :(得分:3)

不确定这是最好的方法,但你当然可以替换这样的刻度标签:

import matplotlib.pyplot as plt
x = range(10)
y = range(10)
plt.plot(x,y)
plt.xticks(x," ")
plt.show()

在Python 3.4中,这会生成一个简单的线图,x轴上没有刻度标签。这里有一个简单的例子: http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html

这个相关问题也有一些更好的建议: Hiding axis text in matplotlib plots

我是python的新手。您的里程可能在早期版本中有所不同也许其他人可以提供帮助?

答案 5 :(得分:0)

plt.gca().axes.yaxis.set_ticklabels([])

enter image description here

答案 6 :(得分:0)

这也有效。

fig, ax = plt.subplots()
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_formatter(plt.NullFormatter())

有关自定义刻度线的好技巧,请参阅本书 https://jakevdp.github.io/PythonDataScienceHandbook/04.10-customizing-ticks.html