2014-10-01 131 views
0

你好的Python/Matplotlib大师,Matplotlib - 设置轴标签在没有蜱

我想在其中特定水平线绘制的随机点来标记y轴。

我的Y轴不应该有任何值,只显示主要的刻度。

为了清楚地说明我的要求,我将使用一些截图。 我有什么当前: enter image description here 我想要什么: enter image description here

正如你所看到的,E1E2不完全在大刻度线。实际上,我知道y轴值(尽管它们应该是隐藏的,因为它是一个模型图)。我也知道E1E2的数值。

我将不胜感激。

让我的代码片段如下:

ax3.axis([0,800,0,2500) #You can see that the major YTick-marks will be at 500 intervals 
ax3.plot(x,y) #plot my lines 
E1 = 1447 
E2 = 2456 
all_ticks = ax3.yaxis.get_all_ticks() #method that does not exist. If it did, I would be able to bind labels E1 and E2 to the respective values. 

感谢您的帮助!

编辑: 对于另一个图形,我使用此代码为标签提供各种颜色。这很好地工作。 energy_rangelabels_energycolors_energy是numpy的阵列一样大,我的y轴,在我的情况下,2500

#Modify the labels and colors of the Power y-axis 
for i, y in enumerate(energy_range): 
    if (i == int(math.floor(E1))): 
     labels_energy[i] = '$E_1$' 
     colors_energy[i] = 'blue' 

    elif (i == int(math.floor(E2))): 
     labels_energy[i] = '$E_2$' 
     colors_energy[i] = 'green' 
    else: 
     labels_energy.append('') 

#Modify the colour of the energy y-axis ticks 
for color,tick in zip(colors_energy,ax3.yaxis.get_major_ticks()): 
    print color, tick 
    if color: 
     print color 
     tick.label1.set_color(color) #set the color property 
ax3.get_yaxis().set_ticklabels(labels_energy) 

EDIT2: 虚拟值全部样本:

#!/bin/python 
import matplotlib 
# matplotlib.use('Agg') #Remote, block show() 

import numpy as np 
import pylab as pylab 
from pylab import * 
import math 

import matplotlib.pyplot as plt 
from matplotlib.ticker import MultipleLocator 
import matplotlib.font_manager as fm 
from matplotlib.font_manager import FontProperties 
import matplotlib.dates as mdates 
from datetime import datetime 
import matplotlib.cm as cm 
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 

from scipy import interpolate 

def plot_sketch(): 
    x = np.arange(0,800,1) 
    energy_range = range (0,2500,1) #Power graph y-axis range 
    labels_energy = [''] * len(energy_range) 
    colors_energy = [''] * len(energy_range) 
    f1=4 
    P1=3 
    P2=2 
    P3=4 
    f2=2 
    f3=6 

    #Set Axes ranges  
    ax3.axis([0,800,0,energy_range[-1]]) 

    #Add Energy lines; E=integral(P) dt 
    y=[i * P1 for i in x] 
    ax3.plot(x,y, color='b') 
    y = [i * P2 for i in x[:0.3*800]] 
    ax3.plot(x[:0.3*800],y, color='g') 
    last_val = y[-1] 
    y = [(i * P3 -last_val) for i in x[(0.3*800):(0.6*800)]] 
    ax3.plot(x[(0.3*800):(0.6*800)],y, color='g') 

    E1 = x[-1] * P1 
    E2 = (0.3 * x[-1]) * P2 + x[-1] * (0.6-0.3) * P3 

    #Modify the labels and colors of the Power y-axis 
    for i, y in enumerate(energy_range): 
     if (i == int(math.floor(E1))): 
      labels_energy[i] = '$E_1$' 
      colors_energy[i] = 'blue' 

     elif (i == int(math.floor(E2))): 
      labels_energy[i] = '$E_2$' 
      colors_energy[i] = 'green' 
     else: 
      labels_energy.append('') 

    #Modify the colour of the power y-axis ticks 
    for color,tick in zip(colors_energy,ax3.yaxis.get_major_ticks()): 
     if color: 
      tick.label1.set_color(color) #set the color property 

    ax3.get_yaxis().set_ticklabels(labels_energy) 

    ax3.axhline(energy_range[int(math.floor(E1))], xmin=0, xmax=1, linewidth=0.25, color='b', linestyle='--') 
    ax3.axhline(energy_range[int(math.floor(E2))], xmin=0, xmax=0.6, linewidth=0.25, color='g', linestyle='--') 
    #Show grid 
    ax3.xaxis.grid(True) 


#fig = Sketch graph 
fig = plt.figure(num=None, figsize=(14, 7), dpi=80, facecolor='w', edgecolor='k') 
fig.canvas.set_window_title('Sketch graph') 

ax3 = fig.add_subplot(111) #Energy plot 
ax3.set_xlabel('Time (ms)', fontsize=12) 
ax3.set_ylabel('Energy (J)', fontsize=12) 

pylab.xlim(xmin=0) # start at 0 
plot_sketch() 
plt.subplots_adjust(hspace=0) 
plt.show() 
+0

我加入了虚拟值代码的完整片段。 – shailenTJ 2014-10-01 11:26:50

+0

噢,我设法以一种非常冒险的方式来解决这个问题。我以1的间隔启用了所有次要的tick。然后,我将tick的可见性设置为False。然后,我遍历所有的蜱虫并在我想要的位置(例如y1 = 1447,y2 = 2456)选择蜱虫,并修改它们的值。正如你所看到的,y1和y2不能是主要的蜱虫。我会尽快发布我的哈克解决方案。我认为做我想做的事的能力应该更简单。该功能可能不适用于matplotlib 1.4.0(目前是最新版本) – shailenTJ 2014-10-01 14:01:55

回答

0

我想你正在寻找正确的转换(请检查this)。在你的情况下,我认为你想要的只是使用text方法和正确的transform kwarg。您的axhline后尝试添加这对你的plot_sketch函数调用:

ax3.text(0, energy_range[int(math.floor(E1))], 
     'E1', color='g', 
     ha='right', 
     va='center', 
     transform=ax3.get_yaxis_transform(), 
     ) 
ax3.text(0, energy_range[int(math.floor(E2))], 
     'E2', color='b', 
     ha='right', 
     va='center', 
     transform=ax3.get_yaxis_transform(), 
     ) 

get_yaxis_transform方法返回一个“混合”变换这使得x值输入到text呼叫在轴单元被绘制,并且在y的数据'数据'单位。你可以调整x数据的值(0)为-0.003或者如果你需要一点填充(或者你可以使用ScaledTranslation转换,但是如果这是一次性修正,通常是不必要的)。

你可能也想使用set_ylabel的“labelpad”选项,例如:

ax3.set_ylabel('Energy (J)', fontsize=12, labelpad=20)