I want to plot some impedance values and task and code are both simple. xhertz_df is a pandas dataframe and after conversion to a numpy array xhertz[0]is the real part, xhertz[1]the imaginary part and xhertz[3]represents the time between measurements.
def xhertz_plot(xhertz_df):
ax = plt.gca()
xhertz = xhertz_df.T.to_numpy()
ax.plot(xhertz[3], xhertz[0], 'green')
ax.plot(xhertz[3], xhertz[1], 'blue')
ax.scatter(xhertz[3], xhertz[0], cmap ='green')
ax.scatter(xhertz[3], xhertz[1], cmap ='blue')
ax.set_xlabel('Time Passed (in Minutes)')
plt.show()
I'm confused as to what can go wrong with this code as it seems so simple. Yet I get this result:
The upper line and points is a mix of blue and green even though it should be just green. The lower line that should be only blue has orange (?!) points. What is going on here?
Edit:
I found the problem: I used cmap instead of just c for the scatter plot. But to someone with expertise in both concepts: Why did I get the result shown above? E.g. where did the orange come from?
As stated in the docs for Axes.scatter:
A Colormap instance or registered colormap name. cmap is only used if c is an array of floats.
Since you did not provide a list of floats for the arg c, matplotlib ignored your cmap and instead used the first and second default colors (blue, then orange).
If you just want a single color, note the docs for the c argument:
If you wish to specify a single color for all points prefer the color keyword argument.
Alternatively, you can just use Axes.plot with o for the marker style, instead of scatter, e.g. ax.plot(x, y, 'o', color='green') or equivalently ax.plot(x, y, 'og'). This is more typical for simple plots; you can use - or o to explicitly set a line plot or marker plot.
Note that cmap is generally intended to be used if you want a different color for each point, like if you wanted to color the points to represent another dimension of data. In that case c would represent that third dimension of data, norm would scale the data, and cmap would be what colors are mapped to that data. See the scatter demo 2 from matplotlib for an example of how that argument is usually used.
Related
Because I am new to data analysis with python, I want to improve my skills with tutorials and adjusting working code from others.
At the moment I am working on the fruit_data_with_colors data set, and want to understand the python code, available at:
https://github.com/susanli2016/Machine-Learning-with-Python/blob/master/Solving%20A%20Simple%20Classification%20Problem%20with%20Python.ipynb
One of the examples at the beginning shows a scatter matrix of the different numeric input variables (height, width, mass, color). With the mentioned code, the colors in the plotted images are purple, brown, yellow and black. I would like to change this to more appealing colors (e.g. red, blue, green, black)
I looked at the documentation of matplotlib and think that I should adjust the "c = y" part of my code.
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.scatter.html
Trying "c = ['blue']" worked well, but if I add another color in the form of "c = ['blue', 'red']" an error occures:
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 59, 'y' with size 59.
X = fruits[feature_names]
y = fruits['fruit_label']
from matplotlib import cm
cmap = cm.get_cmap('gnuplot')
scatter = pd.scatter_matrix(X, c = y, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap = cmap)
plt.suptitle('Scatter-matrix for each input variable')
plt.savefig('fruits_scatter_matrix')```
I think the easiest way to achieve what you want is to change the colormap, just edit:
cmap = cm.get_cmap('new_color_map')
with a more appealing colormap. You can check the full list here. In addition also seaborn library provides some nice colormaps. In seaborn you can produce nice scatter matrix plots using the pairplot function!
I have been able to change the facecolor of a plot using basic colors, r,g,b etc. However, I am working on a project and I need to prepare a presentation that will be visually pleasing and I would like to use a wider range of colors, such as colors that are listed here. This is the code I am using (I want the area below the graph to be colored):
fig = plt.figure(num=None, figsize=(30, 50))
ax1 = fig.add_subplot(2,1,1)
ax1.plot(x, y, 'k-')
ax1.fill_between(x, min(y), y, facecolor='#8B0000')
However, facecolor does nothing when I use HEX colors, but it works when I use 'r','b' etc. Is there any way to use HEX color codes for fill_between?
According the docs facecolor accepts matplotlib color arg or sequence of rgba tuples. If you want to use hex colors you must first convert the hex value to correct format. Take a look at the matplotlib.colors module. I'm not fully familiar with the library but maybe hex2color is of use.
I'm plotting a meshgrid with pyplot.pcolormesh, and I want to customize the ticklabels on the colorbar. I set a list of tick positions, and provide a list of ticklabels, which should match the tick positions, but I don't know ahead of time which ticks will actually be included, since I don't know the max and the min of the data. The problem is that the first ticklabel I provide is always used at the first visible tick, regardless of whether that is the first tick in my list or not.
Working example:
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(1,10).reshape(3,3)
m = plt.pcolormesh(a)
c = plt.colorbar(m)
c.set_ticks(np.arange(11))
c.set_ticklabels(np.arange(11))
plt.savefig('mesh.png')
This code produces the image below, and the problem here is that the darkest blue is labled 0, while the value in that cell is actually 1, and similarly all the other labels are shifted by 1.
Is this a bug or a feature, and if it's a feature, how can I make sure the labels will match in an elegant manner? I guess I manage with some tests on the data and trying to figure out which tick will be the first visible and so on, but that doesn't seem very pythonic.
Its a feature, because you are setting the ticklabels yourself (with the wrong labels). Its best always trying to avoid setting the ticklabels manually, unless there is no other way.
If you remove this line, the labels will show up correctly:
c.set_ticklabels(np.arange(11))
To improve readability you could also consider normalizing the colors so they become discrete and match specific integer values. But this only works well if the total amount of colors is limited, like in this example.
fig, ax = plt.subplots()
cmap = plt.cm.jet
bounds = np.arange(0.5,10.5,1)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
m = ax.pcolormesh(a, cmap=cmap, norm=norm)
c = plt.colorbar(m, ticks=bounds-0.5)
I'm very new to python and matplotlib, and I want to create a plot with different colored lines. I know I have to use a colormap, but I'm not sure how. So I have a for loop:
for i in range(len(params)):
centers,fN = graph.createHistogram(values = NHI[i])
for j in range(len(centers)):
if params[i]!=fidVal:
vals[j] = (np.log10(origfNHI[j]/fN[j]))/(fidVal-params[i])
plt.plot(centers,vals)
I want to give each line different colors based on the difference between the value of params[i] and fidVal. If fidVal - params[i] is a negative large number, I want the line to be very red, and if it is a negative small number, I want it to be not as red. Similarly if fidVal - params[i] is positive, I want it to be blue based on that value. Finally, I want the colors to be mapped on a colorbar which would be displayed on the plot.
Alternatively, is there a way I can specify the rgb color of a line when I use plt.plot()? Like, could I say plt.plot(centers,vals,Color(0,0,0))?
What code should I use to solve this problem?
I will answer about the colormap. You can use the karg color for specify an rgb color with a tuple... It's well explained in the documentation.
"In addition, you can specify colors in many weird and wonderful ways, including full names ('green'), hex strings ('#008000'), RGB or RGBA tuples ((0,1,0,1)) or grayscale intensities as a string ('0.8'). Of these, the string specifications can be used in place of a fmt group, but the tuple forms can be used only as kwargs."
Here you have a very simple example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,1,1000)
n=50
for i in range(1,n):
y = i/float(n)*x**2
plt.plot(x,y,color=(i/float(n),(i/float(n))**4,(i/float(n))**2))
ax = plt.gca()
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
plt.show()
I have a 3 data sets, X,Y,Z which are my axes and my data respectively. They are well defined, i.e.
len(X) = len(Y) = len(Z) = len(Z[i]) = N for i in range(0,N).
I would like to make a similar to a contourf plot (I already made it), but using discrete axes, like a "contour squares", where each square (x,y) has a color given by the Z value (which is a float value).
So far I'm using the contourf(X,Y,Z), but it makes some interpolations that I don't want, I need a better visualization with squares.
Does anyone knows how to do it?
Thanks
You should use matshow or imshow plotting functions.
An important argument here is the interpolation one.
Check this example from the matplotlib gallery to see some examples.
By using matshow(), keyword arguments are passed to imshow().
matshow() sets defaults for origin, interpolation (='nearest'), and aspect.
here is an example from my own work...
# level, time and conc are previously read from a file
X,Y=[level,time]
Z=conc.transpose() # Create the data to be plotted
cax = matshow(Z, origin='lower', vmin=0, vmax=500)
# I am telling all the Z values above 500 will have the same color
# in the plot (if vmin or vmax are not given, they are taken from
# the input’s minimum and maximum value respectively)
grid(True)
cbar = colorbar(cax)
...which returns this plot: