2014-12-16 15 views
5

Dữ liệu tôi đang hiển thị chỉ có ý nghĩa nếu đó là số nguyên.cách buộc matplotlib chỉ hiển thị số nguyên trên trục Y

I.e. 0,2 bản ghi không có ý nghĩa về ngữ cảnh của thông tin tôi đang phân tích.

Làm cách nào để buộc matplotlib chỉ sử dụng số nguyên trên trục Y. I E. 1, 100, 5 vv? không phải 0.1, 0.2 vv

for a in account_list: 
    f = plt.figure() 
    f.set_figheight(20) 
    f.set_figwidth(20) 
    f.sharex = True 
    f.sharey=True 

    left = 0.125 # the left side of the subplots of the figure 
    right = 0.9 # the right side of the subplots of the figure 
    bottom = 0.1 # the bottom of the subplots of the figure 
    top = 0.9  # the top of the subplots of the figure 
    wspace = 0.2 # the amount of width reserved for blank space between subplots 
    hspace = .8 # the amount of height reserved for white space between subplots 
    subplots_adjust(left=left, right=right, bottom=bottom, top=top, wspace=wspace, hspace=hspace) 

    count = 1 
    for h in headings: 
     sorted_data[sorted_data.account == a].ix[0:,['month_date',h]].plot(ax=f.add_subplot(7,3,count),legend=True,subplots=True,x='month_date',y=h) 

     #set bottom Y axis limit to 0 and change number format to 1 dec place. 
     axis_data = f.gca() 
     axis_data.set_ylim(bottom=0.) 
     from matplotlib.ticker import FormatStrFormatter 
     axis_data.yaxis.set_major_formatter(FormatStrFormatter('%.0f')) 

     #This was meant to set Y axis to integer??? 
     y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) 
     axis_data.yaxis.set_major_formatter(y_formatter) 

     import matplotlib.patches as mpatches 

     legend_name = mpatches.Patch(color='none', label=h) 
     plt.xlabel("") 
     ppl.legend(handles=[legend_name],bbox_to_anchor=(0.,1.2,1.0,.10), loc="center",ncol=2, mode="expand", borderaxespad=0.) 
     count = count + 1 
     savefig(a + '.png', bbox_inches='tight') 
+0

Không chắc chắn lý do bạn bị giảm giá. Đó là một câu hỏi hay, rõ ràng với một câu trả lời không rõ ràng (ngoài việc thiết lập thủ công các dấu tích, không linh hoạt). –

Trả lời

1

Bạn có thể sửa đổi nhãn/số đánh dấu như sau. Đây là ví dụ duy nhất, vì bạn đã không cung cấp bất kỳ mã nào mà bạn có, do đó, không chắc chắn nếu nó áp dụng cho bạn hay không.

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 

fig.canvas.draw() 

# just the original labels/numbers and modify them, e.g. multiply by 100 
# and define new format for them. 
labels = ["{:0.0f}".format(float(item.get_text())*100) 
       for item in ax.get_xticklabels()] 


ax.set_xticklabels(labels) 

plt.show() 

Nếu không sửa đổi các trục x:

enter image description here

Với sự sửa đổi:

enter image description here

10

Cách linh hoạt nhất là xác định integer=True nơi tìm địa điểm đánh dấu mặc định (MaxNLocator) làm điều tương tự như sau:

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

fig, ax = plt.subplots() 

# Be sure to only pick integer tick locations. 
for axis in [ax.xaxis, ax.yaxis]: 
    axis.set_major_locator(ticker.MaxNLocator(integer=True)) 

# Plot anything (note the non-integer min-max values)... 
x = np.linspace(-0.1, np.pi, 100) 
ax.plot(0.5 * x, 22.8 * np.cos(3 * x), color='black') 

# Just for appearance's sake 
ax.margins(0.05) 
ax.axis('tight') 
fig.tight_layout() 

plt.show() 

enter image description here

Ngoài ra, bạn có thể tự thiết lập các địa điểm đánh dấu/nhãn như Marcin và Joel đề nghị (hoặc sử dụng một MultipleLocator). Nhược điểm này là bạn cần phải làm việc ra những gì đánh dấu vị trí có ý nghĩa, thay vì có matplotlib chọn một khoảng thời gian đánh dấu số nguyên hợp lý dựa trên các giới hạn trục.

+0

Cảm ơn @Joe Kington - Bạn có thể giúp tôi Tôi cảm thấy tôi đang sử dụng sai: Tôi nhận được lỗi: axis_data.set_major_locator (ticker.MaxNLocator (số nguyên = True)) Đối tượng 'AxesSubplot' không có thuộc tính 'set_major_locator'. Đây có phải là vì tôi đang cố gắng đặt trục Y thành số nguyên cho SUBPLOTS không? – yoshiserry

+0

@yoshiserry - Tôi đoán rằng bạn đang cố gọi 'ax.set_major_locator' thay vì 'ax.yaxis.set_major_locator'. Dấu vị trí đánh dấu là thuộc tính của 'Trục' (tức là trục x/y), không phải là 'Trục' (tức là cốt truyện). Hãy thử 'axis_data.yaxis.set_major_locator' (hoặc' xaxis', tùy thuộc). –

1

Nếu nó chỉ là yaxis bạn muốn thay đổi, một cách dễ dàng là để xác định bọ ve bạn muốn:

tickpos = [0,1,4,6] 

py.yticks(tickpos,tickpos) 

sẽ đặt ve 0, 1, 4 và 6. Tổng quát hơn

py.yticks([0,1,2,3], ['zero', 1, 'two', 3.0]) 

sẽ đặt nhãn của danh sách thứ hai tại vị trí trong danh sách đầu tiên. Nếu nhãn sẽ là giá trị yvalue, bạn nên sử dụng phiên bản py.yticks(tickpos,tickpos) chỉ để đảm bảo rằng bất cứ khi nào bạn thay đổi vị trí của các dấu, các nhãn sẽ nhận được cùng một thay đổi.

Nói chung, mặc dù vậy, câu trả lời của Kington sẽ cho bạn biết chỉ số nguyên là số nguyên cho trục y, nhưng hãy để nó chọn nơi bọ ve đi.

Các vấn đề liên quan