See enclosed the python file of my code.
The code is not a professional one because i just start python and matplotlib.
Thanks,
Naga
________________________________
De : John Hunter <jdh2...@gmail.com>
À : Clyng11 <coulibalyn...@yahoo.fr>
Cc : matplotlib-devel@lists.sourceforge.net
Envoyé le : Sam 30 avril 2011, 22h 45min 22s
Objet : Re: [matplotlib-devel] displaying ticklabels on a graph
On Wed, Apr 27, 2011 at 4:17 AM, Clyng11 <coulibalyn...@yahoo.fr> wrote:
>
> I run this code, but labels do not appear under the ticks.
>
> # label the X ticks with years
> self.mpl.canvas.ax.set_xticks(np.arange(len(years))+width/2,
> [int(year) for year in years])
> self.mpl.canvas.ax.set_xticklabels([str(year) for year in years])
>
> I Need an help.
Could you post a complete, free-standing example that we can run that
exposes your problem?
# used to parse files more easily
from __future__ import with_statement
# Numpy module
import numpy as np
# matplotlib colormap module
import matplotlib.cm as cm
# Matplotlib font manager
import matplotlib.font_manager as font_manager
#need for navigation tool bar
#from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
# for command-line arguments
import sys, os
# Qt4 bindings for core Qt functionalities (non-GUI)
from PyQt4 import QtCore
# Python Qt4 bindings for GUI objects
from PyQt4 import QtGui
#from PyQt4.QtGui import QAction, QIcon
from qgis.core import *
# import the MainWindow widget from the converted .ui files
from mplmainwindow import Ui_MplMainWindow
# Initialize Qt resources from file resources.py
import resources
class DoGraphCsv (QtGui.QDialog, Ui_MplMainWindow):
def __init__(self, iface):
QtGui.QDialog.__init__( self )
self.iface = iface
self.setupUi( self )
def initGui(self):
# Create action that will start plugin configuration
self.action = QtGui.QAction(QtGui.QIcon(":/plugins/graphcsvfile/csv_graph.png"), \
"Csv graph...", self.iface.mainWindow())
# self.action = QAction( QCoreApplication.translate( "mnuCsvGraph", "Csv graph", self.iface.mainWindow() )
# self.action.setIcon(QIcon(":/plugins/graphcsvfile/csv_graph.png"))
# self.calcStats.setIcon( QIcon( ":/statist.png" ) )
self.action.setWhatsThis( "Csv file graphic" )
# connect the action to the run method
QtCore.QObject.connect(self.action, QtCore.SIGNAL("triggered()"), self.run)
# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu("&Csv graph...", self.action)
def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu("&Csv graph...",self.action)
self.iface.removeToolBarIcon(self.action)
# run method that performs all the real work
def run(self):
# create and show the dialog
self.dlg = Ui_MplMainWindow()
# connect the signals with the slots
QtCore.QObject.connect(self.mplactionAdd, QtCore.SIGNAL("clicked()"), self.select_file)
QtCore.QObject.connect(self.mplpushButton, QtCore.SIGNAL("clicked()"), self.validate_entries)
# show the dialog
self.show()
result = self.exec_()
def select_file(self):
"""opens a file select dialog"""
# open the dialog and get the selected file
file = QtGui.QFileDialog.getOpenFileName()
# if a file is selected
if file:
# update the lineEdit text with the selected filename
self.mpllineEdit.setText(file)
def validate_entries(self):
# check to see that all fields have been entered
msg = ''
if self.mpllineEdit.text() == '' or \
self.xlabelEdit.text() == '' or \
self.ylabelEdit.text() == '' or \
self.titleEdit.text() == '':
# ui.lineEdit_min_version_no.text() == '' or \
# ui.lineEdit_menu_text.text() == '' or \
# ui.lineEdit_company_name.text() == '' or \
# ui.lineEdit_email_address.text() == '':
msg = 'All fields are required to create a graph\n'
# try:
# flt = float(str(ui.lineEdit_version_no.text()))
# flt = float(str(ui.lineEdit_min_version_no.text()))
# except:
# msg += 'Version numbers must be numeric'
# if msg != '':
QtGui.QMessageBox.warning(self, "Missing Information", msg)
else:
self.plot_csvfile()
def plot_csvfile(self):
width = .6
# open CSV file
with open(self.mpllineEdit.text()) as f:
# read the first line, splitting the years
years = map(int, f.readline().split(',')[1:])
# we prepare the dtype for exacting data; it's made of:
# <1 string field> <len(years) integers fields>
dtype = [('continents', 'S16')] + [('', np.int32)]*len(years)
# we load the file, setting the delimiter and the dtype above
y = np.loadtxt(f, delimiter=',', dtype=dtype)
# "map" the resulting structure to be easily accessible:
# the first column (made of string) is called 'continents'
# the remaining values are added to 'data' sub-matrix
# where the real data are
y = y.view(np.dtype([('continents', 'S16'), ('data', np.int32, len(years))]))
data = y['data']
continents = y['continents']
self.mpl.canvas.ax.clear()
# prepare the bottom array
bottom = np.zeros(len(years))
# for each line in data
for i in range(len(data)):
# create the bars for each element, on top of the previous bars
bt = self.mpl.canvas.ax.bar(range(len(data[i])), data[i], width=width, color=cm.hsv(32*i), \
label=continents[i], bottom=bottom)
# update the bottom array
bottom += data[i]
# self.mpl.canvas.ax.clear()
# label the X ticks with years
self.mpl.canvas.ax.set_xticks(np.arange(len(years))+width/2, [int(year) for year in years])
self.mpl.canvas.ax.set_xticklabels([str(year) for year in years], rotation=0, ha="center")
# some information on the plot
self.mpl.canvas.ax.set_xlabel(self.xlabelEdit.text())
self.mpl.canvas.ax.set_ylabel(self.ylabelEdit.text())
self.mpl.canvas.ax.set_title(self.titleEdit.text())
# draw a legend, with a smaller font
self.mpl.canvas.ax.legend(loc='upper left', prop=font_manager.FontProperties(size=8))
#draw navigator
# self.mpltoolbar = NavigationToolbar()
# apply the custom function as Y axis formatter
#mpl.canvas.gca().yaxis.set_major_formatter(FuncFormatter(billions))
self.mpl.canvas.draw()
------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today. Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel