I need log scale x-axis. Here is my code:
plt.bar(critical_pressures_reversed, mercury_volume_scaled, bottom = 0, log = True, linewidth=0, align="center",width=.1)
plt.title("Mercury intrusion", fontsize=20)
plt.xlabel("Critical Pressure $P_c \, [kPa]$", fontsize=16)
plt.ylabel("Mercury volume $V_m \, [\mu m^3]$", fontsize=16)
plt.grid(b=True, which='major', color='black', linestyle='-', linewidth=1)
plt.grid(b=True, which='minor', color='gray', linestyle='-', linewidth=0.15)
frame = plt.gca()
figure = plt.gcf()
frame.set_xscale('log')
frame.set_axisbelow(True)
figure.set_size_inches(12, 6)
plt.savefig("intrusion_6n_press.png", dpi=300, bbox_inches='tight')
plt.close()
Resulting plot:
How to force pyplot to draw bars with constant width?
I am using matplotlib (1.4.2)
You could use plt.fill but the bar width should change based on the log. For instance, for a random dataset, the following lines:
import matplotlib.pyplot as plt
import numpy as np
x, y = np.random.randint(1,51,10), np.random.randint(1,51,10)
width = 1e-2
for i in range(len(x)):
plt.fill([10**(np.log10(x[i])-width), 10**(np.log10(x[i])-width), 10**(np.log10(x[i])+width), 10**(np.log10(x[i])+width)],[0, y[i], y[i], 0], 'r', alpha=0.4)
plt.bar(x,y, bottom = 0, log = True, linewidth=0, align="center",width=.1, alpha=0.4)
will produce the figure below. Everything you need to do is to choose a proper width parameter.
Related
My x-axis minor gridlines are not showing, this is my code
ax = plt.gca()
ax.minorticks_on()
plt.semilogx(data_x1,data_y1,"red")
plt.semilogx(data_x2,data_y2,"blue")
ax.grid(b=True, which='major',axis="both", color='k', linestyle='-', linewidth=0.5)
ax.grid(b=True, which='minor',axis="both", color='k', linestyle='-', linewidth=0.2)
plt.xlabel("frequency(Hz)")
plt.ylabel("Iramp(dB)")
plt.show()
enter image description here
Either I'm not sure of what you want, or your code is actually working correctly. The minor grid lines are those between the powers of 10. I made a little example to show a comparison of your plot with the minor grid lines on and off.
import numpy as np
import matplotlib.pyplot as plt
data_x1 = np.linspace(0,2,10)
data_x2 = np.linspace(0,4,10)
data_y1 = np.random.rand(10)
data_y2 = np.random.rand(10)
fig, axall =plt.subplots(1,2, figsize=(10,5))
# your code with some changes
ax = axall[0]
ax.minorticks_on()
ax.semilogx(data_x1,data_y1,"red")
ax.semilogx(data_x2,data_y2,"blue")
ax.grid(b=True, which='major',axis="both", color='k', linestyle='-', linewidth=0.5)
ax.grid(b=True, which='minor',axis="both", color='k', linestyle='-', linewidth=0.2)
ax.set_xlabel("frequency(Hz)")
ax.set_ylabel("Iramp(dB)")
# code to make the plot on the right.
ax = axall[1]
ax.minorticks_on()
ax.semilogx(data_x1,data_y1,"red")
ax.semilogx(data_x2,data_y2,"blue")
ax.grid(b=True, which='major',axis="both", color='k', linestyle='-', linewidth=0.5)
# ax.grid(b=True, which='minor',axis="both", color='k', linestyle='-', linewidth=0.2)
ax.set_xlabel("frequency(Hz)")
ax.set_ylabel("Iramp(dB)")
plt.show()
Note how I commented out your minor grid lines.
I would like to make a scatter plot with unfilled squares. markerfacecolor is not an option recognized by scatter. I made a MarkerStyle but the fill style seems to be ignored by the scatter plot. Is there a way to make unfilled markers in the scatterplot?
import matplotlib.markers as markers
import matplotlib.pyplot as plt
import numpy as np
def main():
size = [595, 842] # in pixels
dpi = 72. # dots per inch
figsize = [i / dpi for i in size]
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0,0,1,1])
x_max = 52
y_max = 90
ax.set_xlim([0, x_max+1])
ax.set_ylim([0, y_max + 1])
x = np.arange(1, x_max+1)
y = [np.arange(1, y_max+1) for i in range(x_max)]
marker = markers.MarkerStyle(marker='s', fillstyle='none')
for temp in zip(*y):
plt.scatter(x, temp, color='green', marker=marker)
plt.show()
main()
It would appear that if you want to use plt.scatter() then you have to use facecolors = 'none' instead of setting fillstyle = 'none' in construction of the MarkerStyle, e.g.
marker = markers.MarkerStyle(marker='s')
for temp in zip(*y):
plt.scatter(x, temp, color='green', marker=marker, facecolors='none')
plt.show()
or, use plt.plot() with fillstyle = 'none' and linestyle = 'none' but since the marker keyword in plt.plot does not support MarkerStyle objects you have to specify the style inline, i.e.
for temp in zip(*y):
plt.plot(x, temp, color='green', marker='s', fillstyle='none')
plt.show()
either of which will give you something that looks like this
Refer to: How to do a scatter plot with empty circles in Python?
Try adding facecolors='none' to your plt.scatter
plt.scatter(x, temp, color='green', marker=marker, facecolors='none')
When using ax.grid() and moving the spines to the middle of the plot, the grid lines go over the axes labels. Any way to stop this and move the axes labels to "front"?
EDIT: It is the ticks labels (the numbers) I'm interested in fixing, not the axis label, which can be easily moved.
EDIT: made the MWE and image match exactly
EDIT: matplotlib version 2.0.0
#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.gca()
ax.minorticks_on()
ax.grid(b=True, which='major', color='k', linestyle='-',alpha=1,linewidth=1)
ax.grid(b=True, which='minor', color='k', linestyle='-',alpha=1,linewidth=1)
x = np.linspace(-5,5,100)
y = np.linspace(-5,5,100)
plt.plot(x,y)
plt.yticks([-5,-4,-3,-2,-1,0,1,2,3,4,5])
ax.spines['left'].set_position(('data', 0))
plt.show()
I am plotting a graph with six curves, where each curve has a label. The legend is placed below the graph, but it's wider than the figure. Please see code and screenshot.
#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
for i in xrange(6):
ax.plot(x, i * x, label='long_long_name = %ix$' % i)
#ax.legend()
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
fancybox=True, shadow=True, ncol=3)
fig.tight_layout(rect=[0, 0.1, 1, 0.95])
plt.show()
How to configure the proper graph and legend size/position?
I looked at Legend Guide and this post, but couldn't figure out how to make the legend narrower.
I would simply recommend you change either the legend font size or the plot figure size. For doing so:
fig = plt.figure(figsize=(x_size, y_size))
Try using x_size = 8 and y_size = 5.
Or
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
fancybox=True, shadow=True, ncol=3, fontsize = size)
Try using size = 8.
How to change axis weight in matplotlib (make the axis much bolder)?
from pylab import *
x = [5,7,5,9,11,14]
y = [4,5,3,11,15,14]
scatter(x, y, s=50, color='green',marker='h')
show()
You can set the width of whats called a spine (a side of the axes) in Matplotlib:
fig, ax = plt.subplots()
ax.plot(np.random.randn(100).cumsum())
# The spines
plt.setp(ax.spines.values(), linewidth=3)
# The ticks
ax.xaxis.set_tick_params(width=3)
ax.yaxis.set_tick_params(width=3)
Use axhline, axvline:
axhline(linewidth=5, color='black')
axvline(linewidth=5, color='black')
axhline(linewidth=5, y=max(y)*1.1, color='black')
axvline(linewidth=5, x=max(x)*1.1, color='black')