I have question to branca.colormap
Below code works:
from branca.colormap import linear
x=linear.YlOrRd_09.scale(1,10)
but I would like to use a different color palette for example gnuplot or gnuplot2
Below code doesn't work:
from branca.colormap import linear
x=linear.gnuplot.scale(1,10)
I have error '_LinearColormaps' object has not attribiute 'gnuplot'. Do You know how use other pallet with linearColormap or where can I find list of available colors names ?
I have one more question, below my code
import folium
import branca.colormap as cm
color_mapa=cm.linear.YlOrRd_09.scale(1,10)
color_mapa=color_mapa.to_step(index=[10,20,30,40,50,60,70,80,90,100])
color_mapa2=color_mapa.to_linear()
m=folium.Map(location=[52,20],zoom_start=7)
color_mapa.add_to(m)
color_mapa.caption='Colors'
color_mapa.add_to(m)
color_mapa2.add_to(m)
color_mapa2.caption='Colors2'
color_mapa2.add_to(m)
m.save('mapy_test.html')
The problem is when I want add lables. I do this by 'to_step()' and define index. But then colors don't change smoothly. So I add 'to_linear()' (color_mapa2), but this change labels (Colors2 on my peacture). Is the way to keep labels and have colors change smoothly ?
You can use the dir() function from the Python standard library to see all the attributes from the linear object :
>>> dir(linear)
['Accent_03',
'Accent_04',
'Accent_05',
'Accent_06',
'Accent_07',
'Accent_08',
'Blues_03',
...]
Related
Currently I'm using command
plt.errorbar(X,Y,yerr=myYerr, fmt="o", alpha=0.5,capsize=4)
and I get default marker colours:
But what should I do to force matplotlib to be more colorblind-friendly?
According to this [1] you can use the predefined colorblind style. It should be as simple as:
import matplotlib.pyplot as plt
plt.style.use('tableau-colorblind10')
[1] https://matplotlib.org/stable/users/prev_whats_new/whats_new_2.2.html#new-style-colorblind-friendly-color-cycle
Check out the last image from this link as well:
https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html
I was working in a plot that had a pull plot, which I defined as :
fig, (ax1, ax2) = aplt.ratio_plot(name= canvasArray[kl], figsize=(800, 800), hspace=0.05)
and it was working fine, but now I have the need to add another pull plot in the image, so i tried:
fig, (ax1, ax2,ax3) = aplt.subplots(3,1,name="fig1", figsize=(850, 950))
and i got the resulting plot:
I tried some options like .set_aspect() but i keep getting the error AttributeError: 'Axes' object has no attribute 'set_aspect'. I would like that the main plot ocupy 2/4 of the full plot, and the pull plots 1/2 each, but i am having dificulties with that.
I am working in a object oriented enviroment, so i dont know if that changes things. I am using the Atlasplots package which uses matplotlib syntax. https://atlas-plots.readthedocs.io/en/latest/
I had an idea. Matplotlib.pyplot has a collection of parameters, and one of them controls the size of the plots. It is called: rcParams. This attribute internally is a dictionary that contains a lot of configurations. Take a look:
>>> from matplotlib import pyplot as plt
>>> plt.rcParams
If you run the above lines of code you get the following. Yeah, those are a lot of things, but we have one specific key that may solve our problem. It is "figure.figsize", if you select this parameter like this:
>>> plt.rcParams["figure.figsize"] = [6.0, 4.0]
You can customize the plot sizes. So I think you would be able to use this at certain locations in your code and reset it to the default values when needed.
To see what are the default values, just run this to get the output based on the key "figure.figsize":
>>> plt.rcParams["figure.figsize"]
[out]: ["your default", "values here"]
Update: October 1, 2021
I've just remembered that you can also unpack subplots (matplotlib.pyplot.subplots) and select directly the parameters, like this:
>>> fig, ax = plt.subplots(figsize=("the size", "you want"))
I've also noticed something very interesting. If you use rcParams["figure.figsize"] to control plot size, it will be persistent throughout the code, but if you use the option shown in the update, the configuration will apply only to that plot area. That is a behavior that I've observed here.
from matplotlib.pyplot import *
list = [1,3,5,7,2,4,6,8,10]
plot(list)
With my code above, the code will graph with x-axis run from 0 to 8. What should I do if I want my x-axis run from another value? i.e. my x-axis run from 120-128?
You can just use the show() function to show the graph.
Your code should look something like this.
from matplotlib.pyplot import *
list = [1,3,5,7,2,4,6,8,10]
plot(list)
show()
And also, as mentioned in the comments by #BcK, don't use keywords or in-built function names as variable names, finally don't import * , this is not a good practice. You can update your whole program to this:
import matplotlib.pyplot as plt
var_list = [1,3,5,7,2,4,6,8,10] # don't use keywords or inbuilt function names as varible names
plt.plot(var_list)
plt.show() # this is to visualize the plot
I'm trying to create a grapher using matplotlib.pyplot and want to graph a function that comes like a string
My Code is:
import matplotlib.pyplot as mpl
import numpy as np
def plot2D(*args):
mpl.grid(1)
xAxis = np.arange(args[1],args[2],args[3])
def xfunction(x,input):
return eval(input)
print(xfunction(5,args[0]))
mpl.plot(xAxis,xfunction(xAxis,args[0]))
mpl.show()
plot2D("1/(x)",-1,2,0.1)
I want it to plot the function 1/x but it looks like this when it should look like this (desmos). Am I converting the string to a function wrong or can matplotlib even be used to graph functions like that or should I use another library? How would I go about graphing a function like x**2 + y**2 = 1 ? Or functions like sin(x!) ?
There's an intrinsic problem with the function 1/x: it's not defined in 0. Now, in your code one of the values inside the range is unfortunately 0, and thus it messes up the whole thing big time. All you have to do is change the last line of code to shift the range a little bit, and increase the number of steps in order to get more accurate results: plot2D("1/x",-1.01,2,0.02). This is the plot:
If you want to eliminate the nasty line in between you'll have to change the code to split the graph into two.
Is there an equivalent plotting function and/or a simple way to make this plot.ly plot in python in "pure" python using e.g. matplotlib?
from here.
Just wondering if there is an equivalent function or similar. Cannot find any, or am not searching for the right thing. "heatmap python" only comes up with square plots, and changing their shape seems cumbersome.
To give you a simple example, the following will generate the attached plot.
from pylab import *
Z = rand(6, 100) # tried to make it look similar to your plot
c = pcolor(Z)
show()
Building on Hun answer, if you don't want your eyes to hurt too much, you can use an alternate color map. Here viridis
import matplotlib.pyplot as plt
import numpy as np
Z = np.random.rand(6, 100)
c = plt.pcolor(Z, cmap='viridis')
plt.show()
and remember: pyplot & numpy will keep your namespace tidy...