I am trying to create two-color mappings but keep getting this error:
TypeError: to_rgb() missing 1 required positional argument: 'c'
I've tried searching this up but had trouble so I came here. Any help would be appreciated!
import numpy as np
import pandas as pd
import pickle
import matplotlib
import matplotlib.pyplot as plt
color_map = plt.cm.winter
from matplotlib.patches import RegularPolygon
import math
from PIL import Image
# Needed for custom colour mapping
from matplotlib.colors import ListedColormap,LinearSegmentedColormap
import matplotlib.colors as mcolors
c = mcolors.ColorConverter().to_rgb()
positive_cm = ListedColormap([c('#e1e5e5'),c('#d63b36')])
negative_cm = ListedColormap([c('#e1e5e5'),c('#28aee4')])
Per the documentation for matplotlib:
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.colors.to_rgb.html
you need to pass the color as an argument to the to_rgb function. If your intent is to assign that function to c, then all you need to do is remove the parens so that you're not attempting to actually call it (with no arguments):
c = mcolors.ColorConverter().to_rgb
I might suggest letting it keep a slightly more meaningful name (and maybe cleaning up some of those imports):
from matplotlib import colors
ListedColorMap = colors.ListedColorMap
to_rgb = colors.ColorConverter().to_rgb
positive_cm = ListedColormap([to_rgb('#e1e5e5'), to_rgb('#d63b36')])
negative_cm = ListedColormap([to_rgb('#e1e5e5'), to_rgb('#28aee4')])
Code Output Image
Desired Image
[
My CSV data consists of X axis value, Y axis value and Hardness value and I wanted to plot smooth heat map rather than in boxes like.
DATA:
import cv2
from skimage.io import imread, imshow
from skimage.transform import resize
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import os
from tqdm import tqdm
import pandas as pd
import seaborn as sns
from sklearn.neighbors import KernelDensity
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import style
from astropy.convolution import convolve, Gaussian2DKernel
from scipy.ndimage.filters import gaussian_filter
path = r"C:\Users\patels\Desktop\Ti/"
ids = os.listdir(path)
#print(ids)
for n, id_ in tqdm(enumerate(ids), total=len(ids)):
data = pd.read_excel(path+id_)
print(path+id_)
df1 = data[['HV 0.2', 'X pos. [mm]', 'Y pos. [mm]']]
heatmap1_data = pd.pivot_table(df1, values='HV 0.2', index=['Y pos. [mm]'], columns='X pos. [mm]')
plt.figure() #this creates a new figure on which your plot will appear
heatmap1 = sns.heatmap(heatmap1_data, cmap="viridis", vmin=300, vmax=400)
plt.title(ids[n]+'Ti Hardness Map')
The stackplot function from Matplotlib library can be used as follows :
import matplotlib.pyplot as plt
import numpy as np
import numpy.random as npr
x = np.linspace(0,10,50)
y = [npr.rand(50) for i in range(4)]
plt.stackplot(x,y)
plt.show()
I need to use PyQtGraph library for a project :
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
import numpy as np
import numpy.random as npr
x = np.linspace(0,10,50)
y = [npr.rand(50) for i in range(4)]
win = pg.GraphicsWindow()
graph = win.addPlot()
#stackplot function
QtGui.QApplication.instance().exec_()
How can I get a stackplot?
Pyqtgraph does not have a built-in stack plot feature, but you could write this yourself just by summing up the plot data before each line to be stacked, and using PlotCurveItem's fillLevel and fillBrush arguments. Example:
import pyqtgraph as pg
import numpy as np
data = np.random.normal(size=(7,10), scale=0.1, loc=1)
stacked = np.cumsum(data, axis=0)
plt = pg.plot()
for i,row in enumerate(stacked):
curve = plt.plot(row, fillLevel=0, fillBrush=(i,10))
curve.setZValue(10-i)
My code as follows:
import ConfigParser
import sys
import time
import matplotlib.pyplot as plt
import networkx as nx
import json
from networkx.algorithms import bipartite
def create_graph(senators):
G= nx.Graph()
G.add_nodes_from(senators)
return G
senators=["ab","cd","ef"]
graph = create_graph(senators)
nx.draw(graph,with_labels=True)
plt.savefig("p1.png")
graph.clear()
graph = nx.DiGraph()
print graph.nodes()
nx.draw(graph,with_labels=True)
plt.savefig("p2.png")
In my code I try to draw two pictures:p1.png and p2.png. After I draw p1.png , I clear graph. However, p2.png has the same node with p1.png.
I don't know what's wrong with my code . Because I have clear graph, so there should be nothing in p2.png
What is the problem?
There are two separate notions of "clearing". One is removing nodes and edges from the graph (graph.clear) and the other is erasing the figure (plt.clf()). You are removing the nodes and edges but not clearing the figure axes. So what you are saving in p2.png is just the original p1.png figure. Try adding a plt.clf() before the second nx.draw().
I want to build a directed graph and subscribe edges.
import os
import scipy as sc
import pylab
import networkx
import matplotlib.pyplot as plt
from networkx import *
from numpy import *
G=networkx.DiGraph()
R=[('S0','S1'),('S1','S2'),('S1','S7'),('S2','S3'),('S2','S6'),('S3','S4'),('S3','S6'),('S4','S5'),('S5','S6'),('S6','S7'),('S7','S8'),('S7','S5'),('S8','Sk') ]
G.add_edges_from([ (2,3,) ])
G.add_edges_from(R)
networkx.draw_circular(G)
plt.show()
plt.savefig("path.png");
Now I have done this. I built a graph, but I cannot think up how to subscribe edges. For example I want to mark S0 and S1 edge like "565", etc. It will make it more visual and demostrative.
Thanks in advance!
Instead of layouting and drawing in one single step (networkx.draw_circular(G)), you can layout and draw nodes, edges, node labels and edge labels separately. Here's a small example:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
R=[('S0','S1'),('S1','S2'),('S1','S7'),('S0','S7')]
G.add_edges_from(R)
# Calculate layout and get all positions
pos = nx.circular_layout(G)
# Draw everything
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edge_labels(G, pos,
{
('S0', 'S1'): 'edge1',
('S1', 'S2'): 'edge2',
('S1', 'S7'): 'edge3',
('S0', 'S7'): 'edge4'
}
)
plt.axis('off')
plt.savefig("path.png");
plt.show()
For more information about what parameters can be passed to the different drawing functions, check the documentation.
You can draw nodes and egdes selectively with:
# nodes
networkx.draw_networkx_nodes(graph, pos, nodelist=[list of the nodes])
# edges
networkx.draw_networkx_edges(graph, pos, edgelist=[list of edges])
There are more options at http://networkx.lanl.gov/reference/drawing.html#module-networkx.drawing.nx_pylab
well, I wanted to do this:
and I did it. i.e. I wanted to mark edges. It seems simple, but it wasn't so. Really.
full image is here http://s019.radikal.ru/i603/1204/2a/921bc6badfae.png
import os
import scipy as sc
import pylab
import networkx
import matplotlib.pyplot as plt
from networkx import *
from numpy import *
G=networkx.DiGraph()
R=[('S0','S1'),('S1','S2'),('S1','S7'),('S2','S3'),('S2','S6'),('S3','S4'),('S3','S6'),('S4','S5'),('S5','S6'),('S6','S7'),('S7','S8'),('S7','S5'),('S8','Sk') ]
G.add_edges_from(R)
label={R[1]:'a',R[2]:'b',R[3]:'c'}
networkx.draw_networkx_edge_labels(G,pos=networkx.graphviz_layout(G),edge_labels=label)
networkx.draw_graphviz(G)
plt.show()
plt.savefig("path.png");