In QGIS (as you know), there are 31 predefined color ramps, and also there are more in cpt-city catalog. And I want access one color ramp which is in cpt-city catalog (name: visspec).
This is my code for a predifined color ramps which is working:
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setClassAttribute("totalTraffic")
myRenderer.setMode(QgsGraduatedSymbolRenderer.EqualInterval)
myRenderer.updateClasses(layer,
QgsGraduatedSymbolRenderer.EqualInterval,500000)
#get default style
myStyle = QgsStyle().defaultStyle()
#get a list of default color ramps [u'Blues', u'BrBG', u'BuGn'....]
defaultColorRampNames = myStyle.colorRampNames()
# setting ramp to Blues, first index of defaultColorRampNames
ramp = myStyle.colorRamp(defaultColorRampNames[7])
myRenderer.updateColorRamp(ramp)
layer.setRenderer(myRenderer)
QgsProject.instance().addMapLayer(layer)
And this is my code where I am trying to import directly file of the color ramp which I want, but this code is not working properly. Just giving random colored pixels.
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setClassAttribute("totalTraffic")
myRenderer.setMode(QgsGraduatedSymbolRenderer.EqualInterval)
myRenderer.updateClasses(gridMilano, QgsGraduatedSymbolRenderer.EqualInterval,10000`)
myStyle = QgsStyle().defaultStyle()
defaultColorRampNames = myStyle.colorRampNames()
ramp = myStyle.colorRamp("C:/Program Files/QGIS 3.0/apps/qgis/resources/cpt-city-qgis-min/jjg/physics/visspec.svg")`
myRenderer.updateColorRamp(ramp)
gridMilano.setRenderer(myRenderer)
QgsProject.instance().addMapLayer(gridMilano)
This is result that i get when I applied visspec ccolor ramp in QGIS directly enter image description here
And this is what I get with my second code enter image description here
Thanks in advance :)