Independent spacing for ticks and ticklabels? - python-3.x

I want to have a scatter plot with ticks as marginals:
x = [ 0, 1, 1.2, 1.3, 4, 5, 6, 7, 8.2, 9, 10]
y = [.2, .4, 2, 3, 4, 5, 5.1, 5.2, 4, 3, 8]
fig, ax1 = plt.subplots()
for spine in ax1.spines.values():
spine.set_visible(False)
ax1.scatter(x, y)
ax1.set_xticks(x)
ax1.set_xticklabels([])
ax1.set_yticks(y)
ax1.set_yticklabels([])
And on top of that, I want to have ticklabels at other positions, not determined by the ticks:
xticklabels = [0, 5, 10]
yticklabels = xticklabels
How could I possibly achieve that?

Matplotlib axes have major and minor ticks. You may use the minor ticks to show the marginal locations of the points. You may turn the major ticks off but show the ticklabels for them.
To set ticks at certain positions you can use a FixedLocator. To change the appearance of the ticks or turn them off, the axes has a tick_params method.
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = [ 0, 1, 1.2, 1.3, 4, 5, 6, 7, 8.2, 9, 10]
y = [.2, .4, 2, 3, 4, 5, 5.1, 5.2, 4, 3, 8]
xticklabels = [0, 5, 10]
yticklabels = xticklabels
fig, ax = plt.subplots()
for spine in ax.spines.values():
spine.set_visible(False)
ax.scatter(x, y)
ax.xaxis.set_major_locator(ticker.FixedLocator(xticklabels))
ax.yaxis.set_major_locator(ticker.FixedLocator(yticklabels))
ax.xaxis.set_minor_locator(ticker.FixedLocator(x))
ax.yaxis.set_minor_locator(ticker.FixedLocator(y))
ax.tick_params(axis="both", which="major", bottom="off", left="off")
ax.tick_params(axis="both", which="minor", length=4)
plt.show()
Note that I personally find this plot rather difficult to grasp and if I may, I would propose something more like this:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = [ 0, 1, 1.2, 1.3, 4, 5, 6, 7, 8.2, 9, 10]
y = [.2, .4, 2, 3, 4, 5, 5.1, 5.2, 4, 3, 8]
xticklabels = [0, 5, 10]
yticklabels = xticklabels
fig, ax = plt.subplots()
ax.scatter(x, y)
ax.xaxis.set_minor_locator(ticker.FixedLocator(x))
ax.yaxis.set_minor_locator(ticker.FixedLocator(y))
c = "#aaaaaa"
ax.tick_params(axis="both", which="major", direction="out", color=c)
ax.tick_params(axis="both", which="minor", length=6, direction="in",
color="C0", width=1.5)
plt.setp(ax.spines.values(), color=c)
plt.setp(ax.get_xticklabels(), color=c)
plt.setp(ax.get_yticklabels(), color=c)
plt.show()

Related

Zorder in plots with different x-axis in matplotlib

How to get blue points in front of the gray points, please? Why the order is not working?
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(10, 6))
ax3 = ax.twiny()
ax3.errorbar([1, 2, 3, 4], [1, 2, 3, 4], yerr = [1, 2, 3, 4], fmt='o', color='gray', zorder = 1)
ax3.plot([-1,4], [1,2], c = 'black', zorder = 2)
ax3.tick_params(axis='x')
ax3.tick_params(axis='x', colors='gray')
ax3.set_xlim(-1,4)
ax.tick_params(axis='x')
ax.tick_params(axis='x', colors='mediumblue')
ax.grid(color='grey', linestyle='-', linewidth=0.5, zorder = 1)
ax.errorbar([1.1, 2.1, 3.1, 4.1, 2.91], [1.1, 2.1, 3.1, 4.1,2], yerr = [1.1, 2.1, 3.1, 4.1,1], fmt='o', color='mediumblue', zorder = 4, capsize=0.1)
plt.tight_layout()
plt.show()
You can set the order using ax.set_zorder(ax3.get_zorder()+1); ax.patch.set_visible(False) which will help bring the blue line in front.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(10, 6))
ax3 = ax.twiny()
ax3.errorbar([1, 2, 3, 4], [1, 2, 3, 4], yerr = [1, 2, 3, 4], fmt='o', color='gray', zorder = 1)
ax3.plot([-1,4], [1,2], c = 'black', zorder = 2)
ax3.tick_params(axis='x')
ax3.tick_params(axis='x', colors='gray')
ax3.set_xlim(-1,4)
ax.tick_params(axis='x')
ax.tick_params(axis='x', colors='mediumblue')
ax.grid(color='grey', linestyle='-', linewidth=0.5, zorder = 1)
ax.errorbar([1.1, 2.1, 3.1, 4.1, 2.91], [1.1, 2.1, 3.1, 4.1,2], yerr = [1.1, 2.1, 3.1, 4.1,1], fmt='o', color='mediumblue', zorder = 4, capsize=0.1)
ax.grid(axis='y')
ax.set_zorder(ax3.get_zorder()+1)
ax.patch.set_visible(False)
plt.tight_layout()
plt.show()

How to autoscale y-axis for bargraph in matplotlib?

I need to autoscale the y-axis on my bargraph in matplotlib in order to display the small differences in values. The reason why it needs to be autoscaled instead of having a fixed limit is because the values will change depending on what the user inputs. I've tried yscale log, but that doesn't work for negative values. I've tried symlog, but the graph stays the same. This is my current code:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = range(700, 710, 1)
fig, ax = plt.subplots()
ax.bar(x, y)
plt.show()
Plots are automatically scaled for the full range of the data provided to the API.
For a bar plot, the best option to display the differences in the values of the bars, is probably to set the ylim for vertical bars or xlim for horizontal bars.
negative data
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = range(-700, -750, -5)
fig, ax = plt.subplots(figsize=(7, 5))
ax.bar(x, y)
plt.ylim(min(y), max(y))
positive data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = range(700, 750, 5)
fig, ax = plt.subplots(figsize=(7, 5))
ax.bar(x, y)
plt.ylim(min(y), max(y))
mixed data
If the data has a wide range of positive and negative values, there's probably not a good option, as you've noted symlog doesn't help the issue.
The best option may be to plot the positive and negative data separately.
Creating a mask does't work with a list, so convert the lists to numpy arrays.
import numpy as np
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [700, -700, 710, -710, 720, -720, 730, -730, 740, -740]
x = np.array(x)
y = np.array(y)
mask = y >= 0 # positive mask
pos_y = y[mask] # get the positive values
neg_y = y[~mask] # get the negative values; ~ is not
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(7, 5))
ax1.bar(x[mask], pos_y) # also mask x to plot the bar at the correct x-tick
ax1.set_title('Positive Values')
ax1.set_ylim(min(pos_y), max(pos_y))
ax1.set_xticks(range(0, 12)) # buffer the number of x-ticks, so the x-ticks of the two plots align.
ax2.bar(x[~mask], neg_y)
ax2.set_title('Negative Values')
ax2.set_ylim(min(neg_y), max(neg_y))
ax2.set_xticks(range(0, 12))
plt.tight_layout() # better spacing between the two plots

How to color the edges of a 3d graph in Mayavi?

I have a 3d graph created using Mayavi and the edges have to be colored by a scalar value.
The following code creates the graph but I am not sure how to color the edges
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from mayavi import mlab
def main(edge_color=(0.8, 0.8, 0.8), edge_size=0.02):
t = [1, 2, 4, 4, 5, 3, 5]
h = [2, 3, 6, 5, 6, 4, 1]
ed_ls = [(x, y) for x, y in zip(t, h)]
G = nx.OrderedGraph()
G.add_edges_from(ed_ls)
nx.draw(G)
plt.show()
graph_pos = nx.spring_layout(G, dim=3)
# numpy array of x,y,z positions in sorted node order
xyz = np.array([graph_pos[v] for v in sorted(G)])
mlab.figure(1)
mlab.clf()
pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2])
pts.mlab_source.dataset.lines = np.array(G.edges())
tube = mlab.pipeline.tube(pts, tube_radius=edge_size)
mlab.pipeline.surface(tube, color=edge_color)
mlab.show() # interactive window
main()
Scalar values to be used for coloring the edges
scalar = [0.1, 0.7, 0.3, 0.5, 0.9, 0.8, 0.2]
Any suggestions on how to do this will be really helpful.
I also see another problem in the 3d graph that has been created. One of the edges is not connected to a node.
EDIT: From what I understand, mlab.pipeline.surface(tube, color=edge_color)
is used to color the edge/tube .
Updated code:
def main(edge_color=(0.8, 0.2, 0.8), edge_size=0.02, graph_colormap='winter'):
t = [1, 2, 4, 4, 5, 3, 5]
h = [2, 3, 6, 5, 6, 4, 1]
ed_ls = [(x, y) for x, y in zip(t, h)]
G = nx.OrderedGraph()
G.add_edges_from(ed_ls)
nx.draw(G)
plt.show()
scalars = np.array(G.nodes())+5
pprint(scalars)
e_color = [(0.8, 0.2, 0.8), (0.8, 0.2, 0.8), (0.8, 0.2, 0.8),
(0.8, 0.2, 0.8), (0.8, 0.2, 0.8), (0.8, 0.2, 0.8),
(0.8, 0.2, 0.8)]
graph_pos = nx.spring_layout(G, dim=3)
# numpy array of x,y,z positions in sorted node order
xyz = np.array([graph_pos[v] for v in sorted(G)])
mlab.figure(1)
mlab.clf()
pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
scalars,
colormap=graph_colormap
)
pts.mlab_source.dataset.lines = np.array(G.edges())
tube = mlab.pipeline.tube(pts, tube_radius=edge_size)
#mlab.pipeline.surface(tube, color=e_color) # doesn't work
mlab.pipeline.surface(tube, color=edge_color) # doesn't work
mlab.show() # interactive window
But the problems is I am no able to assign different color for different edge/tube
A possible solution, not at all automated, but sufficient for a proof of concept.
import networkx as nx
import numpy as np
from mayavi import mlab
t = [1, 2, 4, 4, 5, 3, 5]
h = [2, 3, 6, 5, 6, 4, 1]
ed_ls = [(x, y) for x, y in zip(t, h)]
G = nx.OrderedGraph()
G.add_edges_from(ed_ls)
graph_pos = nx.spring_layout(G, dim=3)
xyz = np.array([graph_pos[v] for v in G])
print(xyz.shape)
mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
np.linspace(1, 2, xyz.shape[0]),
colormap='winter', resolution=100, scale_factor=0.3)
smallTri = np.tile(xyz[-3:, :], (2, 1))[:4, :]
remEdges = np.vstack((xyz[-1, :], xyz[:-2, :]))
allEdges = np.vstack((smallTri, remEdges))
for i in range(allEdges.shape[0] - 1):
mlab.plot3d(allEdges[i:i + 2, 0], allEdges[i:i + 2, 1],
allEdges[i:i + 2, 2], color=(0.2, 1 - 0.1 * i, 0.8))
mlab.show()

How do I get the x and y labels to appear when displaying more then one histogram using pandas hist() function with the by argument?

I am trying to create a series of graphs that share x and y labels. I can get the graphs to each have a label (explained well here!), but this is not what I am looking for.
I want one label that covers the y axis of both graphs, and same for the x axis.
I've been looking at the matplotlib and pandas documentation and I was unable to find anything that addresses this issues when the using by argument.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 1, 2, 3, 4, 3, 4],
'B': [1, 7, 2, 4, 1, 4, 8, 3],
'C': [1, 4, 8, 3, 1, 7, 3, 4],
'D': [1, 2, 6, 5, 8, 3, 1, 7]},
index=[0, 1, 2, 3, 5, 6, 7, 8])
histo = df.hist(by=df['A'], sharey=True, sharex=True)
plt.ylabel('ylabel') # I assume the label is created on the 4th graph and then deleted?
plt.xlabel('xlabel') # Creates a label on the 4th graph.
plt.tight_layout()
plt.show()
The ouput looks like this.
Is there any way that I can create a Y Label that goes across the entire left side of the image (not each graph individually) and the same for the X Label.
As you can see, the x label only appears on the last graph created, and there is no y label.
Help?
This is one way to do it indirectly using the x- and y-labels as texts. I am not aware of a direct way using plt.xlabel or plt.ylabel. When passing an axis object to df.hist, the sharex and sharey arguments have to be passed in plt.subplots(). Here you can manually control/specify the position where you want to put the labels. For example, if you think the x-label is too close to the ticks, you can use 0.5, -0.02, 'X-label' to shift it slightly below.
import matplotlib.pyplot as plt
import pandas as pd
f, ax = plt.subplots(2, 2, figsize=(8, 6), sharex=True, sharey=True)
df = pd.DataFrame({'A': [1, 2, 1, 2, 3, 4, 3, 4],
'B': [1, 7, 2, 4, 1, 4, 8, 3],
'C': [1, 4, 8, 3, 1, 7, 3, 4],
'D': [1, 2, 6, 5, 8, 3, 1, 7]},
index=[0, 1, 2, 3, 5, 6, 7, 8])
histo = df.hist(by=df['A'], ax=ax)
f.text(0, 0.5, 'Y-label', ha='center', va='center', fontsize=20, rotation='vertical')
f.text(0.5, 0, 'X-label', ha='center', va='center', fontsize=20)
plt.tight_layout()
I fixed the issue with the variable number of sub-plots using something like this:
cols = 3
n = len(set(df['A']))
rows = int(n / cols) + (0 if n % cols == 0 else 1)
fig, axes = plt.subplots(rows, cols)
extra = rows * cols - n
if extra:
newaxes = []
count = 0
for row in range(rows):
for col in range(cols):
if count < n:
newaxes.append(axes[row][col])
else:
axes[row][col].axis('off')
count += 1
else:
newaxes = axes
hist = df.hist(by=df['A'], ax=newaxes)

Draw different graphs at the same position/co-ordinates in python using networkX and matplotlib

Graph 1:
Adjacency list:
2: [2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14]
3: [2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14]
5: [2, 3, 4, 5, 6, 7, 8, 9]
Plot:
`import networkx as nx
G = nx.Graph()
G1 = nx.Graph()
import matplotlib.pyplot as plt
for i, j in adj_list.items():
for k in j:
G.add_edge(i, k)
pos = nx.spring_layout(G)
nx.draw(G, with_labels=True, node_size = 1000, font_size=20)
plt.draw()
plt.figure() # To plot the next graph in a new figure
plt.show() `
Graph 1
In graph 2, I am eliminating a few edges and replotting the graph, but the position of nodes is changing, how to store the position of nodes for the next graph?
You need to re-use your pos variable while plotting the graph. nx.spring_layout returns a dictionary where the node id is the key and the values are the x,y co-ordinates of the node to be plotted. Just reuse the same pos variable and pass it as an attribute to nx.draw function like this
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G1 = nx.Graph()
adj_list = {2: [2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14],
3: [2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14],
5: [2, 3, 4, 5, 6, 7, 8, 9]}
import matplotlib.pyplot as plt
for i, j in adj_list.items():
for k in j:
G.add_edge(i, k)
pos = nx.spring_layout(G) #<<<<<<<<<< Initialize this only once
nx.draw(G,pos=pos, with_labels=True, node_size = 1000, font_size=20) #<<<<<<<<< pass the pos variable
plt.draw()
plt.figure() # To plot the next graph in a new figure
plt.show()
Now I will create a new graph and add only half the edges
cnt = 0
G = nx.Graph()
for i, j in adj_list.items():
for k in j:
cnt+=1
if cnt%2 == 0:
continue
G.add_edge(i, k)
nx.draw(G,pos=pos, with_labels=True, node_size = 1000, font_size=20) #<-- same pos variable is used
plt.draw()
plt.figure() # To plot the next graph in a new figure
plt.show()
As you can see only half the edges are added and the node positions still remain the same.

Resources