I have found code to make bar charts and pie charts in plotly also other 3D plots.
A simple bar chart works like this:
from plotly.offline import plot
from plotly.graph_objs import *
trace1 = Bar(
x=['cats', 'dogs', 'monkeys'],
y=[20, 14, 23]
)
data = Data([trace1])
plot(data)
Is there any option available in plotly to plot this bar graph in 3D layout. Also for pie chart/donut also?
Have a look at the official documentation:
https://plot.ly/python/3d-charts/
As this should be a list of all available 3D Charts in plot.ly:
No, it seems there is currently no option for 3D Bar/Pie/Donut.
See also: https://community.plot.ly/t/will-there-be-3d-bar-charts-in-the-future/1045/2
Bar/Pie/Donut are two-dimensional by nature, making them 3D would provide no additional value (apart from cosmetics)
As suggested in the link above, you could try using 3D filled line plots.
Though I doubt that the additional complexity required to get the desired result is worth it.
Still, 3D bar charts haven't been implemented in Plotly. There is a variant of 3D bar chart plotting function at https://github.com/buran21/barchart3d-plotly, intended for drawing of 1D labelled data. An example:
import plotly.data as pdata
from barchart3d import barchart3d
df = pdata.gapminder()
df = df[df['year'] == 2007].sort_values(by='pop', ascending=False).head(10)
fig = barchart3d(
df['country'].to_list(), (df['pop']/1e06).round(1).to_list(),
'Top 10 most populous countries in 2007 [Gapminder]', 'Population, mln',
colorscale='Bluered', opacity=0.6, flatshading=True)
fig.show()
Related
I'm trying to reproduce a chart that I made in Python using matplotlib using plotly as I would like my chart to have more interactivity and potentially other features that plotly can offer in combination with Dash.
I want to achieve a couple of things so I will ask the following questions:
Is it possible in Plotly to overlay multiple pie charts, one on top of each other but of different sizes?
How can I adjust the radius in Plotly? I know this option is available in matplotlib but I haven't found that in Plotly.
Is there a way to control each slice individually such as adjust the radius to make the slice longer, adjust opacity but an individual slice? I found an example with hiding a particular slice but this one changes the pie chart completely, I would like for the slice that was turned off/made invisible to stay there, pie chart shouldn't change its composition. https://community.plotly.com/t/is-there-a-way-to-hide-some-slices-by-default-in-a-pie-chart/884
Is there a way to use continuous colors/colormap in a pie chart? From what I've read the documentation only mentions the option to use discrete.
Below I attempted to make a smaller pie chart on top of another using some example from the documentation but I failed to do so. Btw, I'm using some custom data.
import plotly.graph_objs as go
df = pd.read_csv('data.csv', index_col=0)
# change index to string
df.index = df.index.map(str)
names = ['Null', 'Pred=-1', 'Pred=0', 'Other', 'R_min'] + df.index[5:].tolist()
trace0 = go.Pie(
values=df['sum_cnt'], labels=names, opacity=0.5)
trace1 = go.Pie(
values=df['sum_cnt'], marker_colors=px.colors.sequential.Sunset,
textinfo='none',
hoverinfo='none',
domain={'x': [0.33, 0.66], 'y': [0.0, 0.6]})
data = [trace0,trace1]
layout = go.Layout(title="Chart",
)
fig = go.Figure(data=data, layout=layout, layout_showlegend=False)
fig.show()
Here is a Plotly Express scatterplot with marker color, size and symbol representing different fields in the data frame. There is a legend for symbol and a colorbar for color, but there is nothing to indicate what marker size represents.
Is it possible to display a "size" legend? In the legend I'm hoping to show some example marker sizes and their respective values.
A similar question was asked for R and I'm hoping for a similar results in Python. I've tried adding markers using fig.add_trace(), and this would work, except I don't know how to make the sizes equal.
import pandas as pd
import plotly.express as px
import random
# create data frame
df = pd.DataFrame({
'X':list(range(1,11,1)),
'Y':list(range(1,11,1)),
'Symbol':['Yes']*5+['No']*5,
'Color':list(range(1,11,1)),
'Size':random.sample(range(10,150), 10)
})
# create scatterplot
fig = px.scatter(df, y='Y', x='X',color='Color',symbol='Symbol',size='Size')
# move legend
fig.update_layout(legend=dict(y=1, x=0.1))
fig.show()
Scatterplot Image:
Thank you
You can not achieve this goal, if you use a metric scale/data like in your range. Plotly will try to always interpret it like metric, even if it seems/is discrete in the output. So your data has to be a factor like in R, as you are showing groups. One possible solution could be to use a list comp. and convert everything to a str. I did it in two steps so you can follow:
import pandas as pd
import plotly.express as px
import random
check = sorted(random.sample(range(10,150), 10))
check = [str(num) for num in check]
# create data frame
df = pd.DataFrame({
'X':list(range(1,11,1)),
'Y':list(range(1,11,1)),
'Symbol':['Yes']*5+['No']*5,
'Color':check,
'Size':list(range(1,11,1))
})
# create scatterplot
fig = px.scatter(df, y='Y', x='X',color='Color',symbol='Symbol',size='Size')
# move legend
fig.update_layout(legend=dict(y=1, x=0.1))
fig.show()
That gives:
Keep in mind, that you also get the symbol label, as you now have TWO groups!
Maybe you want to sort the values in the list before converting to string!
Like in this picture (added it to the code above)
UPDATE
Hey There,
yes, but as far as I know, only in matplotlib, and it is a little bit hacky, as you simulate scatter plots. I can only show you a modified example from matplotlib, but maybe it helps you so you can fiddle it out by yourself:
from numpy.random import randn
z = randn(10)
red_dot, = plt.plot(z, "ro", markersize=5)
red_dot_other, = plt.plot(z*2, "ro", markersize=20)
plt.legend([red_dot, red_dot_other], ["Yes", "No"], markerscale=0.5)
That gives:
As you can see you are working with two different plots, to be exact one plot for each size legend. In the legend these plots are merged together. Legendsize is further steered through markerscale and it is linked to markersize of each plot. And because we have two plots with TWO different markersizes, we can create a plot with different markersizes in the legend. markerscale is normally a value between 0 and 1 but you can also do 150% thus 1.5.
You can achieve this through fiddling around with the legend handler in matplotlib see here:
https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html
I am trying to create a waterfall chart, which is like a bar chart, except that each bar starts at the end of its neighboring bars, at the end or beginning, so you have the total, and can see how it breaks down.
I am trying to create this chart in python, but there are no direct charts in matplot.lib called waterfall.
I found code for a vertical waterfall, but I could not transform it to horizontal.
How can I transform a barh matplot chart, for example, to a horizontal waterfall?
I want to create a HORIZONTAL waterfall.
For example, I am trying to make each bar in barh chart in matplotlib start at end of other, but I do not think I am approaching the problem the right way, because I have no results so far.
It should look like this:
Code to create the plot:
my_plot = trans.plot(
kind='barh',
stacked=True,
bottom=blank,legend=None,
figsize=(10, 5)
)
How do I separate the bars?
EDIT
I have found this ready to use python package, but it doesn't work with dataframes, so I cannot use it.
import waterfall_chart
from matplotlib import transforms
a = ['sales','returns','credit fees','rebates','late charges','shipping']
b = [10,-30,-7.5,-25,95,-7]
my_plot = waterfall_chart.plot(a, b, rotation_value=30, sorted_value=True, threshold=0.2,
formatting="$ {:,.1f}", net_label="end result", other_label="misc",
Title="chart", x_lab="X", y_lab="money", blue_color="blue",
green_color="#95ff24", red_color="r")
rot = transforms.Affine2D().rotate_deg(90)
my_plot.show()
I also found this tutorial, with code, for a vertical waterfall chart.
https://pbpython.com/waterfall-chart.html.
It works great, but I didn't manage to reproduce the same thing for a horizontal waterfall.
I want to plot bar graph or graphs in python using a Pandas dataframe using two columns that don't contain numeric. One column is Operating System, another is computer name, I want to plot a graph between them showing which OS is running over how many Systems, the sample data is like below.
How can I plot bar graph or other graphs for these two colums. When I try the code below:
ax = dfdefault[['Operating System','Computer Name']].plot(kind='bar')
ax.set_xlabel("Hour", fontsize=12)
ax.set_ylabel("V", fontsize=12)
plt.show()
I get this error:
Error:
TypeError: Empty 'DataFrame': no numeric data to plot
You will need to count the occurrence of each operating system first and then plot using a bar graph or pie chart. bar expects numeric data already, which you don't have. Counting will take care of this. Here is an example using a pie chart:
df = pd.DataFrame(
[['asd', 'win'],
['sdf', 'mac'],
['aww', 'win'],
['dd', 'linux']],
columns=['computer', 'os']
)
df['os'].value_counts().plot.pie()
A bar chart would work similarly. Just change pie to bar.
I was plotting a line graph and a bar chart in matplotlib and both individually were working fine with my script.
but i'm facing a problem:
1. if i want to plot both graphs in the same output window
2. if i want to customize the display window to 1024*700
in 1 st case I was using subplot to plot two graphs in same window but i'm not being able to give both graphs their individual x-axis and y-axis names and also their individual title.
my failed code is:
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
xs,ys = np.loadtxt("c:/users/name/desktop/new folder/x/counter.cnt",delimiter = ',').T
fig = plt.figure()
lineGraph = fig.add_subplot(211)
barChart = fig.add_subplot(212)
plt.title('DISTRIBUTION of NUMBER')
lineGraph = lineGraph.plot(xs,ys,'-') #generate line graph
barChart = barChart.bar(xs,ys,width=1.0,facecolor='g') #generate bar plot
plt.grid(True)
plt.axis([0,350,0,25]) #controlls axis for charts x first and then y axis.
plt.savefig('new.png',dpi=400)
plt.show()
but with this I am not being able to mark both graphs properly.
and also please site some idea about how to resize the window to 1024*700.
When you say
I was using subplot to plot two graphs in same window but i'm not being able to give both graphs their individual x-axis and y-axis names and also their individual title.
do you mean you want to set axis labels? If so try using lineGraph.set_xlabel and lineGraph.set_ylabel. Alternatively, call plt.xlabel and plot.ylabel just after you create a plot and before you create any other plots. For example
# Line graph subplot
lineGraph = lineGraph.plot(xs,ys,'-')
lineGraph.set_xlabel('x')
lineGraph.set_ylabel('y')
# Bar graph subplot
barChart = barChart.bar(xs,ys,width=1.0,facecolor='g')
barChart.set_xlabel('x')
barChart.set_ylabel('y')
The same applies to the title. Calling plt.title will add a title to the currently active plot. This is the last plot that you created or the last plot you actived with plt.gca. If you want a title on a specific subplot use the subplot handle: lineGraph.set_title or barChart.set_title.
fig.add_subplot returns a matplotlib Axes object. Methods on that object include set_xlabel and set_ylabel, as described by Chris. You can see the full set of methods available on Axes objects at http://matplotlib.sourceforge.net/api/axes_api.html.