I'm using Geopandas (0.11.1) to plot data on maps. I'm facing an issue with missing_kwds. As some of my values are undefined, I want them to be colored in a specific way. I do that using the missing_kwds option of the plot method.
However, when using it, the shape of the map slightly changes, which is disgraceful when switching quickly from one to the other.
Here is an example.
A map without using missing_kwds :
import geopandas
import matplotlib.pyplot as plt
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.plot()
plt.savefig('world1.png')
A map using missing_kwds :
import geopandas
import matplotlib.pyplot as plt
import numpy as np
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.loc[df.name=="China", 'pop_est'] = np.nan
df.plot(column="pop_est", missing_kwds=dict(color="lightgray"))
plt.savefig('world2.png')
Those are the two resulting maps.
world1.png:
world2.png:
In case the difference isn't clear, here is a GIF that illustrates the shape changes.
Does anyone have an idea how I could solve this issue?
Add plt.gca().set_aspect('equal') after df.plot().
I would like to know if the behavior of the following code is expected.
The first figure (Series) is saved as I would expect. The second (DataFrame) is not.
If this is not a bug, how can I achieve my (obvious) goal?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
pd.Series(np.random.randn(100)).plot()
fig.savefig('c:\\temp\\plt_series.png')
fig = plt.figure()
pd.DataFrame(np.random.randn(100,2)).plot()
fig.savefig('c:\\temp\\plt_df.png')
After saving the figure, close the current plot using plt.close() to close the current figure, otherwise the old one is still active even if the next plot is being generated. You can also use plt.close('all') to be sure all open figures are closed.
I'm trying to change from matplotlib to plotly and I have to relearn every basic move.
One of my habit was to change the edge color of every histogram I made to white so it is easier to see the bars.
On matplotlib, I would do it like that :
import matplotlib.pyplot as plt
import numpy as np
vals = np.random.rand(50)
plt.hist(vals, edgecolor='white');
Which gives me :
I suppose there is a way to do it with plotly but I searched in the doc and other stackoverflow questions and haven't found it yet.
Closest way (that make me believe it is somehow possible): setting the template of the figure to a template using white edgecolor
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import numpy as np
vals = np.random.rand(50)
fig = px.histogram(x=vals, template="simple_white")
fig.show()
I also tried to examinate the template simple_white and to look for the param that was making the edges to be white but this didn't lead to anything (if you know what this param is, the answer interests me even more !)
There is function called fig.update_traces you can increase marker(edge) width and change color to 'White' like:
fig = px.histogram(x=vals)
fig.update_traces(marker_line_width=1,marker_line_color="white")
fig.show()
Reference: https://plotly.com/python/reference/histogram/
plotly.graph_objects.Histogram accepts a Marker as input argument. That means you can enable the borders and specify their color using a marker:
import plotly.graph_objects as go
fig.show()
go.Histogram(x=vals,
marker=dict(line=dict(width=0.8,
color="white")))
To see all other parameters of a marker, see the docs here.
Trying to create a simple Box Plot using Google Colab for my Intro Python class. It is not appearing as I would like it. You can see my code and output below. I read in a file on NBA statistics, and my box plot would be based on a variable called "SHOT_CLOCK".
So far what I have:
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('file path')
plt.boxplot(df['SHOT_CLOCK'], vert=False)
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
plt.show()
Output:
Edit
In your example you are passing a Series object, try this way
plt.figure()
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
df.boxplot(column='SHOT_CLOCK')
Once you add the following Import to your code it will work:
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
Korean in pictures is not important. Sorry for showing non-english character
environment : Jupyter notebook
For this dataFrame(which read csv files), I want to make bar graph which has specific colors on each item.
so, I make some code like that...
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
font_name =font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family=font_name)
from matplotlib import colors as mcolors
colors=dict(mcolors.BASE_COLORS,**mcolors.CSS4_COLORS)
data = pd.read_csv('subway.csv')
subwayPassengerPerLine.plot.bar(color=['tab:blue','tab:green','tab:orange','tab:cyan','tab:purple','tab:brown','tab:green','tab:pink','tab:gold','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black','tab:black'])
I want to make like this one
But My code(upper code) doesn't change color.
how to change color in bar graph like second image? thanks
I believe, you don't need to use tab:"black" ...etc.
Just using
subwayPassengerPerLine.plot.bar(y = 'sum',color=['blue','green','orange','cyan','purple','brown','green','pink','gold','black','black','black','black','black','black','black','black','black','black','black','black','black','black','black','black'])
This can also help if you want to automate your plot color.
How to pick a new color for each plotted line within a figure in matplotlib?
Doc reference
https://python-graph-gallery.com/3-control-color-of-barplots/
Edited:
Missed the y = 'sum' field.
If you want to remove the useless legend, add this line too:
subwayPassengerPerLine.get_legend().remove()