2014-11-11 2 views
0

Я пытаюсь отформатировать ось X графика, сгенерированного следующим примером кода. Ось X нанесена на график в масштабе шкалы. В настоящее время график показывает [1000,2000,3000,4000,5000] с незначительными тиками = 100. Я хотел бы видеть каждые 100 в качестве маркированного основного текста от [0,1000], а затем только каждые 1000 от [1000,5000].Форматирование осей при построении с использованием matplotlib

В принципе, я хочу, чтобы ось х имела следующие метки: [100,200,300,400,500,600,700,800,900,1000,2000,3000,4000,5000]. Возможно ли использование matplotlib?

import numpy 
import math 
import random 

from matplotlib import pyplot 
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 

### ----------------------------- ### 

plot_aspect = 1.7 
plot_height = 10.0 
plot_width = int(plot_height*plot_aspect) 

pyplot.figure(figsize=(plot_width, plot_height), dpi=100) 
pyplot.subplots_adjust(left=0.10, right=0.90, top=0.90, bottom=0.10, hspace=0.30) 

### ----------------------------- ### 

title1 = 'Example Plot Title' 
main_title = 'Example Main Title' 
png_title = 'example_plot.png' 

file1_data = [] 

for i in range(10,5100,10): 
    b = (60-(2*math.log(i)))+random.uniform(-2.0,2.0) 
    file1_data.append([i,b]) 

# ---------- 

subplot1 = pyplot.subplot(111) 

majorLocator1 = MultipleLocator(1000) 
majorFormatter1 = FormatStrFormatter('%d') 
minorLocator1 = MultipleLocator(100) 

pyplot.plot(numpy.array(file1_data)[:,0],numpy.array(file1_data)[:,1],'red',linewidth=1.0,label=title1) 

pyplot.xscale('log',fontsize=10) 

pyplot.xlim(0,5000) 

pyplot.xticks(fontsize = 10) 

subplot1.xaxis.set_major_locator(majorLocator1) 
subplot1.xaxis.set_major_formatter(majorFormatter1) 
subplot1.xaxis.set_minor_locator(minorLocator1) 

subplot1.xaxis.grid() 

pyplot.legend(loc=3,prop={'size':20}) 

pyplot.yticks(fontsize = 16) 
pyplot.ylim(30,65) 
pyplot.xlabel('Freq [Hz]', fontsize=16, weight="bold") 
pyplot.ylabel('PSD [dB/Hz]', fontsize=16, weight="bold") 

pyplot.suptitle(main_title, fontsize = 28, weight="bold") 

### ----------------------------- ### 

pyplot.savefig(png_title, dpi=100) 
pyplot.show() 

enter image description here

+1

вы пробовали что-то вроде: 'xticks = диапазон (100, 1001, 100) + диапазон (1001, 5001, 1000) ', а затем' pyplot.xticks (xticks, fontsize = 10) '? – Thibaut

+0

'xticks = range (100, 1001, 100) + range (2000, 6000, 1000)', но да, что работает – HotDogCannon

ответ

1

@Thibaut помог мне с этим (с некоторыми незначительными изменениями):

import numpy 
import math 
import random 

from matplotlib import pyplot 
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 

### ----------------------------- ### 

plot_aspect = 1.7 
plot_height = 10.0 
plot_width = int(plot_height*plot_aspect) 

pyplot.figure(figsize=(plot_width, plot_height), dpi=100) 
pyplot.subplots_adjust(left=0.10, right=0.90, top=0.90, bottom=0.10, hspace=0.30) 

### ----------------------------- ### 

title1 = 'Example Plot Title' 
main_title = 'Example Main Title' 
png_title = 'example_plot.png' 

file1_data = [] 

for i in range(10,5100,10): 
    b = (60-(2*math.log(i)))+random.uniform(-2.0,2.0) 
    file1_data.append([i,b]) 

# ---------- 

subplot1 = pyplot.subplot(111) 

majorLocator1 = MultipleLocator(1000) 
majorFormatter1 = FormatStrFormatter('%d') 
minorLocator1 = MultipleLocator(100) 

pyplot.plot(numpy.array(file1_data)[:,0],numpy.array(file1_data)[:,1],'red',linewidth=1.0,label=title1) 

pyplot.xscale('log',fontsize=10) 

pyplot.xlim(0,5000) 

xticks = range(100, 1001, 100) + range(2000, 6000, 1000) 

# pyplot.xticks(fontsize = 10) 
pyplot.xticks(xticks, fontsize=10) 

# subplot1.xaxis.set_major_locator(majorLocator1) 
subplot1.xaxis.set_major_formatter(majorFormatter1) 
# subplot1.xaxis.set_minor_locator(minorLocator1) 

subplot1.xaxis.grid() 

pyplot.legend(loc=3,prop={'size':20}) 

pyplot.yticks(fontsize = 16) 
pyplot.ylim(30,65) 
pyplot.xlabel('Freq [Hz]', fontsize=16, weight="bold") 
pyplot.ylabel('PSD [dB/Hz]', fontsize=16, weight="bold") 

pyplot.suptitle(main_title, fontsize = 28, weight="bold") 

### ----------------------------- ### 

pyplot.savefig(png_title, dpi=100) 
pyplot.show() 

enter image description here

Смежные вопросы